#assert #const #generics

nightly const-assert

Assert struct for const generics

2 stable releases

1.0.1 Jul 31, 2023
1.0.0 Oct 3, 2022

#2452 in Rust patterns

Download history 498/week @ 2024-03-14 357/week @ 2024-03-21 347/week @ 2024-03-28 455/week @ 2024-04-04 363/week @ 2024-04-11 342/week @ 2024-04-18 411/week @ 2024-04-25 440/week @ 2024-05-02 341/week @ 2024-05-09 305/week @ 2024-05-16 428/week @ 2024-05-23 521/week @ 2024-05-30 497/week @ 2024-06-06 471/week @ 2024-06-13 456/week @ 2024-06-20 441/week @ 2024-06-27

1,938 downloads per month
Used in 4 crates

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