2

A clear explanation will be appreciated as to why Decode trait is not implemented while encode is. What are the design reason for this?

1 Answer 1

2

What? It is definetly decodable. Code from here:

impl<T: Decode, S: Get<u32>> Decode for BoundedVec<T, S> {
    fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
        let inner = Vec::<T>::decode(input)?;
        if inner.len() > S::get() as usize {
            return Err("BoundedVec exceeds its limit".into())
        }
        Ok(Self(inner, PhantomData))
    }

    fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
        Vec::<T>::skip(input)
    }
}

and a small test:

#[test]
fn encode_decode_works() {
    let bounded: BoundedVec<u32, ConstU32<7>> = bounded_vec![1, 2, 3, 4, 5, 6];
    let encoded = bounded.encode();
    let decoded: BoundedVec<u32, ConstU32<7>> = Decode::decode(&mut &encoded[..]).unwrap();

    assert_eq!(bounded, decoded);
}
1
  • I Guess not the trait Decode is not implemented for sp_runtime::BoundedVec<<T as frame_system::Config>::AccountId, u32>
    – not 0x12
    Dec 20, 2022 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.