Skip to content

Commit d9852fd

Browse files
committed
removed Option arount KeyExpr, Gravestone trait added to make it work
1 parent 0cde423 commit d9852fd

File tree

9 files changed

+202
-88
lines changed

9 files changed

+202
-88
lines changed

build.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,22 @@ pub struct {type_name} {{
196196
// done by "decl_c_type!" macro in transmute module.
197197
s += format!(
198198
"#[repr(C)]
199-
#[derive(Default)]
200199
pub struct {moved_type_name} {{
201200
_this: {type_name},
202201
}}
203202
204203
impl crate::transmute::TakeCType for {moved_type_name} {{
205204
type CType = {type_name};
206205
fn take_c_type(&mut self) -> Self::CType {{
207-
std::mem::take(&mut self._this)
206+
use crate::transmute::Gravestone;
207+
std::mem::replace(&mut self._this, {type_name}::gravestone())
208208
}}
209209
}}
210210
211211
impl Drop for {type_name} {{
212212
fn drop(&mut self) {{
213-
use crate::transmute::RustTypeRef;
214-
std::mem::take(self.as_rust_type_mut());
213+
use crate::transmute::{{RustTypeRef, Gravestone, IntoRustType}};
214+
let _ = std::mem::replace(self.as_rust_type_mut(), {type_name}::gravestone().into_rust_type());
215215
}}
216216
}}
217217
"
@@ -1279,12 +1279,10 @@ pub fn find_loan_mut_functions(path_in: &str) -> Vec<FunctionSignature> {
12791279

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

1285-
for (_, [func_name, arg_type, arg_name]) in
1286-
re.captures_iter(&bindings).map(|c| c.extract())
1287-
{
1285+
for (_, [func_name, arg_type, arg_name]) in re.captures_iter(&bindings).map(|c| c.extract()) {
12881286
let (prefix, _, semantic, postfix) = split_type_name(arg_type);
12891287
let z_owned_type = format!("{}_{}_{}_{}*", prefix, "owned", semantic, postfix);
12901288
let z_loaned_type = format!("{}_{}_{}_{}*", prefix, "loaned", semantic, postfix);
@@ -1294,7 +1292,7 @@ pub fn find_take_from_loaned_functions(path_in: &str) -> Vec<FunctionSignature>
12941292
func_name.to_string() + "_take_from_loaned",
12951293
vec![
12961294
FuncArg::new(&z_owned_type, arg_name),
1297-
FuncArg::new(&z_loaned_type, "src")
1295+
FuncArg::new(&z_loaned_type, "src"),
12981296
],
12991297
);
13001298
res.push(f);

src/collections.rs

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use libc::strlen;
2525

2626
use crate::{
2727
result::{self, z_result_t},
28-
transmute::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
28+
transmute::{Gravestone, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
2929
};
3030

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

45-
#[derive(Default, Clone)]
45+
#[derive(Clone)]
4646
pub struct CSliceOwned(CSlice);
47-
#[derive(Default)]
4847
pub struct CSliceView(CSlice);
4948

49+
impl Gravestone for CSliceOwned {
50+
fn gravestone() -> Self {
51+
Self(CSlice::gravestone())
52+
}
53+
fn is_gravestone(&self) -> bool {
54+
self.0.is_gravestone()
55+
}
56+
}
57+
58+
impl Gravestone for CSliceView {
59+
fn gravestone() -> Self {
60+
Self(CSlice::gravestone())
61+
}
62+
fn is_gravestone(&self) -> bool {
63+
self.0.is_gravestone()
64+
}
65+
}
66+
5067
impl Deref for CSliceOwned {
5168
type Target = CSlice;
5269
fn deref(&self) -> &Self::Target {
@@ -158,7 +175,7 @@ impl CSlice {
158175
#[allow(clippy::missing_safety_doc)]
159176
pub unsafe fn new_owned_unchecked(data: *const u8, len: usize) -> Self {
160177
if len == 0 {
161-
return Self::default();
178+
return Self::gravestone();
162179
}
163180
let b = unsafe { from_raw_parts(data, len).to_vec().into_boxed_slice() };
164181
let slice = Box::leak(b);
@@ -216,15 +233,18 @@ impl Clone for CSlice {
216233
}
217234
}
218235

219-
impl Default for CSlice {
220-
fn default() -> Self {
236+
impl Gravestone for CSlice {
237+
fn gravestone() -> Self {
221238
Self {
222239
data: null(),
223240
len: 0,
224241
drop: None,
225242
context: null_mut(),
226243
}
227244
}
245+
fn is_gravestone(&self) -> bool {
246+
self.data.is_null()
247+
}
228248
}
229249

230250
impl Drop for CSlice {
@@ -274,7 +294,9 @@ decl_c_type!(
274294
/// Constructs an empty view slice.
275295
#[no_mangle]
276296
pub extern "C" fn z_view_slice_empty(this_: &mut MaybeUninit<z_view_slice_t>) {
277-
this_.as_rust_type_mut_uninit().write(CSliceView::default());
297+
this_
298+
.as_rust_type_mut_uninit()
299+
.write(CSliceView::gravestone());
278300
}
279301

280302
/// Constructs a `len` bytes long view starting at `start`.
@@ -294,7 +316,7 @@ pub unsafe extern "C" fn z_view_slice_from_buf(
294316
result::Z_OK
295317
}
296318
Err(e) => {
297-
this.write(CSliceView::default());
319+
this.write(CSliceView::gravestone());
298320
e
299321
}
300322
}
@@ -317,7 +339,7 @@ pub extern "C" fn z_view_slice_is_empty(this_: &z_view_slice_t) -> bool {
317339
pub extern "C" fn z_slice_empty(this_: &mut MaybeUninit<z_owned_slice_t>) {
318340
this_
319341
.as_rust_type_mut_uninit()
320-
.write(CSliceOwned::default());
342+
.write(CSliceOwned::gravestone());
321343
}
322344

323345
/// Constructs an empty `z_owned_slice_t`.
@@ -387,7 +409,7 @@ pub unsafe extern "C" fn z_slice_copy_from_buf(
387409
result::Z_OK
388410
}
389411
Err(e) => {
390-
this.write(CSliceOwned::default());
412+
this.write(CSliceOwned::gravestone());
391413
e
392414
}
393415
}
@@ -417,7 +439,7 @@ pub unsafe extern "C" fn z_slice_from_buf(
417439
result::Z_OK
418440
}
419441
Err(e) => {
420-
this.write(CSliceOwned::default());
442+
this.write(CSliceOwned::gravestone());
421443
e
422444
}
423445
}
@@ -427,13 +449,38 @@ pub use crate::opaque_types::{
427449
z_loaned_string_t, z_moved_string_t, z_owned_string_t, z_view_string_t,
428450
};
429451

430-
#[derive(Default, Clone)]
452+
#[derive(Clone)]
431453
pub struct CString(CSlice);
432-
#[derive(Default)]
433454
pub struct CStringOwned(CString);
434-
#[derive(Default)]
435455
pub struct CStringView(CString);
436456

457+
impl Gravestone for CString {
458+
fn gravestone() -> Self {
459+
Self(CSlice::gravestone())
460+
}
461+
fn is_gravestone(&self) -> bool {
462+
self.0.is_gravestone()
463+
}
464+
}
465+
466+
impl Gravestone for CStringOwned {
467+
fn gravestone() -> Self {
468+
Self(CString::gravestone())
469+
}
470+
fn is_gravestone(&self) -> bool {
471+
self.0.is_gravestone()
472+
}
473+
}
474+
475+
impl Gravestone for CStringView {
476+
fn gravestone() -> Self {
477+
Self(CString::gravestone())
478+
}
479+
fn is_gravestone(&self) -> bool {
480+
self.0.is_gravestone()
481+
}
482+
}
483+
437484
impl CString {
438485
pub fn new_borrowed_from_slice(slice: &[u8]) -> Self {
439486
CString(CSlice::new_borrowed_from_slice(slice))
@@ -551,7 +598,7 @@ pub extern "C" fn z_internal_string_check(this_: &z_owned_string_t) -> bool {
551598
pub extern "C" fn z_internal_string_null(this_: &mut MaybeUninit<z_owned_string_t>) {
552599
this_
553600
.as_rust_type_mut_uninit()
554-
.write(CStringOwned::default());
601+
.write(CStringOwned::gravestone());
555602
}
556603

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

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

581628
/// Borrows string.
@@ -619,7 +666,7 @@ pub unsafe extern "C" fn z_string_copy_from_substr(
619666
result::Z_OK
620667
}
621668
Err(e) => {
622-
this.write(CStringOwned::default());
669+
this.write(CStringOwned::gravestone());
623670
e
624671
}
625672
}
@@ -646,7 +693,7 @@ pub unsafe extern "C" fn z_string_from_str(
646693
result::Z_OK
647694
}
648695
Err(e) => {
649-
this.write(CStringOwned::default());
696+
this.write(CStringOwned::gravestone());
650697
e
651698
}
652699
}
@@ -668,7 +715,7 @@ pub unsafe extern "C" fn z_view_string_from_str(
668715
result::Z_OK
669716
}
670717
Err(e) => {
671-
this.write(CStringView::default());
718+
this.write(CStringView::gravestone());
672719
e
673720
}
674721
}
@@ -691,7 +738,7 @@ pub unsafe extern "C" fn z_view_string_from_substr(
691738
result::Z_OK
692739
}
693740
Err(e) => {
694-
this.write(CStringView::default());
741+
this.write(CStringView::gravestone());
695742
e
696743
}
697744
}
@@ -741,10 +788,19 @@ decl_c_type!(
741788
loaned(z_loaned_string_array_t),
742789
);
743790

791+
impl Gravestone for ZVector {
792+
fn gravestone() -> Self {
793+
Vec::new()
794+
}
795+
fn is_gravestone(&self) -> bool {
796+
self.is_empty()
797+
}
798+
}
799+
744800
/// Constructs a new empty string array.
745801
#[no_mangle]
746802
pub extern "C" fn z_string_array_new(this_: &mut MaybeUninit<z_owned_string_array_t>) {
747-
this_.as_rust_type_mut_uninit().write(ZVector::new());
803+
this_.as_rust_type_mut_uninit().write(ZVector::gravestone());
748804
}
749805

750806
/// Constructs string array in its gravestone state.

src/commons.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::transmute::IntoCType;
3535
use crate::z_moved_source_info_t;
3636
use crate::{
3737
result,
38-
transmute::{CTypeRef, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
38+
transmute::{CTypeRef, Gravestone, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
3939
z_id_t, z_loaned_bytes_t, z_loaned_encoding_t, z_loaned_keyexpr_t, z_loaned_session_t,
4040
};
4141

@@ -595,6 +595,15 @@ decl_c_type!(
595595
loaned(z_loaned_source_info_t, SourceInfo),
596596
);
597597

598+
impl Gravestone for SourceInfo {
599+
fn gravestone() -> Self {
600+
SourceInfo::default()
601+
}
602+
fn is_gravestone(&self) -> bool {
603+
self.source_id().is_none() && self.source_sn().is_none()
604+
}
605+
}
606+
598607
#[cfg(feature = "unstable")]
599608
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
600609
/// @brief Creates source info.

src/encoding.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use zenoh::bytes::Encoding;
2727
pub use crate::opaque_types::{z_loaned_encoding_t, z_owned_encoding_t};
2828
use crate::{
2929
result::{self, z_result_t},
30-
transmute::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
30+
transmute::{Gravestone, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
3131
z_moved_encoding_t, z_owned_string_t, z_string_copy_from_substr,
3232
};
3333

@@ -36,6 +36,15 @@ decl_c_type!(
3636
loaned(z_loaned_encoding_t, Encoding),
3737
);
3838

39+
impl Gravestone for Encoding {
40+
fn gravestone() -> Self {
41+
Encoding::default()
42+
}
43+
fn is_gravestone(&self) -> bool {
44+
self == &Self::default()
45+
}
46+
}
47+
3948
/// Constructs a `z_owned_encoding_t` from a specified substring.
4049
#[no_mangle]
4150
#[allow(clippy::missing_safety_doc)]

src/get.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use zenoh::{
2929
pub use crate::opaque_types::{z_loaned_reply_err_t, z_moved_reply_err_t, z_owned_reply_err_t};
3030
use crate::{
3131
result,
32-
transmute::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
32+
transmute::{Gravestone, LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
3333
z_closure_reply_call, z_closure_reply_loan, z_congestion_control_t, z_consolidation_mode_t,
3434
z_loaned_bytes_t, z_loaned_encoding_t, z_loaned_keyexpr_t, z_loaned_sample_t,
3535
z_loaned_session_t, z_moved_bytes_t, z_moved_closure_reply_t, z_moved_encoding_t, z_priority_t,
@@ -45,17 +45,28 @@ decl_c_type!(
4545
loaned(z_loaned_reply_err_t, ReplyError),
4646
);
4747

48+
impl Gravestone for ReplyError {
49+
fn gravestone() -> Self {
50+
ReplyError::empty()
51+
}
52+
fn is_gravestone(&self) -> bool {
53+
self.payload().is_empty()
54+
}
55+
}
56+
4857
/// Constructs an empty `z_owned_reply_err_t`.
4958
#[no_mangle]
5059
pub extern "C" fn z_internal_reply_err_null(this_: &mut MaybeUninit<z_owned_reply_err_t>) {
51-
this_.as_rust_type_mut_uninit().write(ReplyError::default());
60+
this_
61+
.as_rust_type_mut_uninit()
62+
.write(ReplyError::gravestone());
5263
}
5364

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

6172
/// Returns reply error payload.

0 commit comments

Comments
 (0)