# Const generic \`RangeSlice\`

**URL:** <https://internals.rust-lang.org/t/const-generic-rangeslice/17277>\
**Category:** language design\
**Created:** [August 26, 2022, 11:45pm UTC](https://internals.rust-lang.org/t/const-generic-rangeslice/17277 "2022-08-26T23:45:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [August 26, 2022, 11:45pm UTC](https://internals.rust-lang.org/t/const-generic-rangeslice/17277/1 "2022-08-26T23:45:54Z")

</div>

I've seen various [discussion along these lines before](https://internals.rust-lang.org/t/const-slicing/12279), 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.

```rust
#[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)`](https://github.com/rust-lang/rust/issues/95174) 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`?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [August 27, 2022, 11:55pm UTC](https://internals.rust-lang.org/t/const-generic-rangeslice/17277/2 "2022-08-27T23:55:58Z")

</div>

The minimum "at least as long as" is clearly beneficial. What benefit would a maximum length provide?

(As a primitive type this'd probably be spelled `&[T; MIN..MAX]`.)

---

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [August 28, 2022, 8:55pm UTC](https://internals.rust-lang.org/t/const-generic-rangeslice/17277/3 "2022-08-28T20:55:35Z")

</div>

> [@CAD97](#):
>
> What benefit would a maximum length provide?

These sorts of ranged inputs with a minimum and maximum size are frequently found in cryptography, where various algorithmic inputs can be variable-sized but with a specified minimum and maximum.

Here's a specific example:

[https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md](https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md)

> - The length in bytes of the salt is between 8 and 48 bytes
> - The hash output is encoded in B64. Its length shall be between 12 and 64 bytes

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 26, 2022, 8:56pm UTC](https://internals.rust-lang.org/t/const-generic-rangeslice/17277/4 "2022-11-26T20:56:36Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
