Struct bounded_vec::BoundedVec [−][src]
Expand description
Non-empty Vec bounded with minimal (L - lower bound) and maximal (U - upper bound) items quantity
Implementations
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();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]);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);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);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]);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);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);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());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());pub 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"));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>,
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"));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);Returns an iterator that allows to modify each value
Return a new BoundedVec with indices included
Trait Implementations
Performs the conversion.
impl<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>
This method tests for self and other values to be equal, and is used
by ==. Read more
This method tests for !=.
impl<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>
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
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
Mutably borrows from an owned value. Read more