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>

source

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();
source

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]);
source

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);
source

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);
source

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

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);
source

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);
source

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());
source

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());
source

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"));
source

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"));
source

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);
source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator

source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns an iterator that allows to modify each value

source

pub fn split_last(&self) -> (&T, &[T])

Returns the last and all the rest of the elements

source

pub fn enumerated(self) -> BoundedVec<(usize, T), L, U>

Return a new BoundedVec with indices included

Trait Implementations§

source§

impl<T, const L: usize, const U: usize> AsMut<[T]> for BoundedVec<T, L, U>

source§

fn as_mut(&mut self) -> &mut [T]

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T, const L: usize, const U: usize> AsMut<Vec<T, Global>> for BoundedVec<T, L, U>

source§

fn as_mut(&mut self) -> &mut Vec<T>

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T, const L: usize, const U: usize> AsRef<[T]> for BoundedVec<T, L, U>

source§

fn as_ref(&self) -> &[T]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T, const L: usize, const U: usize> AsRef<Vec<T, Global>> for BoundedVec<T, L, U>

source§

fn as_ref(&self) -> &Vec<T>

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: Clone, const L: usize, const U: usize> Clone for BoundedVec<T, L, U>

source§

fn clone(&self) -> BoundedVec<T, L, U>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug, const L: usize, const U: usize> Debug for BoundedVec<T, L, U>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T, const L: usize, const U: usize> From<[T; L]> for BoundedVec<T, L, U>

source§

fn from(arr: [T; L]) -> Self

Converts to this type from the input type.
source§

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

Converts to this type from the input type.
source§

impl<T: Hash, const L: usize, const U: usize> Hash for BoundedVec<T, L, U>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a, T, const L: usize, const U: usize> IntoIterator for &'a BoundedVec<T, L, U>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T, const L: usize, const U: usize> IntoIterator for &'a mut BoundedVec<T, L, U>

§

type Item = &'a mut T

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T, const L: usize, const U: usize> IntoIterator for BoundedVec<T, L, U>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T, Global>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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 · source§

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
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T, const L: usize, const U: usize> TryFrom<Vec<T, Global>> for BoundedVec<T, L, U>

§

type Error = BoundedVecOutOfBounds

The type returned in the event of a conversion error.
source§

fn try_from(value: Vec<T>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: Eq, const L: usize, const U: usize> Eq for BoundedVec<T, L, U>

source§

impl<T, const L: usize, const U: usize> StructuralEq for BoundedVec<T, L, U>

source§

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.