Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Fix one-by-off in BoundedSlice::try_from #11600

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion frame/support/src/storage/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<T: Ord, Bound: Get<u32>> Ord for BoundedVec<T, Bound> {
impl<'a, T, S: Get<u32>> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> {
type Error = ();
fn try_from(t: &'a [T]) -> Result<Self, Self::Error> {
if t.len() < S::get() as usize {
if t.len() <= S::get() as usize {
Ok(BoundedSlice(t, PhantomData))
} else {
Err(())
Expand Down Expand Up @@ -1007,4 +1007,18 @@ pub mod test {
_ => unreachable!("deserializer must raise error"),
}
}

#[test]
fn bounded_vec_try_from_works() {
assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0]).is_ok());
assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0, 1]).is_ok());
assert!(BoundedVec::<u32, ConstU32<2>>::try_from(vec![0, 1, 2]).is_err());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could additionally test that the returned value is correct, with the into() for &[T].

}

#[test]
fn bounded_slice_try_from_works() {
assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0][..]).is_ok());
assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0, 1][..]).is_ok());
assert!(BoundedSlice::<u32, ConstU32<2>>::try_from(&[0, 1, 2][..]).is_err());
}
}