#编译时 #排序 #id #const #static #unique-id #compiled

const-identify

在编译时生成唯一标识符

2 个版本

0.1.1 2023 年 9 月 28 日
0.1.0 2023 年 9 月 28 日

#2917Rust 模式

MIT 许可证

15KB
280 代码行

Const Identify

在编译时生成唯一标识符。

还包含数组切片类型来管理标识符集合。

使用方法

#[derive(ConstIdentify)]
pub struct Struct1;

#[derive(ConstIdentify)]
pub struct Struct2;

#[derive(ConstIdentify)]
pub struct Struct3;

// create an ordered id array
// these are arrays of ids that are pre sorted
const ORDERED: OrderedIdArray<2> = ordered_ids![Struct1, Struct1, Struct2];

// create a unique id array
// these are arrays of ids that are guarunteed to be unique
const UNIQUE: UniqueIdArray<3> = match unique_ids![Struct1, Struct2, Struct3] {
    Some(unique) => unique,
    // compile time panics can be bubbled up preventing compilation if uniqueness is not held
    None => panic!("Duplicate ids detected"),
}

// arrays may be compared at compile time using the provided methods
const COMPARE: std::cmp::Ordering = ORDERED.const_cmp_unique(&UNIQUE);

// arrays provide raw access to the underlying slices
const SORTED_SLICE: &'static [ConstId] = ORDERED.as_raw_slice();

// arrays may be converted to special slice types that carry the ordering/uniqueness guaruntee
const ORDERED_SLICE: OrderedIdSlice<'static> = OrderedIdSlice::from_arr(&ORDERED);
const UNIQUE_SLICE: UniqueIdSlice<'static> = UniqueIdSlice::from_arr(&UNIQUE);

MIT 许可证

依赖项

~280–730KB
~17K SLoC