@@ -20,6 +20,8 @@ use crate::alloc::vec::Vec;
2020use crate :: codec:: { Encode , Decode , Input , Output , EncodeAsRef } ;
2121use crate :: encode_like:: EncodeLike ;
2222use crate :: Error ;
23+ #[ cfg( feature = "max-encoded-len" ) ]
24+ use crate :: MaxEncodedLen ;
2325#[ cfg( feature = "fuzz" ) ]
2426use arbitrary:: Arbitrary ;
2527
@@ -206,18 +208,39 @@ impl<'de, T> serde::Deserialize<'de> for Compact<T> where T: serde::Deserialize<
206208 }
207209}
208210
211+ /// Requires the presence of `MaxEncodedLen` when the `max-encoded-len` feature is active.
212+ // Remove this trait when the feature is removed.
213+ #[ cfg( feature = "max-encoded-len" ) ]
214+ pub trait MaybeMaxEncodedLen : MaxEncodedLen { }
215+ #[ cfg( feature = "max-encoded-len" ) ]
216+ impl < T : MaxEncodedLen > MaybeMaxEncodedLen for T { }
217+
218+ /// Requires the presence of `MaxEncodedLen` when the `max-encoded-len` feature is active.
219+ // Remove this trait when the feature is removed.
220+ #[ cfg( not( feature = "max-encoded-len" ) ) ]
221+ pub trait MaybeMaxEncodedLen { }
222+ #[ cfg( not( feature = "max-encoded-len" ) ) ]
223+ impl < T > MaybeMaxEncodedLen for T { }
224+
209225/// Trait that tells you if a given type can be encoded/decoded in a compact way.
210226pub trait HasCompact : Sized {
211227 /// The compact type; this can be
212- type Type : for < ' a > EncodeAsRef < ' a , Self > + Decode + From < Self > + Into < Self > ;
228+ type Type : for < ' a > EncodeAsRef < ' a , Self > + Decode + From < Self > + Into < Self > + MaybeMaxEncodedLen ;
213229}
214230
215231impl < ' a , T : ' a > EncodeAsRef < ' a , T > for Compact < T > where CompactRef < ' a , T > : Encode + From < & ' a T > {
216232 type RefType = CompactRef < ' a , T > ;
217233}
218234
235+ #[ cfg( feature = "max-encoded-len" ) ]
236+ impl < T > MaxEncodedLen for Compact < T > where T : CompactAs , Compact < T :: As > : MaxEncodedLen , Compact < T > : Encode {
237+ fn max_encoded_len ( ) -> usize {
238+ Compact :: < T :: As > :: max_encoded_len ( )
239+ }
240+ }
241+
219242impl < T : ' static > HasCompact for T where
220- Compact < T > : for < ' a > EncodeAsRef < ' a , T > + Decode + From < Self > + Into < Self >
243+ Compact < T > : for < ' a > EncodeAsRef < ' a , T > + Decode + From < Self > + Into < Self > + MaybeMaxEncodedLen
221244{
222245 type Type = Compact < T > ;
223246}
0 commit comments