Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider const panic if min > max #21

Open
dzmitry-lahoda opened this issue Sep 18, 2022 · 5 comments
Open

consider const panic if min > max #21

dzmitry-lahoda opened this issue Sep 18, 2022 · 5 comments

Comments

@dzmitry-lahoda
Copy link

// Assert<{ L > 0 }>: IsTrue,

that compiles for me:

#[allow(non_snake_case)]
pub trait RC5<Word, const WS: usize, const R: u8, const B: u8>
where
    Word: Sized
        + WrappingShl<Output = Word>
        + WrappingShr<Output = Word>
        + BitAnd<Output = Word>
        + Add<Output = Word>
        + BitOr<Word, Output = Word>
        + WrappingAdd
        + Copy
        + LeBytes<WS>
        + BitXor<Output = Word>
        + From<u8>
        + Zero,
{
    const W_BYTES: u8 = if std::mem::size_of::<Word>() > WS || Self::R < Self::B  {
        panic!("Word size must be WS")
    } else {
        WS as u8
    };
@greenhat
Copy link
Member

Thank you for this idea! I tried this on stable (1.63):

trait CheckBounds<const L: usize, const U: usize> {
    const WB: u8 = if Self::L > Self::U || Self::L == 0 {
        panic!("invalid bounds")
    } else {
        1
    };
}

impl<T, const L: usize, const U: usize> CheckBounds<L, U> for BoundedVec<T, L, U> {}

and got:

error[E0599]: no associated item named `L` found for type parameter `Self` in the current scope
  --> src/bounded_vec.rs:47:29
   |
47 |     const WB: u8 = if Self::L > Self::U || Self::L == 0 {
   |                             ^ associated item not found in `Self`

error[E0599]: no associated item named `U` found for type parameter `Self` in the current scope
  --> src/bounded_vec.rs:47:39
   |
47 |     const WB: u8 = if Self::L > Self::U || Self::L == 0 {
   |                                       ^ associated item not found in `Self`

error[E0599]: no associated item named `L` found for type parameter `Self` in the current scope
  --> src/bounded_vec.rs:47:50
   |
47 |     const WB: u8 = if Self::L > Self::U || Self::L == 0 {
   |                                                  ^ associated item not found in `Self`

Any ideas? Maybe you're have some features enabled that allow this.

@dzmitry-lahoda
Copy link
Author

@greenhat
Copy link
Member

Oh, it's #![feature(generic_const_exprs)] that is not available on stable yet. I'm keeping an eye on it and will implement the checks as soon as it gets into stable.

@dzmitry-lahoda
Copy link
Author

dzmitry-lahoda commented Sep 19, 2022

you may expose in toml
[features]
generic_const_exprs =[]

so

#[cfg(feature(generic_const_exprs))
#[feature(generic_const_exprs)]

so you can have both stable and unstable, unstable with future proof feature

@greenhat
Copy link
Member

Good idea! Thank you!

@greenhat greenhat reopened this Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants