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

Commit

Permalink
Fix one-by-off in BoundedSlice::try_from (#11600)
Browse files Browse the repository at this point in the history
  • Loading branch information
koute authored Jun 6, 2022
1 parent 3860bbb commit a0a40ad
Showing 1 changed file with 15 additions and 1 deletion.
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());
}

#[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());
}
}

0 comments on commit a0a40ad

Please sign in to comment.