1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use objc2::rc::{Id, Shared};
use objc2::{msg_send, Encode, Encoding, RefEncode};
use super::{NSCopying, NSObject};
object! {
unsafe pub struct NSUUID: NSObject;
}
#[repr(transparent)]
struct UuidBytes([u8; 16]);
unsafe impl RefEncode for UuidBytes {
const ENCODING_REF: Encoding<'static> = Encoding::Array(16, &u8::ENCODING);
}
unsafe impl Sync for NSUUID {}
unsafe impl Send for NSUUID {}
impl NSUUID {
pub fn new_v4() -> Id<Self, Shared> {
unsafe {
let obj: *mut Self = msg_send![Self::class(), alloc];
let obj: *mut Self = msg_send![obj, init];
Id::new(obj).unwrap()
}
}
pub fn from_bytes(bytes: [u8; 16]) -> Id<Self, Shared> {
let bytes = UuidBytes(bytes);
unsafe {
let obj: *mut Self = msg_send![Self::class(), alloc];
let obj: *mut Self = msg_send![obj, initWithUUIDBytes: &bytes];
Id::new(obj).unwrap()
}
}
pub fn as_bytes(&self) -> [u8; 16] {
let mut bytes = UuidBytes([0; 16]);
let _: () = unsafe { msg_send![self, getUUIDBytes: &mut bytes] };
bytes.0
}
}
#[cfg(feature = "uuid")]
impl NSUUID {}
unsafe impl NSCopying for NSUUID {
type Ownership = Shared;
type Output = NSUUID;
}
impl alloc::borrow::ToOwned for NSUUID {
type Owned = Id<NSUUID, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let uuid1 = NSUUID::new_v4();
let uuid2 = NSUUID::new_v4();
assert_ne!(uuid1, uuid2, "Statistically very unlikely");
}
#[test]
fn test_bytes() {
let uuid = NSUUID::from_bytes([10; 16]);
assert_eq!(uuid.as_bytes(), [10; 16]);
}
}