add some UUID support

This commit is contained in:
Dustin J. Mitchell 2022-01-23 19:45:39 +00:00
parent e590dc7c98
commit 46e08bc040
8 changed files with 136 additions and 1 deletions

View file

@ -11,6 +11,7 @@ crate-type = ["cdylib"]
[dependencies]
libc = "0.2.113"
taskchampion = { path = "../taskchampion" }
uuid = { version = "^0.8.2", features = ["serde", "v4"] }
anyhow = "1.0"
[build-dependencies]

View file

@ -10,6 +10,16 @@ fn main() {
.with_language(Language::C)
.with_config(Config {
cpp_compat: true,
export: ExportConfig {
item_types: vec![
ItemType::Structs,
ItemType::Globals,
ItemType::Functions,
ItemType::Constants,
ItemType::OpaqueItems,
],
..Default::default()
},
..Default::default()
})
.generate()

View file

@ -1 +1,2 @@
pub mod replica;
pub mod uuid;

63
lib/src/uuid.rs Normal file
View file

@ -0,0 +1,63 @@
use libc;
use taskchampion::Uuid as TcUuid;
/// Uuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
#[repr(C)]
pub struct Uuid([u8; 16]);
impl From<TcUuid> for Uuid {
fn from(uuid: TcUuid) -> Uuid {
// TODO: can we avoid clone here?
Uuid(uuid.as_bytes().clone())
}
}
impl From<Uuid> for TcUuid {
fn from(uuid: Uuid) -> TcUuid {
TcUuid::from_bytes(uuid.0)
}
}
/// Create a new, randomly-generated UUID.
#[no_mangle]
pub extern "C" fn tc_uuid_new_v4() -> Uuid {
TcUuid::new_v4().into()
}
/// Create a new UUID with the nil value.
#[no_mangle]
pub extern "C" fn tc_uuid_nil() -> Uuid {
TcUuid::nil().into()
}
/// Length, in bytes, of a C string containing a Uuid.
#[no_mangle]
pub static TC_UUID_STRING_BYTES: usize = ::uuid::adapter::Hyphenated::LENGTH;
/// Write the string representation of a Uuid into the given buffer, which must be
/// at least TC_UUID_STRING_BYTES long. No NUL terminator is added.
#[no_mangle]
pub extern "C" fn tc_uuid_to_str<'a>(uuid: Uuid, out: *mut libc::c_char) {
debug_assert!(!out.is_null());
let buf: &'a mut [u8] = unsafe {
std::slice::from_raw_parts_mut(out as *mut u8, ::uuid::adapter::Hyphenated::LENGTH)
};
let uuid: TcUuid = uuid.into();
uuid.to_hyphenated().encode_lower(buf);
}
/// Parse the given value as a UUID. The value must be exactly TC_UUID_STRING_BYTES long. Returns
/// false on failure.
#[no_mangle]
pub extern "C" fn tc_uuid_from_str<'a>(val: *const libc::c_char, out: *mut Uuid) -> bool {
debug_assert!(!val.is_null());
debug_assert!(!out.is_null());
let slice = unsafe { std::slice::from_raw_parts(val as *const u8, TC_UUID_STRING_BYTES) };
if let Ok(s) = std::str::from_utf8(slice) {
if let Ok(u) = TcUuid::parse_str(s) {
unsafe { *out = u.into() };
return true;
}
}
false
}

View file

@ -8,8 +8,15 @@
/// for querying and modifying that data.
struct Replica;
/// Uuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
struct Uuid {
uint8_t _0[16];
};
extern "C" {
extern const uintptr_t TC_UUID_STRING_BYTES;
/// Create a new Replica.
///
/// If path is NULL, then an in-memory replica is created. Otherwise, path is the path to the
@ -34,4 +41,18 @@ const char *tc_replica_error(Replica *rep);
/// Free a Replica.
void tc_replica_free(Replica *rep);
/// Create a new, randomly-generated UUID.
Uuid tc_uuid_new_v4();
/// Create a new UUID with the nil value.
Uuid tc_uuid_nil();
/// Write the string representation of a Uuid into the given buffer, which must be
/// at least TC_UUID_STRING_BYTES long. No NUL terminator is added.
void tc_uuid_to_str(Uuid uuid, char *out);
/// Parse the given value as a UUID. The value must be exactly TC_UUID_STRING_BYTES long. Returns
/// false on failure.
bool tc_uuid_from_str(const char *val, Uuid *out);
} // extern "C"