-
Notifications
You must be signed in to change notification settings - Fork 94
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
Custom types with HasCompact
#613
Comments
impl<T> CompactAs for MyWrapper<T> {
type As = T;
fn encode_as(&self) -> &Self::As {
self.0
}
fn decode_from(v: Self::As) -> Result<Self, Error> {
Ok(Self(v))
}
}
impl<T> From<Compact<MyWrapper<T>>> for MyWrapper<T> {
fn from(v: Compact<Self>) -> Self {
Self(v.0)
}
} This should do the trick |
Thanks for your fast answer! I've already tried that, but didn't work:
Basically |
Indeed If we want to support any type to have a
|
Thanks for come with a solution! As a note, I've found this "wall" because So maybe, for those who came here for the same issue as me, the easiest way to pass through is allowing non-compact types in the The related issue regarding this: paritytech/polkadot-sdk#5004 |
I see, use parity_scale_codec::*;
pub struct NumWrapper {
inner_type: u64,
}
impl From<Compact<NumWrapper>> for NumWrapper {
fn from(c: Compact<NumWrapper>) -> Self {
c.0
}
}
impl CompactAs for NumWrapper {
type As = u64;
fn encode_as(&self) -> &Self::As {
&self.inner_type
}
fn decode_from(d: Self::As) -> Result<Self, Error> {
// Allow to do some more check
Ok(NumWrapper { inner_type: d })
}
}
fn foo<T: HasCompact>(t: T) -> T {
let e = <<T as HasCompact>::Type as EncodeAsRef<T>>::RefType::from(&t).encode();
<<T as HasCompact>::Type as Decode>::decode(&mut &e[..]).unwrap().into()
} |
Wow! I was always testing with a generic Where exactly is the part that makes the generic wrapper impossible? I'm not able to see it in the crate. Where does this limitation come from? |
Ok it seems that when I implement the impl<T: CompactAs> CompactAs for NumWrapper<T> {
..
} the requirement of impl<T> CompactAs for NumWrapper<T> {
..
} it compiles |
Hi, I have a custom wrapper type that, in terms of encoding/decoding, should behave exactly as its underlying type:
I'm able to implement
Encode
/Decode
successfully, nevertheless, if this type is used as follows:The compile throws an error. Basically, it requires that
CompactRef<'a, MyWrapper<u64>>
implementsEncode/Decode
. But I can not do that because bothCompactRef
andEncode/Decode
are foreign to my crate. I can neither implementHasCompact
directly because it's already implemented forT
in the library, and to fulfill the conditions, I need theCompactRef
implementation.Is any way to create compactables custom types?
The text was updated successfully, but these errors were encountered: