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§

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

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

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

Returns an iterator that allows to modify each value

Returns the last and all the rest of the elements

Return a new BoundedVec with indices included

Trait Implementations§

Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
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
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.