add tc_task_get_taskmap
This commit is contained in:
103
lib/src/kv.rs
Normal file
103
lib/src/kv.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use crate::traits::*;
|
||||
use crate::types::*;
|
||||
|
||||
/// TCKV contains a key/value pair that is part of a task.
|
||||
///
|
||||
/// Neither key nor value are ever NULL. They remain owned by the TCKV and
|
||||
/// will be freed when it is freed with tc_kv_list_free.
|
||||
#[repr(C)]
|
||||
pub struct TCKV {
|
||||
pub key: *mut TCString<'static>,
|
||||
pub value: *mut TCString<'static>,
|
||||
}
|
||||
|
||||
impl PassByValue for TCKV {
|
||||
type RustType = (TCString<'static>, TCString<'static>);
|
||||
|
||||
unsafe fn from_ctype(self) -> Self::RustType {
|
||||
// SAFETY:
|
||||
// - self is owned, so we can take ownership of this TCString
|
||||
// - self.key is a valid, non-null TCString (see type docstring)
|
||||
let key = unsafe { TCString::take_from_arg(self.key) };
|
||||
// SAFETY: (same)
|
||||
let value = unsafe { TCString::take_from_arg(self.value) };
|
||||
(key, value)
|
||||
}
|
||||
|
||||
fn as_ctype((key, value): Self::RustType) -> Self {
|
||||
TCKV {
|
||||
// SAFETY: caller assumes ownership of this value
|
||||
key: unsafe { key.return_val() },
|
||||
// SAFETY: caller assumes ownership of this value
|
||||
value: unsafe { value.return_val() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// TCKVList represents a list of key/value pairs.
|
||||
///
|
||||
/// The content of this struct must be treated as read-only.
|
||||
#[repr(C)]
|
||||
pub struct TCKVList {
|
||||
/// number of key/value pairs in items
|
||||
len: libc::size_t,
|
||||
|
||||
/// total size of items (internal use only)
|
||||
_capacity: libc::size_t,
|
||||
|
||||
/// array of TCKV's. these remain owned by the TCKVList instance and will be freed by
|
||||
/// tc_kv_list_free. This pointer is never NULL for a valid TCKVList.
|
||||
items: *const TCKV,
|
||||
}
|
||||
|
||||
impl CList for TCKVList {
|
||||
type Element = TCKV;
|
||||
|
||||
unsafe fn from_raw_parts(items: *const Self::Element, len: usize, cap: usize) -> Self {
|
||||
TCKVList {
|
||||
len,
|
||||
_capacity: cap,
|
||||
items,
|
||||
}
|
||||
}
|
||||
|
||||
fn into_raw_parts(self) -> (*const Self::Element, usize, usize) {
|
||||
(self.items, self.len, self._capacity)
|
||||
}
|
||||
}
|
||||
|
||||
/// Free a TCKVList instance. The instance, and all TCKVs it contains, must not be used after
|
||||
/// this call.
|
||||
///
|
||||
/// When this call returns, the `items` pointer will be NULL, signalling an invalid TCKVList.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn tc_kv_list_free(tckvs: *mut TCKVList) {
|
||||
// SAFETY:
|
||||
// - tckvs is not NULL and points to a valid TCKVList (caller is not allowed to
|
||||
// modify the list)
|
||||
// - caller promises not to use the value after return
|
||||
unsafe { drop_value_list(tckvs) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn empty_list_has_non_null_pointer() {
|
||||
let tckvs = TCKVList::return_val(Vec::new());
|
||||
assert!(!tckvs.items.is_null());
|
||||
assert_eq!(tckvs.len, 0);
|
||||
assert_eq!(tckvs._capacity, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn free_sets_null_pointer() {
|
||||
let mut tckvs = TCKVList::return_val(Vec::new());
|
||||
// SAFETY: testing expected behavior
|
||||
unsafe { tc_kv_list_free(&mut tckvs) };
|
||||
assert!(tckvs.items.is_null());
|
||||
assert_eq!(tckvs.len, 0);
|
||||
assert_eq!(tckvs._capacity, 0);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ mod util;
|
||||
|
||||
pub mod annotation;
|
||||
pub mod atomic;
|
||||
pub mod kv;
|
||||
pub mod replica;
|
||||
pub mod result;
|
||||
pub mod status;
|
||||
@@ -18,6 +19,7 @@ pub mod workingset;
|
||||
|
||||
pub(crate) mod types {
|
||||
pub(crate) use crate::annotation::{TCAnnotation, TCAnnotationList};
|
||||
pub(crate) use crate::kv::{TCKVList, TCKV};
|
||||
pub(crate) use crate::replica::TCReplica;
|
||||
pub(crate) use crate::result::TCResult;
|
||||
pub(crate) use crate::status::TCStatus;
|
||||
|
||||
@@ -243,7 +243,24 @@ pub unsafe extern "C" fn tc_task_get_status<'a>(task: *mut TCTask) -> TCStatus {
|
||||
wrap(task, |task| task.get_status().into())
|
||||
}
|
||||
|
||||
// TODO: tc_task_get_taskmap (?? then we have to wrap a map..)
|
||||
/// Get the underlying key/value pairs for this task. The returned TCKVList is
|
||||
/// a "snapshot" of the task and will not be updated if the task is subsequently
|
||||
/// modified.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn tc_task_get_taskmap(task: *mut TCTask) -> TCKVList {
|
||||
wrap(task, |task| {
|
||||
let vec: Vec<TCKV> = task
|
||||
.get_taskmap()
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
let key = TCString::from(k.as_ref());
|
||||
let value = TCString::from(v.as_ref());
|
||||
TCKV::as_ctype((key, value))
|
||||
})
|
||||
.collect();
|
||||
TCKVList::return_val(vec)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get a task's description, or NULL if the task cannot be represented as a C string (e.g., if it
|
||||
/// contains embedded NUL characters).
|
||||
|
||||
Reference in New Issue
Block a user