Module scuttlebutt::generic_array_length
source · [−]Expand description
Helper types to make using the
generic-array
easier.
We use the generic-array
crate, so that we can support situations like:
trait Foo {
const N: usize;
}
struct Blarg<F: Foo> {
contents: [u8; F::N],
}
While some of the original use cases for generic-array
can be accomplished since the
stabilization of min_const_generics
, other use cases (like the above) remain unstable (as of
Rust 1.65).
The generic_array
crate uses a type, instead of a constant, to represent the length of the
array. generic_array
exposes the [ArrayLength
] trait to denote a type which represents the
length of an array. Due to internal reasons, ArrayLength
is parametrized on the type of an
element of the array. For example, we would write code like:
trait MyBlockCipher {
type BlockSize: generic_array::ArrayLength<u8>;
}
fn foo<C: MyBlockCipher>(x: generic_array::GenericArray<u8, C::BlockSize>) {
let _ = x; // do something!
}
This code lets us use BlockSize
only to create arrays of u8
s. If you want to create an
array of any other size, you’re out of luck.
In Rust 1.65, Generic Associated Types were stabilized. This provides a solution for us to work around this issue, and let us specify array lengths which can be used with any element type.
Use this module as follows:
trait MyBlockCipher {
type BlockSize: AnyArrayLength;
}
fn foo<C: MyBlockCipher>(x: Arr<u8, C::BlockSize>) {
let _ = x; // do something!
}
Because we’ve used AnyArrayLength
, we can instantiate an array of length BlockSize
with
any type that we want! (And we couldn’t do that with the “normal” GenericArray
solution.)
fn blarg<C: MyBlockCipher>(x: Arr<(i32, String), C::BlockSize>) {
let _ = x; // do something!
}
Traits
Self
corresponds to an ArrayLength
over any typeType Definitions
GenericArray
] of length N
containing type T