unique-uuid 0.1.0

A library to generate unique UUIDs.
Documentation
//! A type-safe UUID-based tagging system for Rust.
//!
//! This crate provides a way to associate unique, stable identifiers with types
//! across different platforms and compilation units. Unlike [`std::any::TypeId`],
//! these identifiers remain stable across different compilations and platforms.
//!
//! # Features
//!
//! - Cross-platform stable type identification
//! - Serialization support via the `serde` feature
//! - Type-safe wrapper around UUIDs
//!
//! # Example
//!
//! ```rust
//! use unique_uuid::{UniqueTypeTag, UniqueTag};
//!
//! #[derive(UniqueTypeTag)]
//! struct MyType;
//!
//! let tag = MyType::TYPE_TAG;
//! ```

pub extern crate uuid;
pub use unique_uuid_derive::*;
use uuid::Uuid;

/// A type-safe wrapper around [`uuid::Uuid`] for storing unique type identifiers.
///
/// `UniqueTag` provides a way to associate stable, unique identifiers with types.
/// It uses a UUID internally to ensure global uniqueness while maintaining
/// type safety through its wrapper type.
///
/// # Representation
///
/// The struct is marked with `#[repr(transparent)]` to ensure it has the same
/// memory layout as the underlying UUID, making it efficient to store and pass.
///
/// # Serialization
///
/// When the `serde` feature is enabled, this type implements `Serialize` and
/// `Deserialize` for compatibility with serde-based serialization formats.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
pub struct UniqueTag(pub Uuid);

/// A trait for types that have an associated unique identifier.
///
/// This trait provides a stable way to identify types across different
/// compilations and platforms. Unlike [`std::any::TypeId`], the identifiers
/// generated by this trait remain constant across different builds and platforms.
///
/// # Examples
///
/// ```rust
/// use unique_uuid::{UniqueTypeTag, UniqueTag};
///
/// #[derive(UniqueTypeTag)]
/// struct MyStruct;
///
/// assert_eq!(MyStruct::TYPE_TAG, MyStruct.type_id());
/// ```
///
/// # Deriving
///
/// This trait can be automatically derived using the `#[derive(UniqueTypeTag)]`
/// macro provided by this crate.
pub trait UniqueTypeTag {
    /// The unique tag associated with the type.
    const TYPE_TAG: UniqueTag;

    /// Returns the type id number.
    fn type_id(&self) -> UniqueTag {
        Self::TYPE_TAG
    }
}