Skip to content

Instantly share code, notes, and snippets.

@JasonkayZK
Created November 21, 2022 07:57
Show Gist options
  • Save JasonkayZK/0dd8be1b08323dd2774d8a24cb049c21 to your computer and use it in GitHub Desktop.
Save JasonkayZK/0dd8be1b08323dd2774d8a24cb049c21 to your computer and use it in GitHub Desktop.
Rust code colloections
// 目前只能在nightly版本下使用
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
fn something<T>(val: T)
where
Assert<{ core::mem::size_of::<T>() < 768 }>: IsTrue,
// ^-----------------------------^ 这里是一个 const 表达式,换成其它的 const 表达式也可以
{
//
}
fn main() {
something([0u8; 0]); // ok
something([0u8; 512]); // ok
something([0u8; 1024]); // 编译错误,数组长度是1024字节,超过了768字节的参数长度限制
}
// ---
pub enum Assert<const CHECK: bool> {
//
}
pub trait IsTrue {
//
}
impl IsTrue for Assert<true> {
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment