unique_uuid/
lib.rs

1//! A type-safe UUID-based tagging system for Rust.
2//!
3//! This crate provides a way to associate unique, stable identifiers with types
4//! across different platforms and compilation units. Unlike [`std::any::TypeId`],
5//! these identifiers remain stable across different compilations and platforms.
6//!
7//! # Features
8//!
9//! - Cross-platform stable type identification
10//! - Serialization support via the `serde` feature
11//! - Type-safe wrapper around UUIDs
12//!
13//! # Example
14//!
15//! ```rust
16//! use unique_uuid::{UniqueTypeTag, UniqueTag};
17//!
18//! #[derive(UniqueTypeTag)]
19//! struct MyType;
20//!
21//! let tag = MyType::TYPE_TAG;
22//! ```
23
24pub extern crate uuid;
25pub use unique_uuid_derive::*;
26use uuid::Uuid;
27
28/// A type-safe wrapper around [`uuid::Uuid`] for storing unique type identifiers.
29///
30/// `UniqueTag` provides a way to associate stable, unique identifiers with types.
31/// It uses a UUID internally to ensure global uniqueness while maintaining
32/// type safety through its wrapper type.
33///
34/// # Representation
35///
36/// The struct is marked with `#[repr(transparent)]` to ensure it has the same
37/// memory layout as the underlying UUID, making it efficient to store and pass.
38///
39/// # Serialization
40///
41/// When the `serde` feature is enabled, this type implements `Serialize` and
42/// `Deserialize` for compatibility with serde-based serialization formats.
43#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
45#[repr(transparent)]
46pub struct UniqueTag(pub Uuid);
47
48/// A trait for types that have an associated unique identifier.
49///
50/// This trait provides a stable way to identify types across different
51/// compilations and platforms. Unlike [`std::any::TypeId`], the identifiers
52/// generated by this trait remain constant across different builds and platforms.
53///
54/// # Examples
55///
56/// ```rust
57/// use unique_uuid::{UniqueTypeTag, UniqueTag};
58///
59/// #[derive(UniqueTypeTag)]
60/// struct MyStruct;
61///
62/// assert_eq!(MyStruct::TYPE_TAG, MyStruct.type_id());
63/// ```
64///
65/// # Deriving
66///
67/// This trait can be automatically derived using the `#[derive(UniqueTypeTag)]`
68/// macro provided by this crate.
69pub trait UniqueTypeTag {
70    /// The unique tag associated with the type.
71    const TYPE_TAG: UniqueTag;
72
73    /// Returns the type id number.
74    fn type_id(&self) -> UniqueTag {
75        Self::TYPE_TAG
76    }
77}