pub struct BoundedVec<T, const L: usize, const U: usize, W = NonEmpty<L, U>> { /* private fields */ }Expand description
Non-empty Vec bounded with minimal (L - lower bound) and maximal (U - upper bound) items quantity.
§Type Parameters
W- witness type to prove vector ranges and shape of interface accordingly
Implementations§
Source§impl<T, const U: usize> BoundedVec<T, 0, U, Empty<U>>
impl<T, const U: usize> BoundedVec<T, 0, U, Empty<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
§Parameters
items- vector of items within bounds
§Errors
UpperBoundError- if `items`` len is more than U (upper bound)
§Example
use bounded_vec::BoundedVec;
use bounded_vec::witnesses;
let data: BoundedVec<_, 0, 8, witnesses::Empty<8>> =
BoundedVec::<_, 0, 8, witnesses::Empty<8>>::from_vec(vec![1u8, 2]).unwrap();Sourcepub fn first(&self) -> Option<&T>
pub fn first(&self) -> Option<&T>
Returns the first element of the vector, or None if it is empty
§Example
use bounded_vec::BoundedVec;
use bounded_vec::witnesses;
use std::convert::TryInto;
let data: BoundedVec<u8, 0, 8, witnesses::Empty<8>> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.first(), Some(&1u8));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the vector contains no elements
§Example
use bounded_vec::BoundedVec;
use bounded_vec::witnesses;
use std::convert::TryInto;
let data: BoundedVec<u8, 0, 8, witnesses::Empty<8>> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.is_empty(), false);Sourcepub fn last(&self) -> Option<&T>
pub fn last(&self) -> Option<&T>
Returns the last element of the vector, or None if it is empty
§Example
use bounded_vec::BoundedVec;
use bounded_vec::witnesses;
use std::convert::TryInto;
let data: BoundedVec<u8, 0, 8, witnesses::Empty<8>> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.last(), Some(&2u8));Source§impl<T, const L: usize, const U: usize, W> BoundedVec<T, L, U, W>
Methods which works for all witnesses
impl<T, const L: usize, const U: usize, W> BoundedVec<T, L, U, W>
Methods which works for all witnesses
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 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]);Source§impl<T, const L: usize, const U: usize> BoundedVec<T, L, U, NonEmpty<L, U>>
impl<T, const L: usize, const U: usize> BoundedVec<T, L, U, NonEmpty<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
§Parameters
items- vector of items within bounds
§Errors
LowerBoundError- if `items`` len is less than L (lower bound)UpperBoundError- if `items`` len is more than U (upper bound)
§Example
use bounded_vec::BoundedVec;
use bounded_vec::witnesses;
let data: BoundedVec<_, 2, 8, witnesses::NonEmpty<2, 8>> =
BoundedVec::<_, 2, 8, witnesses::NonEmpty<2, 8>>::from_vec(vec![1u8, 2]).unwrap();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 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, NonEmpty<L, U>>where
F: FnMut(T) -> N,
pub fn mapped<F, N>(self, map_fn: F) -> BoundedVec<N, L, U, NonEmpty<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, NonEmpty<L, U>>
pub fn mapped_ref<F, N>(&self, map_fn: F) -> BoundedVec<N, L, U, NonEmpty<L, U>>
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, NonEmpty<L, U>>, E>
pub fn try_mapped<F, N, E>( self, map_fn: F, ) -> Result<BoundedVec<N, L, U, NonEmpty<L, U>>, 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, NonEmpty<L, U>>, E>
pub fn try_mapped_ref<F, N, E>( &self, map_fn: F, ) -> Result<BoundedVec<N, L, U, NonEmpty<L, U>>, 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 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, NonEmpty<L, U>>
pub fn enumerated(self) -> BoundedVec<(usize, T), L, U, NonEmpty<L, U>>
Return a new BoundedVec with indices included
Sourcepub fn opt_empty_vec(
v: Vec<T>,
) -> Result<Option<BoundedVec<T, L, U, NonEmpty<L, U>>>, BoundedVecOutOfBounds>
pub fn opt_empty_vec( v: Vec<T>, ) -> Result<Option<BoundedVec<T, L, U, NonEmpty<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::<u8>::new());
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§
Source§impl<T: Clone, const L: usize, const U: usize, W: Clone> Clone for BoundedVec<T, L, U, W>
impl<T: Clone, const L: usize, const U: usize, W: Clone> Clone for BoundedVec<T, L, U, W>
Source§fn clone(&self) -> BoundedVec<T, L, U, W>
fn clone(&self) -> BoundedVec<T, L, U, W>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more