This sounds similar to the issues I had when trying to use
extra::serialize::Encoder and Encodable - as it's set up now, I can't have
a method take a a parameter defined to be Encodable and use my choice of
Encoder to stringify it. One suggestion made was to implement Encoder for
~Encoder, but that turns out to not be possible due to the design of
Encoder.
I posted on this list and to a reddit thread on /r/rust, but it sounds like
what I was trying to do is just not possible at present.


On Sat, Mar 22, 2014 at 12:00 PM, Josh Matthews <j...@joshmatthews.net>wrote:

> Whoops, excluded the mailing list.
>
>
> On 22 March 2014 11:53, Edward Wang <edward.yu.w...@gmail.com> wrote:
>
>> `to_encode_object` has type `Encodable<Encoder<'a>>` so 
>> `to_encode_object.encode(...)`
>> requires an Encoder instance with lifetime 'a, the one defined in the
>> struct bound.
>>
>> The problem I think is that I can't write:
>>
>>     pub fn buffer_encode<T:Encodable<_>>(to_encode_object: &T) -> ~[u8]
>> {
>>
>>         let mut m = MemWriter::new();
>>         {
>>             let mut encoder = Encoder::new(&mut m as &mut io::Writer);
>>             to_encode_object.encode(&mut encoder);
>>         }
>>         m.unwrap()
>>     }
>>
>> and ask rustc to infer a proper type parameter. That would be global
>> inference.
>>
>> -Ed
>>
>>
>> On Sat, Mar 22, 2014 at 11:43 PM, Josh Matthews <j...@joshmatthews.net>wrote:
>>
>>> "m here is merely a local variable but Encoder<'a> requires a lifetime
>>> at least 'a"
>>>
>>> This statement does not make sense to me. Why does m's lifetime not
>>> satisfy the requirement?
>>>
>>> Cheers,
>>> Josh
>>>
>>>
>>> On 22 March 2014 10:24, Edward Wang <edward.yu.w...@gmail.com> wrote:
>>>
>>>> Hi,
>>>>
>>>> I'm in the process of fixing a `ty_trait` variance inference bug, which
>>>> will void the following code in json.rs:
>>>>
>>>> pub struct Encoder<'a> {
>>>>     priv wr: &'a mut io::Writer,
>>>>     priv error: io::IoResult<()>,
>>>> }
>>>> impl<'a> Encoder<'a> {
>>>>     pub fn new<'a>(wr: &'a mut io::Writer) -> Encoder<'a> {
>>>>         Encoder { wr: wr, error: Ok(()) }
>>>>     }
>>>>
>>>>     pub fn buffer_encode<T:Encodable<Encoder<'a>>>(to_encode_object:
>>>> &T) -> ~[u8]  {
>>>>         let mut m = MemWriter::new();
>>>>         {
>>>>             let mut encoder = Encoder::new(&mut m as &mut io::Writer);
>>>>             to_encode_object.encode(&mut encoder);
>>>>         }
>>>>         m.unwrap()
>>>>     }
>>>> }
>>>>
>>>> If taking a close look, m here is merely a local variable but
>>>> Encoder<'a> requires a lifetime at least 'a. A naive fix would be:
>>>>
>>>> let mut encoder = Encoder::new(cast::transmute_mut_region(&mut m));
>>>>
>>>> But of course, it is very uncivilized :) Any suggestion on how to
>>>> refactor this piece of code so it won't violate the lifetime requirement?
>>>>
>>>> Regards,
>>>> Edward
>>>>
>>>> _______________________________________________
>>>> Rust-dev mailing list
>>>> Rust-dev@mozilla.org
>>>> https://mail.mozilla.org/listinfo/rust-dev
>>>>
>>>>
>>>
>>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to