-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Introduce MaxEncodedLenNoBound
#9180
Conversation
This will be very useful in structs containing `BoundedVec`, where the second type parameter does not need to have any trait bounds, since it represents the max supported length of the `BoundedVec`.
coriolinus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that NoBound is precisely the wrong handle the generic bounds for Bounded{Vec, BTreeSet, BTreeMap}. For ease of implementation, it's throwing away data that it can use to perform a precise calculation.
I.e. if we did
#[derive(Encode, Decode, MaxEncodedLenNoBound)
struct BoundedVec<T, S>(Vec<T>, PhantomData<S>);Then what I'd expect is that BoundedVec<u8>::max_encoded_len() would produce a compile error: Vec<T> doesn't have a maximum encoded length.
What I'd expect instead is an implementation, whether manual or macro, like this:
impl<T: MaxEncodedLen, S: Get<u32>> MaxEncodedLen for BoundedVec<T, S> {
fn max_encoded_len() -> usize {
T::max_encoded_len().saturating_mul(Self::bound()).saturating_add(u32::max_encoded_len())
}
}The above could be pretty easily be made into a declarative macro; I think this extension to the proc macro isn't worth it.
The other factor in play right now is that we are currently in the process of moving the MaxEncodedLen implementation from this repo to parity-scale-codec: paritytech/parity-scale-codec#268, #9163. It would be better to wait until that move is complete before substantially editing the trait or macro.
| #[cfg(feature = "derive")] | ||
| pub use max_encoded_len_derive::MaxEncodedLen; | ||
|
|
||
| /// Same as `MaxEncodedLen`, but without bounding the generics on the derived type with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Same as `MaxEncodedLen`, but without bounding the generics on the derived type with | |
| /// Same as [`MaxEncodedLen`](macro@MaxEncodedLen), but without bounding the generics on the derived type with |
|
Yeah I see this as @coriolinus. If you don't have the bound for So, if we need this, it needs to be implemented in the scale-codec crate after the mentioned pr is merged. |
|
maybe the macro MaxEncodedLen could handle custom bounds similar to this paritytech/parity-scale-codec#263 EDIT: I was too late :-) |
Yeah, that is what I meant above :) |
This will be very useful in structs containing
BoundedVec,where the second type parameter does not need to have any
trait bounds, since it represents the max supported length
of the
BoundedVec.