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,30 +1,29 @@
use libc::c_char;
use std::ffi::{CStr, CString, OsStr};
use std::path::PathBuf;
use taskchampion::Replica as TCReplica;
use taskchampion::StorageConfig;
use taskchampion::{Replica, StorageConfig};
// TODO: unix-only
use std::os::unix::ffi::OsStrExt;
/// A replica represents an instance of a user's task data, providing an easy interface
/// for querying and modifying that data.
pub struct Replica {
pub struct TCReplica {
// TODO: make this an option so that it can be take()n when holding a mut ref
inner: TCReplica,
inner: Replica,
error: Option<CString>,
}
/// Create a new Replica.
/// Create a new TCReplica.
///
/// If path is NULL, then an in-memory replica is created. Otherwise, path is the path to the
/// on-disk storage for this replica. The path argument is no longer referenced after return.
///
/// Returns NULL on error; see tc_replica_error.
///
/// Replicas are not threadsafe.
/// TCReplicas are not threadsafe.
#[no_mangle]
pub extern "C" fn tc_replica_new<'a>(path: *const c_char) -> *mut Replica {
pub extern "C" fn tc_replica_new<'a>(path: *const c_char) -> *mut TCReplica {
let storage_res = if path.is_null() {
StorageConfig::InMemory.into_storage()
} else {
@@ -40,23 +39,23 @@ pub extern "C" fn tc_replica_new<'a>(path: *const c_char) -> *mut Replica {
Err(_) => return std::ptr::null_mut(),
};
Box::into_raw(Box::new(Replica {
inner: TCReplica::new(storage),
Box::into_raw(Box::new(TCReplica {
inner: Replica::new(storage),
error: None,
}))
}
/// Utility function to safely convert *mut Replica into &mut Replica
fn rep_ref(rep: *mut Replica) -> &'static mut Replica {
/// Utility function to safely convert *mut TCReplica into &mut TCReplica
fn rep_ref(rep: *mut TCReplica) -> &'static mut TCReplica {
debug_assert!(!rep.is_null());
unsafe { &mut *rep }
}
fn wrap<'a, T, F>(rep: *mut Replica, f: F, err_value: T) -> T
fn wrap<'a, T, F>(rep: *mut TCReplica, f: F, err_value: T) -> T
where
F: FnOnce(&mut TCReplica) -> anyhow::Result<T>,
F: FnOnce(&mut Replica) -> anyhow::Result<T>,
{
let rep: &'a mut Replica = rep_ref(rep);
let rep: &'a mut TCReplica = rep_ref(rep);
match f(&mut rep.inner) {
Ok(v) => v,
Err(e) => {
@@ -76,7 +75,7 @@ where
/// Returns -1 on error, 0 if there are no local operations to undo, and 1 if operations were
/// undone.
#[no_mangle]
pub extern "C" fn tc_replica_undo<'a>(rep: *mut Replica) -> i32 {
pub extern "C" fn tc_replica_undo<'a>(rep: *mut TCReplica) -> i32 {
wrap(rep, |rep| Ok(if rep.undo()? { 1 } else { 0 }), -1)
}
@@ -84,8 +83,8 @@ pub extern "C" fn tc_replica_undo<'a>(rep: *mut Replica) -> i32 {
///
/// The returned string is valid until the next replica operation.
#[no_mangle]
pub extern "C" fn tc_replica_error<'a>(rep: *mut Replica) -> *const c_char {
let rep: &'a Replica = rep_ref(rep);
pub extern "C" fn tc_replica_error<'a>(rep: *mut TCReplica) -> *const c_char {
let rep: &'a TCReplica = rep_ref(rep);
if let Some(ref e) = rep.error {
e.as_ptr()
} else {
@@ -93,8 +92,8 @@ pub extern "C" fn tc_replica_error<'a>(rep: *mut Replica) -> *const c_char {
}
}
/// Free a Replica.
/// Free a TCReplica.
#[no_mangle]
pub extern "C" fn tc_replica_free(rep: *mut Replica) {
pub extern "C" fn tc_replica_free(rep: *mut TCReplica) {
drop(unsafe { Box::from_raw(rep) });
}