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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#![warn(unsafe_op_in_unsafe_fn)]
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]
use ffizz_passby::PassByValue;
use libc::c_char;
use std::ffi::CStr;
struct Uuid(uuid::Uuid);
pub const UUID_STRING_BYTES: usize = 36;
#[derive(Clone, Copy)]
#[repr(C)]
pub struct uuid_t([u8; 16]);
impl PassByValue for Uuid {
type CType = uuid_t;
unsafe fn from_ctype(cval: uuid_t) -> Self {
Uuid(uuid::Uuid::from_bytes(cval.0))
}
fn as_ctype(self) -> uuid_t {
uuid_t(*self.0.as_bytes())
}
}
#[no_mangle]
pub unsafe extern "C" fn uuid_new_v4() -> uuid_t {
unsafe { Uuid(uuid::Uuid::new_v4()).return_val() }
}
#[no_mangle]
pub unsafe extern "C" fn uuid_nil() -> uuid_t {
unsafe { Uuid(uuid::Uuid::nil()).return_val() }
}
#[no_mangle]
pub unsafe extern "C" fn uuid_version(uuid: uuid_t) -> usize {
let uuid: Uuid = unsafe { Uuid::val_from_arg(uuid) };
uuid.0.get_version_num()
}
#[no_mangle]
pub unsafe extern "C" fn uuid_to_buf(tcuuid: uuid_t, buf: *mut c_char) {
debug_assert!(!buf.is_null());
let buf: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(buf as *mut u8, UUID_STRING_BYTES) };
let uuid: Uuid = unsafe { Uuid::val_from_arg(tcuuid) };
uuid.0.as_hyphenated().encode_lower(buf);
}
#[no_mangle]
pub unsafe extern "C" fn uuid_from_str(s: *const c_char, uuid_out: *mut uuid_t) -> bool {
debug_assert!(!s.is_null());
debug_assert!(!uuid_out.is_null());
let s = unsafe { CStr::from_ptr(s) };
if let Ok(s) = s.to_str() {
if let Ok(u) = uuid::Uuid::parse_str(s) {
unsafe { Uuid::val_to_arg_out(Uuid(u), uuid_out) };
return true;
}
}
false
}
fn main() {
let u = unsafe { uuid_new_v4() };
assert_eq!(unsafe { uuid_version(u) }, 4);
let u = unsafe { uuid_nil() };
assert_eq!(unsafe { uuid_version(u) }, 0);
let mut buf = [0u8; UUID_STRING_BYTES];
unsafe { uuid_to_buf(u, buf.as_mut_ptr() as *mut c_char) };
assert_eq!(
std::str::from_utf8(&buf[..]).expect("invalid utf-8"),
"00000000-0000-0000-0000-000000000000"
);
let mut u = unsafe { uuid_nil() };
assert!(unsafe {
uuid_from_str(
"d9c5d004-1bf4-11ed-861d-0242ac120002\0".as_ptr() as *const c_char,
&mut u as *mut uuid_t,
)
});
assert_eq!(unsafe { uuid_version(u) }, 1);
}