Skip to content
Snippets Groups Projects
Commit 03ab76e9 authored by Xiliang Chen's avatar Xiliang Chen Committed by GitHub
Browse files

add sort & try_append to bounded_vec (#11196)

parent 641a43a0
Branches
No related merge requests found
...@@ -129,6 +129,16 @@ impl<T, S> BoundedVec<T, S> { ...@@ -129,6 +129,16 @@ impl<T, S> BoundedVec<T, S> {
self.0.sort_by(compare) self.0.sort_by(compare)
} }
/// Exactly the same semantics as [`slice::sort`].
///
/// This is safe since sorting cannot change the number of elements in the vector.
pub fn sort(&mut self)
where
T: sp_std::cmp::Ord,
{
self.0.sort()
}
/// Exactly the same semantics as `Vec::remove`. /// Exactly the same semantics as `Vec::remove`.
/// ///
/// # Panics /// # Panics
...@@ -374,6 +384,17 @@ impl<T, S: Get<u32>> BoundedVec<T, S> { ...@@ -374,6 +384,17 @@ impl<T, S: Get<u32>> BoundedVec<T, S> {
} }
} }
/// Exactly the same semantics as [`Vec::append`], but returns an error and does nothing if the
/// length of the outcome is larger than the bound.
pub fn try_append(&mut self, other: &mut Vec<T>) -> Result<(), ()> {
if other.len().saturating_add(self.len()) <= Self::bound() {
self.0.append(other);
Ok(())
} else {
Err(())
}
}
/// Consumes self and mutates self via the given `mutate` function. /// Consumes self and mutates self via the given `mutate` function.
/// ///
/// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is /// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment