Skip to content

Can you help me figure out how to add serde to this struct? #1207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pitdicker opened this issue Apr 2, 2018 · 5 comments
Closed

Can you help me figure out how to add serde to this struct? #1207

pitdicker opened this issue Apr 2, 2018 · 5 comments
Labels

Comments

@pitdicker
Copy link

In rust-random/rand#325 I am trying to add serde to struct, with no luck. I am kind of lost in the trait bound errors. Can you help me out?
By now I have serialization working, but no idea where to start with deserialization.

Is is about a struct where one field is a generic:

pub struct BlockRng<R: BlockRngCore + ?Sized> {
    pub results: R::Results,
    pub index: usize,
    pub core: R,
}

pub trait BlockRngCore {
    type Item;

    /// Results type. This is the 'block' an RNG implementing `BlockRngCore`
    /// generates, which will usually be an array like `[u32; 16]`.
    type Results: AsRef<[Self::Item]> + AsMut<[Self::Item]> + Default;

    /// Generate a new block of results.
    fn generate(&mut self, results: &mut Self::Results);
}

Please see the linked PR for my latest attempt.

@dtolnay
Copy link
Member

dtolnay commented Apr 2, 2018

You may need to use a handwritten generic type bound to enforce that R::Results is serializable and deserializable.

#[derive(Serialize, Deserialize)]
pub struct BlockRng<R: BlockRngCore + ?Sized> {
    #[serde(bound(
        serialize = "R::Results: Serialize",
        deserialize = "R::Results: Deserialize<'de>"))]
    pub results: R::Results,
    pub index: usize,
    pub core: R,
}

@pitdicker
Copy link
Author

Thank you! I'll try that soon, but it would have taken me hours more to find, even with the good documentation!

@pitdicker
Copy link
Author

It works 😄 .

@dtolnay
Copy link
Member

dtolnay commented Apr 19, 2018

This has been fixed in Serde 1.0.38 so the original code should work as written and the bounds on the associated type can be inferred.

#[derive(Serialize, Deserialize)]
struct BlockRng<R: BlockRngCore + ?Sized> {
    results: R::Results,
    index: usize,
    core: R,
}

@pitdicker
Copy link
Author

Wow, that is nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants