Struct bounded_vec::BoundedVec
source · pub struct BoundedVec<T, const L: usize, const U: usize> { /* private fields */ }
Expand description
Non-empty Vec bounded with minimal (L - lower bound) and maximal (U - upper bound) items quantity
Implementations§
source§impl<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 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]
pub fn as_slice(&self) -> &[T]
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
Trait Implementations§
source§impl<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>
source§fn clone(&self) -> BoundedVec<T, L, U>
fn clone(&self) -> BoundedVec<T, L, U>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<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>
source§fn from(v: BoundedVec<T, L, U>) -> Self
fn from(v: BoundedVec<T, L, U>) -> Self
source§impl<'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>
source§impl<'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>
source§impl<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>
source§impl<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>
source§fn cmp(&self, other: &BoundedVec<T, L, U>) -> Ordering
fn cmp(&self, other: &BoundedVec<T, L, U>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§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>
source§fn eq(&self, other: &BoundedVec<T, L, U>) -> bool
fn eq(&self, other: &BoundedVec<T, L, U>) -> bool
self
and other
values to be equal, and is used
by ==
.source§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>
source§fn partial_cmp(&self, other: &BoundedVec<T, L, U>) -> Option<Ordering>
fn partial_cmp(&self, other: &BoundedVec<T, L, U>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more