Struct bounded_vec::BoundedVec[][src]

pub struct BoundedVec<T, const L: usize, const U: usize> { /* fields omitted */ }
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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

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 tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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.