#const #const-generics #assert

nightly const-assert

Assert struct for const generics

2 stable releases

1.0.1 Jul 31, 2023
1.0.0 Oct 3, 2022

#1780 in Rust patterns

Download history 734/week @ 2024-07-24 773/week @ 2024-07-31 1615/week @ 2024-08-07 1830/week @ 2024-08-14 1815/week @ 2024-08-21 1850/week @ 2024-08-28 1272/week @ 2024-09-04 1663/week @ 2024-09-11 2799/week @ 2024-09-18 2962/week @ 2024-09-25 2250/week @ 2024-10-02 2506/week @ 2024-10-09 2445/week @ 2024-10-16 3133/week @ 2024-10-23 2594/week @ 2024-10-30 2351/week @ 2024-11-06

11,018 downloads per month
Used in 7 crates (6 directly)

MIT license

3KB

License Cargo Documentation

Assert is used to create generic trait bounds:

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use const_assert::{Assert, IsTrue, IsFalse};

struct Buffer<const N: usize> {
  inner: [usize; N],
}

impl<const N: usize> Buffer<N>
where
  Assert<{ N == N.next_power_of_two() }>: IsTrue,
  Assert<{ N == 1 }>: IsFalse
{
  pub const fn new() -> Self {
      Buffer { inner: [0; N] }
  }
}

static BUFFER: Buffer<1024> = Buffer::new();

lib.rs:

Assert is used to create generic trait bounds:

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use const_assert::{Assert, IsTrue, IsFalse};

struct Buffer<const N: usize> {
  inner: [usize; N],
}

impl<const N: usize> Buffer<N>
where
  Assert<{ N == N.next_power_of_two() }>: IsTrue,
  Assert<{ N == 1 }>: IsFalse
{
  pub const fn new() -> Self {
      Buffer { inner: [0; N] }
  }
}

static BUFFER: Buffer<1024> = Buffer::new();

No runtime deps