Closed pgherveou closed 1 year ago
Also, a side note only tangentially related to this PR - when reviewing this code I've noticed that a compact u16
's maximum encoded length is apparently 4 bytes!? That is strange, because for all of the other types it only takes at most one extra byte over the size of the type itself, and IIRC the bare minimum to represent a u16 in a space-efficient varint encoding should be 3 bytes (which can actually encode a number up to 21 bits, [edit]although, okay, this assumes that the encodings for different types are compatible and that the maximum size is 64-bit; with different constraints the maximum number of bits is going to be different[/edit]).
But apparently we kinda screwed this up, and that's how we encode u16
s:
assert_eq!(Compact(u16::MAX).encode().len(), 4); // Doesn't panic!
Kinda unfortunate that we waste space here.
Wait, why is this adding a new function
max_compact_encoded_len
instead of fixing the impl of themax_encoded_len
for theCompact
wrapper?
the impl that we fixed previously #508 is not wrong, but it forces us to add extra constraint on the type when used with generics.
See https://github.com/paritytech/parity-scale-codec/pull/512#discussion_r1318437939
If you have a better solution, I'll take it.
Pushed the proper solution. We add a new required trait for HasCompact::Type
. But I doubt that someone implements this trait downstream.
We need to seal this trait.
lgtm too, checking if everything compile properly on polkadot-sdk
and will merge this next and create a new release
508 fixed the generated code for MaxEncoded derive trait
This is all fine but cause some compilation errors when this field is generic, since Rust can not tell that
Compact<T>
implementsMaxEncodedLen
whenT
implements it, without extra constraints