Good afternoon Phillippe,

Here's how to do it, assuming you're using rust 0.8 and the json library:

```
#[feature(managed_boxes)];

extern mod extra;

use std::io::mem::MemWriter;
use extra::serialize::{Encoder, Encodable};
use extra::json;

pub fn memory_encode<
    T: Encodable<json::Encoder>
>(to_encode_object: &T) -> @mut MemWriter {
    //Serialize the object in a string using a writer
    let m = @mut MemWriter::new();
    let mut encoder = json::Encoder(m as @mut Writer);
    to_encode_object.encode(&mut encoder);
    m
}

fn main() {
}
```

Regarding the trouble returning a `MemWriter` instead of a `@mut
MemWriter`, the easiest thing would be to fix library to use `&mut ...`
instead of `@mut ...`. I'll put in a PR to do that.



On Thu, Nov 28, 2013 at 3:55 AM, Philippe Delrieu
<philippe.delr...@free.fr>wrote:

> I try to develop a function that take a Encodable parameter but I have the
> error wrong number of type arguments: expected 1 but found 0
>
> pub fn memory_encode(to_encode_object: &serialize::Encodable) -> @mut
> MemWriter  {
>    //Serialize the object in a string using a writer
>     let m = @mut MemWriter::new();
>     let mut encoder = Encoder(m as @mut Writer);
>     to_encode_object.encode(&mut encoder);
>     m
> }
>
> The encodable trait is :
> pub trait Encodable<S:Encoder> {
>     fn encode(&self, s: &mut S);
> }
>
> I try this definition
> memory_encode<T:serialize::Encodable<Encoder>>(to_encode_object: &T) ->
> @mut MemWriter
>
> But I can't use the method with a struct that implement Encodable. The
> error : mismatched types: expected `&<V31>` but found ..
>
> I have another question :
> I would like to return a MemWriter and not a @mut MemWriter . I didn't
> find a way to convert the @mut to ~
>
> Philippe Delrieu
> _______________________________________________
> 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