taskchampion_lib/uuid.rs
1use crate::traits::*;
2use crate::types::*;
3use libc;
4use taskchampion::Uuid;
5
6#[ffizz_header::item]
7#[ffizz(order = 300)]
8/// ***** TCUuid *****
9///
10/// TCUuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
11/// Uuids are typically treated as opaque, but the bytes are available in big-endian format.
12///
13/// ```c
14/// typedef struct TCUuid {
15/// uint8_t bytes[16];
16/// } TCUuid;
17/// ```
18#[repr(C)]
19#[derive(Debug, Default)]
20pub struct TCUuid([u8; 16]);
21
22impl PassByValue for TCUuid {
23 type RustType = Uuid;
24
25 unsafe fn from_ctype(self) -> Self::RustType {
26 // SAFETY:
27 // - any 16-byte value is a valid Uuid
28 Uuid::from_bytes(self.0)
29 }
30
31 fn as_ctype(arg: Uuid) -> Self {
32 TCUuid(*arg.as_bytes())
33 }
34}
35
36#[ffizz_header::item]
37#[ffizz(order = 301)]
38/// Length, in bytes, of the string representation of a UUID (without NUL terminator)
39///
40/// ```c
41/// #define TC_UUID_STRING_BYTES 36
42/// ```
43// TODO: debug_assert or static_assert this somewhere?
44pub const TC_UUID_STRING_BYTES: usize = 36;
45
46#[ffizz_header::item]
47#[ffizz(order = 310)]
48/// TCUuidList represents a list of uuids.
49///
50/// The content of this struct must be treated as read-only.
51///
52/// ```c
53/// typedef struct TCUuidList {
54/// // number of uuids in items
55/// size_t len;
56/// // reserved
57/// size_t _u1;
58/// // Array of uuids. This pointer is never NULL for a valid TCUuidList.
59/// struct TCUuid *items;
60/// } TCUuidList;
61/// ```
62#[repr(C)]
63pub struct TCUuidList {
64 /// number of uuids in items
65 len: libc::size_t,
66
67 /// total size of items (internal use only)
68 capacity: libc::size_t,
69
70 /// Array of uuids. This pointer is never NULL for a valid TCUuidList.
71 items: *mut TCUuid,
72}
73
74impl CList for TCUuidList {
75 type Element = TCUuid;
76
77 unsafe fn from_raw_parts(items: *mut Self::Element, len: usize, cap: usize) -> Self {
78 TCUuidList {
79 len,
80 capacity: cap,
81 items,
82 }
83 }
84
85 fn slice(&mut self) -> &mut [Self::Element] {
86 // SAFETY:
87 // - because we have &mut self, we have read/write access to items[0..len]
88 // - all items are properly initialized Element's
89 // - return value lifetime is equal to &mmut self's, so access is exclusive
90 // - items and len came from Vec, so total size is < isize::MAX
91 unsafe { std::slice::from_raw_parts_mut(self.items, self.len) }
92 }
93
94 fn into_raw_parts(self) -> (*mut Self::Element, usize, usize) {
95 (self.items, self.len, self.capacity)
96 }
97}
98
99#[ffizz_header::item]
100#[ffizz(order = 302)]
101/// Create a new, randomly-generated UUID.
102///
103/// ```c
104/// EXTERN_C struct TCUuid tc_uuid_new_v4(void);
105/// ```
106#[no_mangle]
107pub unsafe extern "C" fn tc_uuid_new_v4() -> TCUuid {
108 // SAFETY:
109 // - value is not allocated
110 unsafe { TCUuid::return_val(Uuid::new_v4()) }
111}
112
113#[ffizz_header::item]
114#[ffizz(order = 302)]
115/// Create a new UUID with the nil value.
116///
117/// ```c
118/// EXTERN_C struct TCUuid tc_uuid_nil(void);
119/// ```
120#[no_mangle]
121pub unsafe extern "C" fn tc_uuid_nil() -> TCUuid {
122 // SAFETY:
123 // - value is not allocated
124 unsafe { TCUuid::return_val(Uuid::nil()) }
125}
126
127#[ffizz_header::item]
128#[ffizz(order = 302)]
129/// Write the string representation of a TCUuid into the given buffer, which must be
130/// at least TC_UUID_STRING_BYTES long. No NUL terminator is added.
131///
132/// ```c
133/// EXTERN_C void tc_uuid_to_buf(struct TCUuid tcuuid, char *buf);
134/// ```
135#[no_mangle]
136pub unsafe extern "C" fn tc_uuid_to_buf(tcuuid: TCUuid, buf: *mut libc::c_char) {
137 debug_assert!(!buf.is_null());
138 // SAFETY:
139 // - buf is valid for len bytes (by C convention)
140 // - (no alignment requirements for a byte slice)
141 // - content of buf will not be mutated during the lifetime of this slice (lifetime
142 // does not outlive this function call)
143 // - the length of the buffer is less than isize::MAX (promised by caller)
144 let buf: &mut [u8] =
145 unsafe { std::slice::from_raw_parts_mut(buf as *mut u8, TC_UUID_STRING_BYTES) };
146 // SAFETY:
147 // - tcuuid is a valid TCUuid (all byte patterns are valid)
148 let uuid: Uuid = unsafe { TCUuid::val_from_arg(tcuuid) };
149 uuid.as_hyphenated().encode_lower(buf);
150}
151
152#[ffizz_header::item]
153#[ffizz(order = 302)]
154/// Return the hyphenated string representation of a TCUuid. The returned string
155/// must be freed with tc_string_free.
156///
157/// ```c
158/// EXTERN_C struct TCString tc_uuid_to_str(struct TCUuid tcuuid);
159/// ```
160#[no_mangle]
161pub unsafe extern "C" fn tc_uuid_to_str(tcuuid: TCUuid) -> TCString {
162 // SAFETY:
163 // - tcuuid is a valid TCUuid (all byte patterns are valid)
164 let uuid: Uuid = unsafe { TCUuid::val_from_arg(tcuuid) };
165 let s = uuid.to_string();
166 // SAFETY:
167 // - caller promises to free this value.
168 unsafe { TCString::return_val(s.into()) }
169}
170
171#[ffizz_header::item]
172#[ffizz(order = 302)]
173/// Parse the given string as a UUID. Returns TC_RESULT_ERROR on parse failure or if the given
174/// string is not valid.
175///
176/// ```c
177/// EXTERN_C TCResult tc_uuid_from_str(struct TCString s, struct TCUuid *uuid_out);
178/// ```
179#[no_mangle]
180pub unsafe extern "C" fn tc_uuid_from_str(s: TCString, uuid_out: *mut TCUuid) -> TCResult {
181 debug_assert!(!s.is_null());
182 debug_assert!(!uuid_out.is_null());
183 // SAFETY:
184 // - s is valid (promised by caller)
185 // - caller will not use s after this call (convention)
186 let mut s = unsafe { TCString::val_from_arg(s) };
187 if let Ok(s) = s.as_str() {
188 if let Ok(u) = Uuid::parse_str(s) {
189 // SAFETY:
190 // - uuid_out is not NULL (promised by caller)
191 // - alignment is not required
192 unsafe { TCUuid::val_to_arg_out(u, uuid_out) };
193 return TCResult::Ok;
194 }
195 }
196 TCResult::Error
197}
198
199#[ffizz_header::item]
200#[ffizz(order = 312)]
201/// Free a TCUuidList instance. The instance, and all TCUuids it contains, must not be used after
202/// this call.
203///
204/// When this call returns, the `items` pointer will be NULL, signalling an invalid TCUuidList.
205///
206/// ```c
207/// EXTERN_C void tc_uuid_list_free(struct TCUuidList *tcuuids);
208/// ```
209#[no_mangle]
210pub unsafe extern "C" fn tc_uuid_list_free(tcuuids: *mut TCUuidList) {
211 // SAFETY:
212 // - tcuuids is not NULL and points to a valid TCUuidList (caller is not allowed to
213 // modify the list)
214 // - caller promises not to use the value after return
215 unsafe { drop_value_list(tcuuids) };
216}
217
218#[cfg(test)]
219mod test {
220 use super::*;
221
222 #[test]
223 fn empty_list_has_non_null_pointer() {
224 let tcuuids = unsafe { TCUuidList::return_val(Vec::new()) };
225 assert!(!tcuuids.items.is_null());
226 assert_eq!(tcuuids.len, 0);
227 assert_eq!(tcuuids.capacity, 0);
228 }
229
230 #[test]
231 fn free_sets_null_pointer() {
232 let mut tcuuids = unsafe { TCUuidList::return_val(Vec::new()) };
233 // SAFETY: testing expected behavior
234 unsafe { tc_uuid_list_free(&mut tcuuids) };
235 assert!(tcuuids.items.is_null());
236 assert_eq!(tcuuids.len, 0);
237 assert_eq!(tcuuids.capacity, 0);
238 }
239}