-
Notifications
You must be signed in to change notification settings - Fork 95
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
Unable to use Compact
on generic T: HasCompact
#218
Comments
You can use HasCompact without requiring other traits: #[derive(Default)]
struct Solution<V, T> {
votes1: Vec<(V, T)>,
}
impl<V: HasCompact, T: HasCompact> Encode for Solution<V, T> {
fn encode(&self) -> Vec<u8> {
let mut r = vec![];
self.votes1
.iter()
.for_each(|(v, t)| {
let compact_v = <<V as HasCompact>::Type as EncodeAsRef<'_, V>>::RefType::from(v);
let compact_t = <<T as HasCompact>::Type as EncodeAsRef<'_, T>>::RefType::from(t);
compact_v.encode_to(&mut r);
compact_t.encode_to(&mut r);
});
r
}
}
#[test]
fn test_solution() {
let s = Solution {
votes1: vec![(0u128, 0u128)]
};
assert_eq!(s.encode(), (Compact::<u128>(0u128), Compact::<u128>(0u128)).encode());
} |
I know traits are confusing but HasCompact can be used as is, the associated type |
I actually came to this, but Perhaps we either export this, or some other easier way in the upcoming release. |
Alternatively I was thinking, why not expose a |
I think this would need more reorganization in the trait itself. Because the function compact_encode in HasCompact would be expected to be same logic as HasCompact::Type::EncodeAsRef.
Maybe it has been changed since then, but as far as I see EncodeAsRef is public and reexported https://substrate.dev/rustdocs/v2.0.0-rc5/parity_scale_codec/trait.EncodeAsRef.html |
Actually the above example #218 (comment) compiles to me when I use parity-scale-codec has dependency, I think we can close this issue no ? |
yeah your examples worked fine for me as well. It is just a bit unergonomic. |
The below code demonstrates the problem. It is an attempt at doing a custom
Encode
implementation of a complex type which takes advantage of the fact that the internals of the complex type are all compact-encodable.Looking at the code, it seems to be that
<Comact<T> as Encode>
should work fine as long asT: HasCompact + CompactAs
, based on:parity-scale-codec/src/compact.rs
Lines 108 to 111 in a54a3dd
and
parity-scale-codec/src/compact.rs
Lines 135 to 138 in a54a3dd
Albeit I might be missing something.
The text was updated successfully, but these errors were encountered: