Skip to content

Commit

Permalink
removed Option arount KeyExpr, Gravestone trait added to make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Feb 20, 2025
1 parent 0cde423 commit d9852fd
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 88 deletions.
16 changes: 7 additions & 9 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,22 @@ pub struct {type_name} {{
// done by "decl_c_type!" macro in transmute module.
s += format!(
"#[repr(C)]
#[derive(Default)]
pub struct {moved_type_name} {{
_this: {type_name},
}}
impl crate::transmute::TakeCType for {moved_type_name} {{
type CType = {type_name};
fn take_c_type(&mut self) -> Self::CType {{
std::mem::take(&mut self._this)
use crate::transmute::Gravestone;
std::mem::replace(&mut self._this, {type_name}::gravestone())
}}
}}
impl Drop for {type_name} {{
fn drop(&mut self) {{
use crate::transmute::RustTypeRef;
std::mem::take(self.as_rust_type_mut());
use crate::transmute::{{RustTypeRef, Gravestone, IntoRustType}};
let _ = std::mem::replace(self.as_rust_type_mut(), {type_name}::gravestone().into_rust_type());
}}
}}
"
Expand Down Expand Up @@ -1279,12 +1279,10 @@ pub fn find_loan_mut_functions(path_in: &str) -> Vec<FunctionSignature> {

pub fn find_take_from_loaned_functions(path_in: &str) -> Vec<FunctionSignature> {
let bindings = std::fs::read_to_string(path_in).unwrap();
let re= Regex::new(r"void (\w+)_take_from_loaned\(struct (\w+) \*(\w+)").unwrap();
let re = Regex::new(r"void (\w+)_take_from_loaned\(struct (\w+) \*(\w+)").unwrap();
let mut res = Vec::<FunctionSignature>::new();

for (_, [func_name, arg_type, arg_name]) in
re.captures_iter(&bindings).map(|c| c.extract())
{
for (_, [func_name, arg_type, arg_name]) in re.captures_iter(&bindings).map(|c| c.extract()) {
let (prefix, _, semantic, postfix) = split_type_name(arg_type);
let z_owned_type = format!("{}_{}_{}_{}*", prefix, "owned", semantic, postfix);
let z_loaned_type = format!("{}_{}_{}_{}*", prefix, "loaned", semantic, postfix);
Expand All @@ -1294,7 +1292,7 @@ pub fn find_take_from_loaned_functions(path_in: &str) -> Vec<FunctionSignature>
func_name.to_string() + "_take_from_loaned",
vec![
FuncArg::new(&z_owned_type, arg_name),
FuncArg::new(&z_loaned_type, "src")
FuncArg::new(&z_loaned_type, "src"),
],
);
res.push(f);
Expand Down
100 changes: 78 additions & 22 deletions src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use libc::strlen;

use crate::{
result::{self, z_result_t},
transmute::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
transmute::{Gravestone, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
};

pub struct CSlice {
Expand All @@ -42,11 +42,28 @@ pub extern "C" fn _z_drop_c_slice_default(data: *mut c_void, context: *mut c_voi
std::mem::drop(b);
}

#[derive(Default, Clone)]
#[derive(Clone)]
pub struct CSliceOwned(CSlice);
#[derive(Default)]
pub struct CSliceView(CSlice);

impl Gravestone for CSliceOwned {
fn gravestone() -> Self {
Self(CSlice::gravestone())
}
fn is_gravestone(&self) -> bool {
self.0.is_gravestone()
}
}

impl Gravestone for CSliceView {
fn gravestone() -> Self {
Self(CSlice::gravestone())
}
fn is_gravestone(&self) -> bool {
self.0.is_gravestone()
}
}

impl Deref for CSliceOwned {
type Target = CSlice;
fn deref(&self) -> &Self::Target {
Expand Down Expand Up @@ -158,7 +175,7 @@ impl CSlice {
#[allow(clippy::missing_safety_doc)]
pub unsafe fn new_owned_unchecked(data: *const u8, len: usize) -> Self {
if len == 0 {
return Self::default();
return Self::gravestone();
}
let b = unsafe { from_raw_parts(data, len).to_vec().into_boxed_slice() };
let slice = Box::leak(b);
Expand Down Expand Up @@ -216,15 +233,18 @@ impl Clone for CSlice {
}
}

impl Default for CSlice {
fn default() -> Self {
impl Gravestone for CSlice {
fn gravestone() -> Self {
Self {
data: null(),
len: 0,
drop: None,
context: null_mut(),
}
}
fn is_gravestone(&self) -> bool {
self.data.is_null()
}
}

impl Drop for CSlice {
Expand Down Expand Up @@ -274,7 +294,9 @@ decl_c_type!(
/// Constructs an empty view slice.
#[no_mangle]
pub extern "C" fn z_view_slice_empty(this_: &mut MaybeUninit<z_view_slice_t>) {
this_.as_rust_type_mut_uninit().write(CSliceView::default());
this_
.as_rust_type_mut_uninit()
.write(CSliceView::gravestone());
}

/// Constructs a `len` bytes long view starting at `start`.
Expand All @@ -294,7 +316,7 @@ pub unsafe extern "C" fn z_view_slice_from_buf(
result::Z_OK
}
Err(e) => {
this.write(CSliceView::default());
this.write(CSliceView::gravestone());
e
}
}
Expand All @@ -317,7 +339,7 @@ pub extern "C" fn z_view_slice_is_empty(this_: &z_view_slice_t) -> bool {
pub extern "C" fn z_slice_empty(this_: &mut MaybeUninit<z_owned_slice_t>) {
this_
.as_rust_type_mut_uninit()
.write(CSliceOwned::default());
.write(CSliceOwned::gravestone());
}

/// Constructs an empty `z_owned_slice_t`.
Expand Down Expand Up @@ -387,7 +409,7 @@ pub unsafe extern "C" fn z_slice_copy_from_buf(
result::Z_OK
}
Err(e) => {
this.write(CSliceOwned::default());
this.write(CSliceOwned::gravestone());
e
}
}
Expand Down Expand Up @@ -417,7 +439,7 @@ pub unsafe extern "C" fn z_slice_from_buf(
result::Z_OK
}
Err(e) => {
this.write(CSliceOwned::default());
this.write(CSliceOwned::gravestone());
e
}
}
Expand All @@ -427,13 +449,38 @@ pub use crate::opaque_types::{
z_loaned_string_t, z_moved_string_t, z_owned_string_t, z_view_string_t,
};

#[derive(Default, Clone)]
#[derive(Clone)]
pub struct CString(CSlice);
#[derive(Default)]
pub struct CStringOwned(CString);
#[derive(Default)]
pub struct CStringView(CString);

impl Gravestone for CString {
fn gravestone() -> Self {
Self(CSlice::gravestone())
}
fn is_gravestone(&self) -> bool {
self.0.is_gravestone()
}
}

impl Gravestone for CStringOwned {
fn gravestone() -> Self {
Self(CString::gravestone())
}
fn is_gravestone(&self) -> bool {
self.0.is_gravestone()
}
}

impl Gravestone for CStringView {
fn gravestone() -> Self {
Self(CString::gravestone())
}
fn is_gravestone(&self) -> bool {
self.0.is_gravestone()
}
}

impl CString {
pub fn new_borrowed_from_slice(slice: &[u8]) -> Self {
CString(CSlice::new_borrowed_from_slice(slice))
Expand Down Expand Up @@ -551,7 +598,7 @@ pub extern "C" fn z_internal_string_check(this_: &z_owned_string_t) -> bool {
pub extern "C" fn z_internal_string_null(this_: &mut MaybeUninit<z_owned_string_t>) {
this_
.as_rust_type_mut_uninit()
.write(CStringOwned::default());
.write(CStringOwned::gravestone());
}

/// @return ``true`` if view string is valid, ``false`` if it is in a gravestone state.
Expand All @@ -566,7 +613,7 @@ pub extern "C" fn z_view_string_is_empty(this_: &z_view_string_t) -> bool {
pub unsafe extern "C" fn z_string_empty(this_: &mut MaybeUninit<z_owned_string_t>) {
this_
.as_rust_type_mut_uninit()
.write(CStringOwned::default());
.write(CStringOwned::gravestone());
}

/// Constructs an empty view string.
Expand All @@ -575,7 +622,7 @@ pub unsafe extern "C" fn z_string_empty(this_: &mut MaybeUninit<z_owned_string_t
pub unsafe extern "C" fn z_view_string_empty(this_: &mut MaybeUninit<z_view_string_t>) {
this_
.as_rust_type_mut_uninit()
.write(CStringView::default());
.write(CStringView::gravestone());
}

/// Borrows string.
Expand Down Expand Up @@ -619,7 +666,7 @@ pub unsafe extern "C" fn z_string_copy_from_substr(
result::Z_OK
}
Err(e) => {
this.write(CStringOwned::default());
this.write(CStringOwned::gravestone());
e
}
}
Expand All @@ -646,7 +693,7 @@ pub unsafe extern "C" fn z_string_from_str(
result::Z_OK
}
Err(e) => {
this.write(CStringOwned::default());
this.write(CStringOwned::gravestone());
e
}
}
Expand All @@ -668,7 +715,7 @@ pub unsafe extern "C" fn z_view_string_from_str(
result::Z_OK
}
Err(e) => {
this.write(CStringView::default());
this.write(CStringView::gravestone());
e
}
}
Expand All @@ -691,7 +738,7 @@ pub unsafe extern "C" fn z_view_string_from_substr(
result::Z_OK
}
Err(e) => {
this.write(CStringView::default());
this.write(CStringView::gravestone());
e
}
}
Expand Down Expand Up @@ -741,10 +788,19 @@ decl_c_type!(
loaned(z_loaned_string_array_t),
);

impl Gravestone for ZVector {
fn gravestone() -> Self {
Vec::new()
}
fn is_gravestone(&self) -> bool {
self.is_empty()
}
}

/// Constructs a new empty string array.
#[no_mangle]
pub extern "C" fn z_string_array_new(this_: &mut MaybeUninit<z_owned_string_array_t>) {
this_.as_rust_type_mut_uninit().write(ZVector::new());
this_.as_rust_type_mut_uninit().write(ZVector::gravestone());
}

/// Constructs string array in its gravestone state.
Expand Down
11 changes: 10 additions & 1 deletion src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::transmute::IntoCType;
use crate::z_moved_source_info_t;
use crate::{
result,
transmute::{CTypeRef, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
transmute::{CTypeRef, Gravestone, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
z_id_t, z_loaned_bytes_t, z_loaned_encoding_t, z_loaned_keyexpr_t, z_loaned_session_t,
};

Expand Down Expand Up @@ -595,6 +595,15 @@ decl_c_type!(
loaned(z_loaned_source_info_t, SourceInfo),
);

impl Gravestone for SourceInfo {
fn gravestone() -> Self {
SourceInfo::default()
}
fn is_gravestone(&self) -> bool {
self.source_id().is_none() && self.source_sn().is_none()
}
}

#[cfg(feature = "unstable")]
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
/// @brief Creates source info.
Expand Down
11 changes: 10 additions & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use zenoh::bytes::Encoding;
pub use crate::opaque_types::{z_loaned_encoding_t, z_owned_encoding_t};
use crate::{
result::{self, z_result_t},
transmute::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
transmute::{Gravestone, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
z_moved_encoding_t, z_owned_string_t, z_string_copy_from_substr,
};

Expand All @@ -36,6 +36,15 @@ decl_c_type!(
loaned(z_loaned_encoding_t, Encoding),
);

impl Gravestone for Encoding {
fn gravestone() -> Self {
Encoding::default()
}
fn is_gravestone(&self) -> bool {
self == &Self::default()
}
}

/// Constructs a `z_owned_encoding_t` from a specified substring.
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
Expand Down
17 changes: 14 additions & 3 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use zenoh::{
pub use crate::opaque_types::{z_loaned_reply_err_t, z_moved_reply_err_t, z_owned_reply_err_t};
use crate::{
result,
transmute::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
transmute::{Gravestone, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
z_closure_reply_call, z_closure_reply_loan, z_congestion_control_t, z_consolidation_mode_t,
z_loaned_bytes_t, z_loaned_encoding_t, z_loaned_keyexpr_t, z_loaned_sample_t,
z_loaned_session_t, z_moved_bytes_t, z_moved_closure_reply_t, z_moved_encoding_t, z_priority_t,
Expand All @@ -45,17 +45,28 @@ decl_c_type!(
loaned(z_loaned_reply_err_t, ReplyError),
);

impl Gravestone for ReplyError {
fn gravestone() -> Self {
ReplyError::empty()
}
fn is_gravestone(&self) -> bool {
self.payload().is_empty()
}
}

/// Constructs an empty `z_owned_reply_err_t`.
#[no_mangle]
pub extern "C" fn z_internal_reply_err_null(this_: &mut MaybeUninit<z_owned_reply_err_t>) {
this_.as_rust_type_mut_uninit().write(ReplyError::default());
this_
.as_rust_type_mut_uninit()
.write(ReplyError::gravestone());
}

/// Returns ``true`` if reply error is in non-default state, ``false`` otherwise.
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub extern "C" fn z_internal_reply_err_check(this_: &'static z_owned_reply_err_t) -> bool {
!this_.as_rust_type_ref().payload().is_empty()
!this_.as_rust_type_ref().is_gravestone()
}

/// Returns reply error payload.
Expand Down
Loading

0 comments on commit d9852fd

Please sign in to comment.