Code Monkey home page Code Monkey logo

Comments (6)

dtolnay avatar dtolnay commented on May 27, 2024 1

Yes, that is tracked in #4.

from erased-serde.

dtolnay avatar dtolnay commented on May 27, 2024

You need to provide a Serialize implementation for your trait object (impl serde::Serialize for Element). Something like this:

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;
extern crate erased_serde;

trait Element: erased_serde::Serialize + Send + Sync {
    /* ... */
}

impl serde::Serialize for Element {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        self.erased_serialize(serializer).map_err(|err| {
            serde::ser::Error::custom(err.to_string())
        })
    }
}

#[derive(Serialize)]
struct GlennPierce {
    i: u8,
}

impl Element for GlennPierce {
    /* ... */
}

#[derive(Serialize)]
struct Pipeline {
    name: String,
    elements: Vec<Box<Element>>,
}

fn main() {
    let pipeline = Pipeline {
        name: "glennpierce".to_owned(),
        elements: vec![
            Box::new(GlennPierce { i: 0 }),
            Box::new(GlennPierce { i: 1 }),
        ],
    };
    println!("{}", serde_json::to_string(&pipeline).unwrap());
}

from erased-serde.

glennpierce avatar glennpierce commented on May 27, 2024

Thanks for this.

Although for this I get something like

error[E0277]: the trait bound GlennPierce: serde::ser::Serialize is not satisfied

impl Element for GlennPierce {
| ^^^^^^^ the trait serde::ser::Serialize is not implemented for `GlennPierce

Should not the GlennPierce struct implement the erased_serde::Serialize and not serde::Serialize ?

#[derive(Serialize)]
struct GlennPierce {
i: u8,
}

from erased-serde.

dtolnay avatar dtolnay commented on May 27, 2024

Make sure you are using serde 0.8 and serde_derive 0.8. This crate has not been updated to serde 0.9 yet.

from erased-serde.

glennpierce avatar glennpierce commented on May 27, 2024

Thanks. That did it.

from erased-serde.

adaszko avatar adaszko commented on May 27, 2024

Hi, I think I just came across this issue with serde 1.0.2. Are there any plans to update this crate?

from erased-serde.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.