Skip to content
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

Add Encode/Decode/BorrowDecode constraints for struct generic type parameters in derive macro's outputs #451

Closed
songzhi opened this issue Dec 11, 2021 · 1 comment · Fixed by #454

Comments

@songzhi
Copy link
Contributor

songzhi commented Dec 11, 2021

For example:

// raw code
use bincode::Encode;

#[derive(Encode)]
struct Foo<T> {
    inner: T,
}

// current derive macro output, and it's failed to compile!
use bincode::Encode;
struct Foo<T> {
    inner: T,
}
impl<T> bincode::enc::Encode for Foo<T> {
    fn encode<E: bincode::enc::Encoder>(
        &self,
        mut encoder: E,
    ) -> core::result::Result<(), bincode::error::EncodeError> {
        bincode::enc::Encode::encode(&self.inner, &mut encoder)?;
        Ok(())
    }
}

// what it should ouput
use bincode::Encode;
struct Foo<T> {
    inner: T,
}
impl<T: Encode> bincode::enc::Encode for Foo<T> {
    fn encode<E: bincode::enc::Encoder>(
        &self,
        mut encoder: E,
    ) -> core::result::Result<(), bincode::error::EncodeError> {
        bincode::enc::Encode::encode(&self.inner, &mut encoder)?;
        Ok(())
    }
}

The current macro needs the type parameter T be T: Encode. But in Rust, the latter one is more common.
I think most of derive macros in Rust ecosystem like serde::Serialize do the latter way.
So it's better for users when bincode's derive macros do it so.

@VictorKoenders
Copy link
Contributor

I agree with this change and have implemented it in #454.

In the future we might want to make a #[bincode(bounds = "...")] attribute that overrides this, like serde has.

Alternatively I think people can just implement their own implementation, it's a simple trait to implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants