rust_macios/foundation/
ns_uuid.rs

1use std::ffi::c_uchar;
2
3use crate::{
4    object,
5    objective_c_runtime::{
6        macros::interface_impl,
7        msg_send, sel, sel_impl,
8        traits::{FromId, PNSObject},
9    },
10};
11
12use super::{NSComparisonResult, NSString};
13
14object! {
15    /// A universally unique value that can be used to identify types, interfaces, and other items.
16    unsafe pub struct NSUUID;
17}
18
19#[interface_impl(NSObject)]
20impl NSUUID {
21    /* Creating UUIDs
22     */
23
24    /// Create and returns a new UUID with RFC 4122 version 4 random bytes.
25    #[method]
26    pub fn uuid() -> Self
27    where
28        Self: Sized + FromId,
29    {
30        unsafe { Self::from_id(msg_send![Self::m_class(), UUID]) }
31    }
32
33    /// Initializes a new UUID with RFC 4122 version 4 random bytes.
34    #[method]
35    pub fn init(&mut self) -> Self
36    where
37        Self: Sized + FromId,
38    {
39        unsafe { Self::from_id(msg_send![self.m_self(), init]) }
40    }
41
42    /// Initializes a new UUID with the formatted string.
43    #[method]
44    pub fn init_with_uuid_string(&mut self, string: NSString) -> Self
45    where
46        Self: Sized + FromId,
47    {
48        unsafe { Self::from_id(msg_send![self.m_self(), initWithUUIDString: string]) }
49    }
50
51    /// Initializes a new UUID with the given bytes.
52    ///
53    /// # Arguments
54    ///
55    /// * bytes - Raw UUID bytes to use to create the UUID.
56    ///
57    /// # Returns
58    ///
59    /// A new UUID object.
60    #[method]
61    pub fn init_with_uuid_bytes(&mut self, bytes: *const c_uchar) -> Self
62    where
63        Self: Sized + FromId,
64    {
65        unsafe { Self::from_id(msg_send![self.m_self(), initWithUUIDBytes: bytes]) }
66    }
67
68    /* Get UUID Values
69     */
70
71    /// Returns the UUID as bytes.
72    ///
73    /// # Arguments
74    ///
75    /// * uuid - The value of uuid represented as raw bytes.
76    #[method]
77    pub fn get_uuid_bytes(&self, uuid: *mut c_uchar) {
78        unsafe { msg_send![self.m_self(), getUUIDBytes: uuid] }
79    }
80
81    /// The UUID as a string.
82    #[property]
83    pub fn uuid_string(&self) -> NSString {
84        unsafe { NSString::from_id(msg_send![self.m_self(), UUIDString]) }
85    }
86
87    /// Compares [`NSUUID`] with another
88    #[method]
89    pub fn compare(&self, other_uuid: NSUUID) -> NSComparisonResult {
90        unsafe { msg_send![self.m_self(), compare: other_uuid] }
91    }
92}