#How to make a wrapper trait that surrounds two other derivable traits
31 messages · Page 1 of 1 (latest)
making your trait derivable requires a derive macro
All my custom trait is a trait with zero function and just the requirement of self implementing two other traits
an alternative to derive macros would be a blanket impl, if you want the trait to always be implemented the same way for any type that has the supertraits
pub trait Syncable: Serialize + for<'a> Deserialize<'a> {}
then that sounds like you could use a blanket impl
Ye, this sounds like it wants to be a trait alias, so a blanket impl will do
How do you write a blanket trait?
a blanket impl is like ```rs
impl<T> MyTrait for T {}
Would it function the same as what I want?
pretty much
any type that implements the other traits will then implement your trait
pub trait Syncable {}
impl<T> Syncable for T where T: Serialize + for<'a> Deserialize<'a> {}
So this ^^^
you should probably also have those other traits as supertraits of Syncable
so would this change work and be derivable?
no, you would not derive your trait
it's still not derivable, you still need a macro for that, but it's no longer necessary
you wouldn't need to
The trait is now automatically implemented in every possible case where you could derive it
It's already implemented
Oh ok
Now im getting this
the trait bound T: Deserialize<'_> is not satisfied
the trait Deserialize<'_> is not implemented for TrustcClick for full compiler diagnostic
lib.rs(179, 8): required by a bound in bincode::deserialize
convert.rs(15, 98): consider further restricting this bound: + serde::Deserialize<'_>
Could you show the code you're getting this in?
/// Converts bytes into an object that implements serde Deserialize
pub fn struct_from_bytes<T>(bytes: &[u8]) -> Result<T, Box<bincode::ErrorKind>> where T: Syncable {
deserialize(bytes)
}
did you add the other traits as supertraits of Syncable?
How?
Like this
pub trait Syncable: Serialize + for<'a> Deserialize<'a> {}
like that
Okay now its fixed thanks
So whatever functions I add to the syncable trait will be applied to all objects that implement Serialize and De-serialize?
Yup