#assert #const #generics

nightly const-assert

常量泛型断言结构体

2 个稳定版本

1.0.1 2023年7月31日
1.0.0 2022年10月3日

2452Rust 模式

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 每月下载次数
4 crates 中使用

MIT 许可证

3KB

License Cargo Documentation

断言用于创建泛型特质约束

#![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:

断言用于创建泛型特质约束

#![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();

无运行时依赖