[−][src]Crate sized_vec
Type Level Sized Vectors
This crate provides a Vec<N, A>
type, which wraps the standard Vec<A>
and tracks its size N
at the type level.
Because the size is embedded in the type, we can do things like verifying at compile time that index lookups are within bounds.
let vec = svec![1, 2, 3]; // This index lookup won't compile, because index `U8` is outside // the vector's length of `U3`: assert_eq!(5, vec[U8::new()]);
let vec = svec![1, 2, 3]; // On the other hand, this lookup can be verified to be correct // by the type system: assert_eq!(3, vec[U2::new()]);
Limitations
If this looks too good to be true, it's because it comes with a number of
limitations: you won't be able to perform operations on the vector which
could leave it with a length that can't be known at compile time. This
includes Extend::extend()
and filtering operations like Vec::retain()
.
FromIterator::from_iter
is, notably, also not available, but you can use
Vec::try_from
as a replacement. Note that try_from
needs to be able to
infer the size of the resulting vector at compile time; there's no way to
construct a vector of arbitrary length.
let vec = svec![1, 2, 3, 4, 5]; let new_vec = Vec::try_from_iter(vec.into_iter().map(|i| i + 10)); assert_eq!(Some(svec![11, 12, 13, 14, 15]), new_vec);
Macros
svec |
Structs
Range | A type level range. |
Vec | A vector of length |
Traits
IsTrue |