Struct bounded_vec::BoundedVec
source · [−]Expand description
Non-empty Vec bounded with minimal (L - lower bound) and maximal (U - upper bound) items quantity
Implementations
sourceimpl<T, const L: usize, const U: usize> BoundedVec<T, L, U>
impl<T, const L: usize, const U: usize> BoundedVec<T, L, U>
sourcepub fn from_vec(items: Vec<T>) -> Result<Self, BoundedVecOutOfBounds>
pub fn from_vec(items: Vec<T>) -> Result<Self, BoundedVecOutOfBounds>
Creates new BoundedVec or returns error if items count is out of bounds
Example
use bounded_vec::BoundedVec;
let data: BoundedVec<_, 2, 8> = BoundedVec::from_vec(vec![1u8, 2]).unwrap();sourcepub fn as_vec(&self) -> &Vec<T>
pub fn as_vec(&self) -> &Vec<T>
Returns a reference to underlying `Vec``
Example
use bounded_vec::BoundedVec;
use std::convert::TryInto;
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.as_vec(), &vec![1u8,2]);sourcepub fn to_vec(self) -> Vec<T>
pub fn to_vec(self) -> Vec<T>
Returns an underlying `Vec``
Example
use bounded_vec::BoundedVec;
use std::convert::TryInto;
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.to_vec(), vec![1u8,2]);sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the vector
Example
use bounded_vec::BoundedVec;
use std::convert::TryInto;
let data: BoundedVec<u8, 2, 4> = vec![1u8,2].try_into().unwrap();
assert_eq!(data.len(), 2);sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Always returns false (cannot be empty)
Example
use bounded_vec::BoundedVec;
use std::convert::TryInto;
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.is_empty(), false);sourcepub fn as_slice(&self) -> &[T]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
pub fn as_slice(&self) -> &[T]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
Extracts a slice containing the entire vector.
Example
use bounded_vec::BoundedVec;
use std::convert::TryInto;
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.as_slice(), &[1u8,2]);sourcepub fn first(&self) -> &T
pub fn first(&self) -> &T
Returns the first element of non-empty Vec
Example
use bounded_vec::BoundedVec;
use std::convert::TryInto;
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
assert_eq!(*data.first(), 1);sourcepub fn last(&self) -> &T
pub fn last(&self) -> &T
Returns the last element of non-empty Vec
Example
use bounded_vec::BoundedVec;
use std::convert::TryInto;
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
assert_eq!(*data.last(), 2);sourcepub fn mapped<F, N>(self, map_fn: F) -> BoundedVec<N, L, U> where
F: FnMut(T) -> N,
pub fn mapped<F, N>(self, map_fn: F) -> BoundedVec<N, L, U> where
F: FnMut(T) -> N,
Create a new BoundedVec by consuming self and mapping each element.
This is useful as it keeps the knowledge that the length is >= U, <= L,
even through the old BoundedVec is consumed and turned into an iterator.
Example
use bounded_vec::BoundedVec;
let data: BoundedVec<u8, 2, 8> = [1u8,2].into();
let data = data.mapped(|x|x*2);
assert_eq!(data, [2u8,4].into());sourcepub fn mapped_ref<F, N>(&self, map_fn: F) -> BoundedVec<N, L, U> where
F: FnMut(&T) -> N,
pub fn mapped_ref<F, N>(&self, map_fn: F) -> BoundedVec<N, L, U> where
F: FnMut(&T) -> N,
Create a new BoundedVec by mapping references to the elements of self
This is useful as it keeps the knowledge that the length is >= U, <= L,
will still hold for new BoundedVec
Example
use bounded_vec::BoundedVec;
let data: BoundedVec<u8, 2, 8> = [1u8,2].into();
let data = data.mapped_ref(|x|x*2);
assert_eq!(data, [2u8,4].into());sourcepub fn try_mapped<F, N, E>(self, map_fn: F) -> Result<BoundedVec<N, L, U>, E> where
F: FnMut(T) -> Result<N, E>,
pub fn try_mapped<F, N, E>(self, map_fn: F) -> Result<BoundedVec<N, L, U>, E> where
F: FnMut(T) -> Result<N, E>,
Create a new BoundedVec by consuming self and mapping each element
to a Result.
This is useful as it keeps the knowledge that the length is preserved
even through the old BoundedVec is consumed and turned into an iterator.
As this method consumes self, returning an error means that this
vec is dropped. I.e. this method behaves roughly like using a
chain of into_iter(), map, collect::<Result<Vec<N>,E>> and
then converting the Vec back to a Vec1.
Errors
Once any call to map_fn returns a error that error is directly
returned by this method.
Example
use bounded_vec::BoundedVec;
let data: BoundedVec<u8, 2, 8> = [1u8,2].into();
let data: Result<BoundedVec<u8, 2, 8>, _> = data.try_mapped(|x| Err("failed"));
assert_eq!(data, Err("failed"));sourcepub fn try_mapped_ref<F, N, E>(
&self,
map_fn: F
) -> Result<BoundedVec<N, L, U>, E> where
F: FnMut(&T) -> Result<N, E>,
pub fn try_mapped_ref<F, N, E>(
&self,
map_fn: F
) -> Result<BoundedVec<N, L, U>, E> where
F: FnMut(&T) -> Result<N, E>,
Create a new BoundedVec by mapping references of self elements
to a Result.
This is useful as it keeps the knowledge that the length is preserved
even through the old BoundedVec is consumed and turned into an iterator.
Errors
Once any call to map_fn returns a error that error is directly
returned by this method.
Example
use bounded_vec::BoundedVec;
let data: BoundedVec<u8, 2, 8> = [1u8,2].into();
let data: Result<BoundedVec<u8, 2, 8>, _> = data.try_mapped_ref(|x| Err("failed"));
assert_eq!(data, Err("failed"));sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference for an element at index or None if out of bounds
Example
use bounded_vec::BoundedVec;
let data: BoundedVec<u8, 2, 8> = [1u8,2].into();
let elem = *data.get(1).unwrap();
assert_eq!(elem, 2);sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Returns an iterator that allows to modify each value
sourcepub fn split_last(&self) -> (&T, &[T])
pub fn split_last(&self) -> (&T, &[T])
Returns the last and all the rest of the elements
sourcepub fn enumerated(self) -> BoundedVec<(usize, T), L, U>
pub fn enumerated(self) -> BoundedVec<(usize, T), L, U>
Return a new BoundedVec with indices included
sourcepub fn opt_empty_vec(
v: Vec<T>
) -> Result<Option<BoundedVec<T, L, U>>, BoundedVecOutOfBounds>
pub fn opt_empty_vec(
v: Vec<T>
) -> Result<Option<BoundedVec<T, L, U>>, BoundedVecOutOfBounds>
Return a Some(BoundedVec) or None if v is empty
Example
use bounded_vec::BoundedVec;
use bounded_vec::OptBoundedVecToVec;
let opt_bv_none = BoundedVec::<u8, 2, 8>::opt_empty_vec(vec![]).unwrap();
assert!(opt_bv_none.is_none());
assert_eq!(opt_bv_none.to_vec(), vec![]);
let opt_bv_some = BoundedVec::<u8, 2, 8>::opt_empty_vec(vec![0u8, 2]).unwrap();
assert!(opt_bv_some.is_some());
assert_eq!(opt_bv_some.to_vec(), vec![0u8, 2]);Trait Implementations
sourceimpl<T: Clone, const L: usize, const U: usize> Clone for BoundedVec<T, L, U>
impl<T: Clone, const L: usize, const U: usize> Clone for BoundedVec<T, L, U>
sourcefn clone(&self) -> BoundedVec<T, L, U>
fn clone(&self) -> BoundedVec<T, L, U>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source. Read more
sourceimpl<T, const L: usize, const U: usize> From<BoundedVec<T, L, U>> for Vec<T>
impl<T, const L: usize, const U: usize> From<BoundedVec<T, L, U>> for Vec<T>
sourcefn from(v: BoundedVec<T, L, U>) -> Self
fn from(v: BoundedVec<T, L, U>) -> Self
Converts to this type from the input type.
sourceimpl<'a, T, const L: usize, const U: usize> IntoIterator for &'a BoundedVec<T, L, U>
impl<'a, T, const L: usize, const U: usize> IntoIterator for &'a BoundedVec<T, L, U>
sourceimpl<'a, T, const L: usize, const U: usize> IntoIterator for &'a mut BoundedVec<T, L, U>
impl<'a, T, const L: usize, const U: usize> IntoIterator for &'a mut BoundedVec<T, L, U>
sourceimpl<T, const L: usize, const U: usize> IntoIterator for BoundedVec<T, L, U>
impl<T, const L: usize, const U: usize> IntoIterator for BoundedVec<T, L, U>
sourceimpl<T: Ord, const L: usize, const U: usize> Ord for BoundedVec<T, L, U>
impl<T: Ord, const L: usize, const U: usize> Ord for BoundedVec<T, L, U>
sourcefn cmp(&self, other: &BoundedVec<T, L, U>) -> Ordering
fn cmp(&self, other: &BoundedVec<T, L, U>) -> Ordering
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<T: PartialEq, const L: usize, const U: usize> PartialEq<BoundedVec<T, L, U>> for BoundedVec<T, L, U>
impl<T: PartialEq, const L: usize, const U: usize> PartialEq<BoundedVec<T, L, U>> for BoundedVec<T, L, U>
sourcefn eq(&self, other: &BoundedVec<T, L, U>) -> bool
fn eq(&self, other: &BoundedVec<T, L, U>) -> bool
This method tests for self and other values to be equal, and is used
by ==. Read more
sourcefn ne(&self, other: &BoundedVec<T, L, U>) -> bool
fn ne(&self, other: &BoundedVec<T, L, U>) -> bool
This method tests for !=.
sourceimpl<T: PartialOrd, const L: usize, const U: usize> PartialOrd<BoundedVec<T, L, U>> for BoundedVec<T, L, U>
impl<T: PartialOrd, const L: usize, const U: usize> PartialOrd<BoundedVec<T, L, U>> for BoundedVec<T, L, U>
sourcefn partial_cmp(&self, other: &BoundedVec<T, L, U>) -> Option<Ordering>
fn partial_cmp(&self, other: &BoundedVec<T, L, U>) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
impl<T: Eq, const L: usize, const U: usize> Eq for BoundedVec<T, L, U>
impl<T, const L: usize, const U: usize> StructuralEq for BoundedVec<T, L, U>
impl<T, const L: usize, const U: usize> StructuralPartialEq for BoundedVec<T, L, U>
Auto Trait Implementations
impl<T, const L: usize, const U: usize> RefUnwindSafe for BoundedVec<T, L, U> where
T: RefUnwindSafe,
impl<T, const L: usize, const U: usize> Send for BoundedVec<T, L, U> where
T: Send,
impl<T, const L: usize, const U: usize> Sync for BoundedVec<T, L, U> where
T: Sync,
impl<T, const L: usize, const U: usize> Unpin for BoundedVec<T, L, U> where
T: Unpin,
impl<T, const L: usize, const U: usize> UnwindSafe for BoundedVec<T, L, U> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more