Add DefensiveTruncateFrom (#12515)

* Add DefensiveTruncateFrom

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Map_err in preimage

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Map_err in beefy

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Make test dependant in debug-assertions

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: parity-processbot <>
This commit is contained in:
Oliver Tale-Yazdi
2022-10-20 23:06:12 +02:00
committed by GitHub
parent 92d2977292
commit 48a02bb056
5 changed files with 168 additions and 7 deletions
@@ -47,6 +47,12 @@ pub struct BoundedVec<T, S>(
#[cfg_attr(feature = "std", serde(skip_serializing))] PhantomData<S>,
);
/// Create an object through truncation.
pub trait TruncateFrom<T> {
/// Create an object through truncation.
fn truncate_from(unbound: T) -> Self;
}
#[cfg(feature = "std")]
impl<'de, T, S: Get<u32>> Deserialize<'de> for BoundedVec<T, S>
where
@@ -234,12 +240,12 @@ impl<'a, T: Ord, Bound: Get<u32>> Ord for BoundedSlice<'a, T, Bound> {
}
impl<'a, T, S: Get<u32>> TryFrom<&'a [T]> for BoundedSlice<'a, T, S> {
type Error = ();
type Error = &'a [T];
fn try_from(t: &'a [T]) -> Result<Self, Self::Error> {
if t.len() <= S::get() as usize {
Ok(BoundedSlice(t, PhantomData))
} else {
Err(())
Err(t)
}
}
}
@@ -250,12 +256,28 @@ impl<'a, T, S> From<BoundedSlice<'a, T, S>> for &'a [T] {
}
}
impl<'a, T, S: Get<u32>> TruncateFrom<&'a [T]> for BoundedSlice<'a, T, S> {
fn truncate_from(unbound: &'a [T]) -> Self {
BoundedSlice::<T, S>::truncate_from(unbound)
}
}
impl<'a, T, S> Clone for BoundedSlice<'a, T, S> {
fn clone(&self) -> Self {
BoundedSlice(self.0, PhantomData)
}
}
impl<'a, T, S> sp_std::fmt::Debug for BoundedSlice<'a, T, S>
where
&'a [T]: sp_std::fmt::Debug,
S: Get<u32>,
{
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
f.debug_tuple("BoundedSlice").field(&self.0).field(&S::get()).finish()
}
}
// Since a reference `&T` is always `Copy`, so is `BoundedSlice<'a, T, S>`.
impl<'a, T, S> Copy for BoundedSlice<'a, T, S> {}
@@ -692,6 +714,12 @@ impl<T, S: Get<u32>> TryFrom<Vec<T>> for BoundedVec<T, S> {
}
}
impl<T, S: Get<u32>> TruncateFrom<Vec<T>> for BoundedVec<T, S> {
fn truncate_from(unbound: Vec<T>) -> Self {
BoundedVec::<T, S>::truncate_from(unbound)
}
}
// It is okay to give a non-mutable reference of the inner vec to anyone.
impl<T, S> AsRef<Vec<T>> for BoundedVec<T, S> {
fn as_ref(&self) -> &Vec<T> {
@@ -809,6 +837,12 @@ where
}
}
impl<'a, T: PartialEq, S: Get<u32>> PartialEq<&'a [T]> for BoundedSlice<'a, T, S> {
fn eq(&self, other: &&'a [T]) -> bool {
&self.0 == other
}
}
impl<T: PartialEq, S: Get<u32>> PartialEq<Vec<T>> for BoundedVec<T, S> {
fn eq(&self, other: &Vec<T>) -> bool {
&self.0 == other
@@ -1219,6 +1253,18 @@ pub mod test {
assert!(b2.is_err());
}
#[test]
fn bounded_vec_debug_works() {
let bound = BoundedVec::<u32, ConstU32<5>>::truncate_from(vec![1, 2, 3]);
assert_eq!(format!("{:?}", bound), "BoundedVec([1, 2, 3], 5)");
}
#[test]
fn bounded_slice_debug_works() {
let bound = BoundedSlice::<u32, ConstU32<5>>::truncate_from(&[1, 2, 3]);
assert_eq!(format!("{:?}", bound), "BoundedSlice([1, 2, 3], 5)");
}
#[test]
fn bounded_vec_sort_by_key_works() {
let mut v: BoundedVec<i32, ConstU32<5>> = bounded_vec![-5, 4, 1, -3, 2];
@@ -1226,4 +1272,27 @@ pub mod test {
v.sort_by_key(|k| k.abs());
assert_eq!(v, vec![1, 2, -3, 4, -5]);
}
#[test]
fn bounded_vec_truncate_from_works() {
let unbound = vec![1, 2, 3, 4, 5];
let bound = BoundedVec::<u32, ConstU32<3>>::truncate_from(unbound.clone());
assert_eq!(bound, vec![1, 2, 3]);
}
#[test]
fn bounded_slice_truncate_from_works() {
let unbound = [1, 2, 3, 4, 5];
let bound = BoundedSlice::<u32, ConstU32<3>>::truncate_from(&unbound);
assert_eq!(bound, &[1, 2, 3][..]);
}
#[test]
fn bounded_slice_partialeq_slice_works() {
let unbound = [1, 2, 3];
let bound = BoundedSlice::<u32, ConstU32<3>>::truncate_from(&unbound);
assert_eq!(bound, &unbound[..]);
assert!(bound == &unbound[..]);
}
}