unique_type_id/lib.rs
1//! A unique id generator for rust types.
2//!
3//! The crate provides a trait and a procedural macro. By deriving one, you implement the
4//! trait with `fn id() -> TypeId` static method. The type id is unique in the whole project.
5//!
6//! For examples, see the `tests` directory in the source tree.
7//!
8//! # Usage
9//!
10//! The example usage:
11//!
12//! ```rust
13//!fn check_id() {
14//! use unique_type_id::UniqueTypeId;
15//!
16//! // This code uses "types.toml" (the default) file for ids.
17//! #[derive(UniqueTypeId)]
18//! struct Test1;
19//! #[derive(UniqueTypeId)]
20//! struct Test2;
21//!
22//! assert_eq!(Test1::id().0, 1u64);
23//! assert_eq!(Test2::id().0, 2u64);
24//!}
25//! ```
26#![no_std]
27
28pub use unique_type_id_derive::*;
29
30/// A strong type for type id.
31#[repr(transparent)]
32#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
33pub struct TypeId<T>(pub T);
34
35/// A trait for providing a type id number.
36pub trait UniqueTypeId<T> {
37 const TYPE_ID: TypeId<T>;
38
39 /// Returns the type id number.
40 fn id() -> TypeId<T>;
41}