Skip to main content

apple_plist/
uid.rs

1//! The `CF$UID` reference type used by Apple keyed archives.
2
3/// A `UID` value, as found in `NSKeyedArchiver` / `CF$UID` property lists.
4///
5/// In binary plists this is a distinct object kind. In XML and OpenStep it is
6/// encoded as the magic single-key dictionary `{ "CF$UID": <integer> }`.
7/// Construct one with [`From<u64>`]; read it back with [`Uid::get`] or
8/// [`From<Uid>`] for `u64`.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Uid(u64);
11
12impl Uid {
13    /// Returns the underlying integer value.
14    ///
15    /// # Examples
16    ///
17    /// ```
18    /// use apple_plist::Uid;
19    ///
20    /// assert_eq!(Uid::from(42).get(), 42);
21    /// ```
22    #[must_use]
23    pub const fn get(self) -> u64 {
24        self.0
25    }
26}
27
28impl From<u64> for Uid {
29    fn from(value: u64) -> Self {
30        Self(value)
31    }
32}
33
34impl From<Uid> for u64 {
35    fn from(uid: Uid) -> Self {
36        uid.0
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn conversions_round_trip() {
46        let uid = Uid::from(u64::MAX);
47        assert_eq!(uid.get(), u64::MAX);
48        assert_eq!(u64::from(uid), u64::MAX);
49        assert_eq!(Uid::from(0).get(), 0);
50    }
51
52    #[test]
53    fn ordering_and_equality_follow_the_value() {
54        assert!(Uid::from(1) < Uid::from(2));
55        assert_eq!(Uid::from(7), Uid::from(7));
56        assert_ne!(Uid::from(7), Uid::from(8));
57    }
58}