Convert primitive integer in generic const context

โš“ Rust    ๐Ÿ“… 2025-08-01    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 10      

surdeus

Warning

This post was published 124 days ago. The information described in this article may have changed.

I want to to something like this:

However, this gives me a weird compilation error:

   Compiling playground v0.0.1 (/playground)
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/lib.rs:1:12
  |
1 | #![feature(generic_const_exprs)]
  |            ^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
  = note: `#[warn(incomplete_features)]` on by default

error[E0207]: the const parameter `CAPACITY` is not constrained by the impl trait, self type, or predicates
  --> src/lib.rs:13:9
   |
13 | impl<T, const CAPACITY: u8> Prefixed<u8, ByteSizedVec<T, CAPACITY>> {
   |         ^^^^^^^^^^^^^^^^^^ unconstrained const parameter
   |
   = note: expressions using a const parameter must map each value to a distinct output value
   = note: proving the result of expressions other than the parameter are unique is not supported

For more information about this error, try `rustc --explain E0207`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib) due to 1 previous error; 1 warning emitted

I do not use Nightly too often, so I donโ€™t know whether this is related to the instability of the feature.

Is it possible to compile-time cast a u8 to a usize in a const generic in Rust?

I want this to ensure that the size of the data fits into a u8 at all times.

2 posts - 2 participants

Read full topic

๐Ÿท๏ธ Rust_feed