mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 20:27:58 +00:00
implement BoundedEncodedLen (#8720)
* implement BoundedEncodedLen * update header * update imports * use impl_for_tuples instead of a custom macro * remove redundant where clause Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * impl for Compact<T> * impl BoundedEncodedLen for BoundedVec (#8727) * impl BoundedEncodedLen for bool * explicitly implement BoundedEncodedLen for each Compact form Turns out that u16 doesn't play nicely with the pattern; those values take two extra bytes, where all other cases take one. :( * rename BoundedEncodedLen -> MaxEncodedLen * add tests of compact encoded length Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
1f21d77ad1
commit
98744eb3b2
@@ -23,7 +23,7 @@ use sp_std::{convert::TryFrom, marker::PhantomData};
|
||||
use codec::{FullCodec, Encode, EncodeLike, Decode};
|
||||
use core::{ops::{Index, IndexMut}, slice::SliceIndex};
|
||||
use crate::{
|
||||
traits::Get,
|
||||
traits::{Get, MaxEncodedLen},
|
||||
storage::{generator, StorageDecodeLength, StorageValue, StorageMap, StorageDoubleMap},
|
||||
};
|
||||
|
||||
@@ -347,6 +347,21 @@ impl<
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> MaxEncodedLen for BoundedVec<T, S>
|
||||
where
|
||||
T: BoundedVecValue + MaxEncodedLen,
|
||||
S: Get<u32>,
|
||||
BoundedVec<T, S>: Encode,
|
||||
{
|
||||
fn max_encoded_len() -> usize {
|
||||
// BoundedVec<T, S> encodes like Vec<T> which encodes like [T], which is a compact u32
|
||||
// plus each item in the slice:
|
||||
// https://substrate.dev/rustdocs/v3.0.0/src/parity_scale_codec/codec.rs.html#798-808
|
||||
codec::Compact::<u32>::max_encoded_len()
|
||||
.saturating_add(Self::bound().saturating_mul(T::max_encoded_len()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user