Type Alias bounded_vec::NonEmptyVec

source ·
pub type NonEmptyVec<T> = BoundedVec<T, 1, { usize::MAX }>;
Expand description

A non-empty Vec with no effective upper-bound on its length

Aliased Type§

struct NonEmptyVec<T> { /* private fields */ }

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

source

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

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>> 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>> 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: 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<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>

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) -> Self
where Self: Sized,

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

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

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

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

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

impl<T: PartialEq, const L: usize, const U: usize> PartialEq 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 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>> 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> StructuralPartialEq for BoundedVec<T, L, U>