uuid-like 0.2.0

Helpers for UUID-like types
Documentation
use std::{
    fmt::{Debug, Display},
    hash::Hash,
};

use uuid::Version;

mod wrapper;

#[doc(hidden)]
pub use uuid::Uuid;

/// Common methods for UUID-like types.
///
/// Feel free to use [`wrapper!`] to generate a UUID wrapper implementing this
/// trait.
pub trait UuidLike:
    Clone + Copy + Debug + Display + PartialEq + Eq + PartialOrd + Ord + Hash + Send + Sync + 'static
{
    /// Generates a new random UUID-like value.
    fn random() -> Self;

    /// Returns the UUID-like value represented by the given bytes.
    ///
    /// See [`Uuid::from_bytes()`].
    fn from_bytes(bytes: [u8; 16]) -> Self;

    /// Returns the UUID-like value represented by the given 128-bit value.
    ///
    /// See [`Uuid::from_u128()`].
    #[inline]
    fn from_u128(value: u128) -> Self {
        let bytes = value.to_be_bytes();
        Self::from_bytes(bytes)
    }

    /// Returns the UUID-like value represented by the given pair of 64-bit values.
    ///
    /// See [`Uuid::from_u64_pair()`].
    #[inline]
    fn from_u64_pair(high: u64, low: u64) -> Self {
        let value = ((high as u128) << 64) | (low as u128);
        Self::from_u128(value)
    }

    /// Returns `true` if the UUID-like value is the nil UUID.
    ///
    /// See [`Uuid::is_nil()`].
    #[inline]
    fn is_nil(&self) -> bool {
        self.as_u128() == 0
    }

    /// Returns the UUID version number of the UUID-like value.
    ///
    /// See [`Uuid::get_version_num()`].
    #[inline]
    fn get_version_num(&self) -> usize {
        (self.as_bytes()[6] >> 4) as usize
    }

    /// Returns the UUID version of the UUID-like value.
    ///
    /// See [`Uuid::get_version()`].
    #[inline]
    fn get_version(&self) -> Option<Version> {
        match self.get_version_num() {
            0 if self.is_nil() => Some(Version::Nil),
            1 => Some(Version::Mac),
            2 => Some(Version::Dce),
            3 => Some(Version::Md5),
            4 => Some(Version::Random),
            5 => Some(Version::Sha1),
            6 => Some(Version::SortMac),
            7 => Some(Version::SortRand),
            8 => Some(Version::Custom),
            0xff => Some(Version::Max),
            _ => None,
        }
    }

    /// Returns the bytes representing the UUID-like value.
    ///
    /// See [`Uuid::as_bytes()`].
    fn as_bytes(&self) -> &[u8; 16];

    /// Returns a 128-bit value representing the UUID-like value.
    ///
    /// See [`Uuid::as_u128()`].
    #[inline]
    fn as_u128(&self) -> u128 {
        let bytes = self.as_bytes();
        u128::from_be_bytes(*bytes)
    }

    /// Returns a pair of 64-bit values representing the UUID-like value.
    ///
    /// See [`Uuid::as_u64_pair()`].
    #[inline]
    fn as_u64_pair(&self) -> (u64, u64) {
        let value = self.as_u128();
        ((value >> 64) as u64, value as u64)
    }

    /// Returns the bytes representing the UUID-like value, consuming the value.
    ///
    /// See [`Uuid::into_bytes()`].
    fn into_bytes(self) -> [u8; 16];
}