I've seen various discussion along these lines before, and perhaps a crate for it already exists, but...
Proposal: a slice newtype/"smart pointer" type which has a const generic LENGTH: Range<usize>
and enforces a type-level invariant that the LENGTH
range contains
the length of the inner slice.
#[repr(transparent)]
pub struct RangeSlice<'a, T, const LENGTH: Range<usize>> {
inner: &'a [T]
}
Notably this type can't yet be written on stable Rust as it needs feature(adt_const_params)
but could be implemented as a nightly-only crate.
However, it seems like the sort of thing that would benefit with direct integration with the slice primitive type, although that can be worked around using extension traits.
Is there any current work happening along these lines, or perhaps a crate? Or failing either of those, does anyone else think something like this would eventually be beneficial in core
?