mod arithmetic;
mod bitwise;
#[cfg(test)]
mod comprehensive_tests;
mod index;
mod int_operand;
mod neg;
mod shift;
mod shift_amount;
#[allow(unused_imports)]
pub use int_operand::IntOperand;
#[allow(unused_imports)]
pub use shift_amount::ShiftAmount;
macro_rules! impl_binop_uuid_uuid {
($trait:ident, $method:ident, $assign_trait:ident, $assign_method:ident, $op:expr) => {
impl $trait for UUID {
type Output = Self;
#[inline]
fn $method(self, rhs: Self) -> Self::Output {
Self::from_u128(($op)(self.to_u128(), rhs.to_u128()))
}
}
impl $trait<&Self> for UUID {
type Output = Self;
#[inline]
fn $method(self, rhs: &Self) -> Self::Output {
<Self as $trait>::$method(self, *rhs)
}
}
impl $assign_trait for UUID {
#[inline]
fn $assign_method(&mut self, rhs: Self) {
*self = <Self as $trait>::$method(*self, rhs);
}
}
impl $assign_trait<&Self> for UUID {
#[inline]
fn $assign_method(&mut self, rhs: &Self) {
*self = <Self as $trait>::$method(*self, *rhs);
}
}
};
}
macro_rules! impl_ref_lhs_uuid {
($trait:ident, $method:ident) => {
impl $trait<UUID> for &UUID {
type Output = UUID;
#[inline]
fn $method(self, rhs: UUID) -> Self::Output {
<UUID as $trait>::$method(*self, rhs)
}
}
impl<'a, 'b> $trait<&'a UUID> for &'b UUID {
type Output = UUID;
#[inline]
fn $method(self, rhs: &'a UUID) -> Self::Output {
<UUID as $trait>::$method(*self, *rhs)
}
}
};
}
macro_rules! impl_uuid_op_int {
($trait:ident, $method:ident, $assign_trait:ident, $assign_method:ident, $op:expr) => {
impl<T: IntOperand> $trait<T> for UUID {
type Output = Self;
#[inline]
fn $method(self, rhs: T) -> Self::Output {
Self::from_u128(($op)(self.to_u128(), rhs.to_u128()))
}
}
impl<T: IntOperand> $trait<T> for &UUID {
type Output = UUID;
#[inline]
fn $method(self, rhs: T) -> Self::Output {
<UUID as $trait<T>>::$method(*self, rhs)
}
}
impl<T: IntOperand> $assign_trait<T> for UUID {
#[inline]
fn $assign_method(&mut self, rhs: T) {
*self = <UUID as $trait<T>>::$method(*self, rhs);
}
}
};
}
macro_rules! impl_int_op_uuid_commutative {
($trait:ident, $method:ident, $($t:ty),* $(,)?) => {
$(
impl $trait<UUID> for $t {
type Output = UUID;
#[inline]
fn $method(self, rhs: UUID) -> Self::Output {
<UUID as $trait<$t>>::$method(rhs, self)
}
}
impl $trait<&UUID> for $t {
type Output = UUID;
#[inline]
fn $method(self, rhs: &UUID) -> Self::Output {
<UUID as $trait<$t>>::$method(*rhs, self)
}
}
impl $trait<UUID> for &$t {
type Output = UUID;
#[inline]
fn $method(self, rhs: UUID) -> Self::Output {
<UUID as $trait<$t>>::$method(rhs, *self)
}
}
impl $trait<&UUID> for &$t {
type Output = UUID;
#[inline]
fn $method(self, rhs: &UUID) -> Self::Output {
<UUID as $trait<$t>>::$method(*rhs, *self)
}
}
)*
};
}
macro_rules! impl_int_op_uuid_noncommutative {
($trait:ident, $method:ident, $op:expr, $($t:ty),* $(,)?) => {
$(
impl $trait<UUID> for $t {
type Output = UUID;
#[inline]
fn $method(self, rhs: UUID) -> Self::Output {
UUID::from_u128(($op)(<$t as IntOperand>::to_u128(self), rhs.to_u128()))
}
}
impl $trait<&UUID> for $t {
type Output = UUID;
#[inline]
fn $method(self, rhs: &UUID) -> Self::Output {
<$t as $trait<UUID>>::$method(self, *rhs)
}
}
impl $trait<UUID> for &$t {
type Output = UUID;
#[inline]
fn $method(self, rhs: UUID) -> Self::Output {
<$t as $trait<UUID>>::$method(*self, rhs)
}
}
impl $trait<&UUID> for &$t {
type Output = UUID;
#[inline]
fn $method(self, rhs: &UUID) -> Self::Output {
<$t as $trait<UUID>>::$method(*self, *rhs)
}
}
)*
};
}
pub(crate) use impl_binop_uuid_uuid;
pub(crate) use impl_int_op_uuid_commutative;
pub(crate) use impl_int_op_uuid_noncommutative;
pub(crate) use impl_ref_lhs_uuid;
pub(crate) use impl_uuid_op_int;
#[cfg(test)]
mod tests {
use crate::UUID;
#[test]
fn to_u128_and_from_u128_roundtrip() {
let values = [0u128, 1, 42, u128::MAX, 0x0123_4567_89ab_cdef];
for &v in &values {
let uuid = UUID::from_u128(v);
assert_eq!(uuid.to_u128(), v, "roundtrip failed for {v}");
}
}
#[test]
fn to_u128_preserves_byte_order() {
let uuid = UUID::from_bytes([
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
0xee, 0xff,
]);
assert_eq!(uuid.to_u128(), 0x0011_2233_4455_6677_8899_aabb_ccdd_eeff);
}
#[test]
fn from_u128_preserves_byte_order() {
let uuid = UUID::from_u128(0x0011_2233_4455_6677_8899_aabb_ccdd_eeff);
assert_eq!(
*uuid.as_bytes(),
[
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
0xee, 0xff
]
);
}
}