Expand description
This crate provides interop between the arrayvec and generic_array crates, allowing you to
use generic_array’s GenericArray as the backing storage for the data structures in
arrayvec. This lets you have vector and string types that store their contents inline, with a
capacity that can be referred to in generic code.
§Usage
This crate exposes the type aliases GenericArrayVecs and GenericArrayString, which are
aliases of datatypes in the arrayvec crate. See the ArrayVec and ArrayString docs
to learn about their functionality. Each one also has a corresponding extension trait
GenericArrayVecExt and GenericArrayStringExt that provide additional constructors and
conversions.
§Example
use generic_arrayvec::arrayvec::Array;
use generic_arrayvec::typenum::{Prod, U2, U4};
use generic_arrayvec::{ArrayvecStorage, Capacity, GenericArrayVec};
use std::ops::Mul;
fn main() {
// Create a vector of `u8`s with a capacity of 4.
let mut arr = GenericArrayVec::<u8, U4>::new();
assert_eq!(arr.capacity(), 4);
// Add some elements to it.
arr.push(1);
arr.push(2);
arr.push(3);
assert_eq!(&arr[..], &[1, 2, 3]);
// To demonstrate generic bounds, we call our `double()` function, which is defined below.
// This function returns a new vector with double the capacity of the input vector.
// The new vector contains two copies of the input vector's items.
let doubled = double(&arr);
assert_eq!(&doubled[..], &[1, 2, 3, 1, 2, 3]);
assert_eq!(doubled.capacity(), 8);
}
fn double<N>(original: &GenericArrayVec<u8, N>) -> GenericArrayVec<u8, Prod<N, U2>>
where
// Boilerplate bounds for the input array.
N: Capacity<u8>,
ArrayvecStorage<u8, N>: Array<Item = u8>,
// Boilerplate bounds for the output array. Note it's the same as above, but
// `N` -> `Prod<N, U2>`.
Prod<N, U2>: Capacity<u8>,
ArrayvecStorage<u8, Prod<N, U2>>: Array<Item = u8>,
N: Mul<U2>,
{
let mut new = GenericArrayVec::<u8, Prod<N, U2>>::new();
// These `unwrap()`s can never fail.
new.try_extend_from_slice(original.as_slice()).unwrap();
new.try_extend_from_slice(original.as_slice()).unwrap();
new
}§where bound boilerplate
When working with a GenericArrayVec<T, N> where T and/or N are not concrete types, you
will need to always include certain bounds in your where clauses, or you will get a compile
error. This dummy function shows how to specify them:
use generic_arrayvec::arrayvec::Array;
use generic_arrayvec::{ArrayvecStorage, Capacity, GenericArrayVec};
fn f<T, N>(_arr: GenericArrayVec<T, N>)
where
N: Capacity<T>,
ArrayvecStorage<T, N>: Array<Item = T>,
{
}And this is how you specify them for GenericArrayString<N>:
use generic_arrayvec::arrayvec::Array;
use generic_arrayvec::{ArrayvecStorage, Capacity, GenericArrayString};
fn f<N>(_arr: GenericArrayString<N>)
where
N: Capacity<u8>,
N::ArrayType: Copy,
ArrayvecStorage<u8, N>: Array<Item = u8>,
{
}Re-exports§
pub use arrayvec;pub use generic_array;pub use generic_array::typenum;
Modules§
- plumbing
- Low-level implementation details you shouldn’t need to touch.
Traits§
- Capacity
- A trait implemented by
typenum’s unsigned integers, which lets them be used to define the capacity ofGenericArrayVec/GenericArrayString. - Generic
Array String Ext - Extension trait for
GenericArrayString. - Generic
Array VecExt - Extension trait for
GenericArrayVec.
Type Aliases§
- Arrayvec
Storage - A wrapper around a
GenericArraythat implements theArraytrait from the arrayvec crate, allowing it to be used as the backing store forArrayVecandArrayString. - Generic
Array String - A
GenericArray-backedArrayString. - Generic
Array Vec - A
GenericArray-backedArrayVec.