add TC prefix to types, too

This commit is contained in:
Dustin J. Mitchell
2022-01-23 19:57:42 +00:00
parent 46e08bc040
commit 821118106a
6 changed files with 69 additions and 77 deletions

View File

@@ -1,60 +1,63 @@
use libc;
use taskchampion::Uuid as TcUuid;
use taskchampion::Uuid;
/// Uuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
/// TCUuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
/// Uuids are typically treated as opaque, but the bytes are available in big-endian format.
///
/// cbindgen:field-names=[bytes]
#[repr(C)]
pub struct Uuid([u8; 16]);
pub struct TCUuid([u8; 16]);
impl From<TcUuid> for Uuid {
fn from(uuid: TcUuid) -> Uuid {
impl From<Uuid> for TCUuid {
fn from(uuid: Uuid) -> TCUuid {
// TODO: can we avoid clone here?
Uuid(uuid.as_bytes().clone())
TCUuid(uuid.as_bytes().clone())
}
}
impl From<Uuid> for TcUuid {
fn from(uuid: Uuid) -> TcUuid {
TcUuid::from_bytes(uuid.0)
impl From<TCUuid> for Uuid {
fn from(uuid: TCUuid) -> Uuid {
Uuid::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()
pub extern "C" fn tc_uuid_new_v4() -> TCUuid {
Uuid::new_v4().into()
}
/// Create a new UUID with the nil value.
#[no_mangle]
pub extern "C" fn tc_uuid_nil() -> Uuid {
TcUuid::nil().into()
pub extern "C" fn tc_uuid_nil() -> TCUuid {
Uuid::nil().into()
}
/// Length, in bytes, of a C string containing a Uuid.
/// Length, in bytes, of a C string containing a TCUuid.
#[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
/// Write the string representation of a TCUuid 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) {
pub extern "C" fn tc_uuid_to_str<'a>(uuid: TCUuid, 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();
let uuid: Uuid = 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 {
pub extern "C" fn tc_uuid_from_str<'a>(val: *const libc::c_char, out: *mut TCUuid) -> 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) {
if let Ok(u) = Uuid::parse_str(s) {
unsafe { *out = u.into() };
return true;
}