TCTags -> TCStrings to be more general

This commit is contained in:
Dustin J. Mitchell
2022-02-06 16:40:17 +00:00
parent 3d248b55fd
commit 831eb0bb15
5 changed files with 42 additions and 42 deletions

View File

@@ -3,11 +3,11 @@
mod traits;
mod util;
pub mod arrays;
pub mod atomic;
pub mod replica;
pub mod result;
pub mod status;
pub mod string;
pub mod strings;
pub mod task;
pub mod uuid;

View File

@@ -3,41 +3,41 @@ use crate::traits::*;
use crate::util::{drop_pointer_array, vec_into_raw_parts};
use std::ptr::NonNull;
/// TCTags represents a list of tags associated with a task.
/// TCStrings represents a list of tags associated with a task.
///
/// The content of this struct must be treated as read-only.
///
/// The lifetime of a TCTags instance is independent of the task, and it
/// The lifetime of a TCStrings instance is independent of the task, and it
/// will remain valid even if the task is freed.
#[repr(C)]
pub struct TCTags {
pub struct TCStrings {
/// number of tags in items
len: libc::size_t,
/// total size of items (internal use only)
_capacity: libc::size_t,
/// strings representing each tag. these remain owned by the TCTags instance and will be freed
/// by tc_tags_free.
/// strings representing each tag. these remain owned by the TCStrings instance and will be freed
/// by tc_strings_free.
items: *const NonNull<TCString<'static>>,
}
impl PassByValue for Vec<NonNull<TCString<'static>>> {
type CType = TCTags;
type CType = TCStrings;
unsafe fn from_ctype(arg: TCTags) -> Self {
unsafe fn from_ctype(arg: TCStrings) -> Self {
// SAFETY:
// - C treats TCTags as read-only, so items, len, and _capacity all came
// - C treats TCStrings as read-only, so items, len, and _capacity all came
// from a Vec originally.
unsafe { Vec::from_raw_parts(arg.items as *mut _, arg.len, arg._capacity) }
}
fn as_ctype(self) -> TCTags {
fn as_ctype(self) -> TCStrings {
// emulate Vec::into_raw_parts():
// - disable dropping the Vec with ManuallyDrop
// - extract ptr, len, and capacity using those methods
let (items, len, _capacity) = vec_into_raw_parts(self);
TCTags {
TCStrings {
len,
_capacity,
items,
@@ -45,7 +45,7 @@ impl PassByValue for Vec<NonNull<TCString<'static>>> {
}
}
impl Default for TCTags {
impl Default for TCStrings {
fn default() -> Self {
Self {
len: 0,
@@ -55,13 +55,13 @@ impl Default for TCTags {
}
}
/// Free a TCTags instance. The instance, and all TCStrings it contains, must not be used after
/// Free a TCStrings instance. The instance, and all TCStrings it contains, must not be used after
/// this call.
#[no_mangle]
pub extern "C" fn tc_tags_free<'a>(tctags: *mut TCTags) {
pub extern "C" fn tc_strings_free<'a>(tctags: *mut TCStrings) {
debug_assert!(!tctags.is_null());
// SAFETY:
// - *tctags is a valid TCTags (caller promises to treat it as read-only)
let tags = unsafe { Vec::take_from_arg(tctags, TCTags::default()) };
// - *tctags is a valid TCStrings (caller promises to treat it as read-only)
let tags = unsafe { Vec::take_from_arg(tctags, TCStrings::default()) };
drop_pointer_array(tags);
}

View File

@@ -1,7 +1,7 @@
use crate::traits::*;
use crate::util::err_to_tcstring;
use crate::{
arrays::TCTags, replica::TCReplica, result::TCResult, status::TCStatus, string::TCString,
replica::TCReplica, result::TCResult, status::TCStatus, string::TCString, strings::TCStrings,
uuid::TCUuid,
};
use chrono::{DateTime, TimeZone, Utc};
@@ -319,7 +319,7 @@ pub extern "C" fn tc_task_has_tag<'a>(task: *mut TCTask, tag: *mut TCString) ->
/// Get the tags for the task. The task must not be NULL.
#[no_mangle]
pub extern "C" fn tc_task_get_tags<'a>(task: *mut TCTask) -> TCTags {
pub extern "C" fn tc_task_get_tags<'a>(task: *mut TCTask) -> TCStrings {
wrap(task, |task| {
let tcstrings: Vec<NonNull<TCString<'static>>> = task
.get_tags()