@@ -25,7 +25,7 @@ use libc::strlen;
25
25
26
26
use crate :: {
27
27
result:: { self , z_result_t} ,
28
- transmute:: { LoanedCTypeRef , RustTypeRef , RustTypeRefUninit , TakeRustType } ,
28
+ transmute:: { Gravestone , LoanedCTypeRef , RustTypeRef , RustTypeRefUninit , TakeRustType } ,
29
29
} ;
30
30
31
31
pub struct CSlice {
@@ -42,11 +42,28 @@ pub extern "C" fn _z_drop_c_slice_default(data: *mut c_void, context: *mut c_voi
42
42
std:: mem:: drop ( b) ;
43
43
}
44
44
45
- #[ derive( Default , Clone ) ]
45
+ #[ derive( Clone ) ]
46
46
pub struct CSliceOwned ( CSlice ) ;
47
- #[ derive( Default ) ]
48
47
pub struct CSliceView ( CSlice ) ;
49
48
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
+
50
67
impl Deref for CSliceOwned {
51
68
type Target = CSlice ;
52
69
fn deref ( & self ) -> & Self :: Target {
@@ -158,7 +175,7 @@ impl CSlice {
158
175
#[ allow( clippy:: missing_safety_doc) ]
159
176
pub unsafe fn new_owned_unchecked ( data : * const u8 , len : usize ) -> Self {
160
177
if len == 0 {
161
- return Self :: default ( ) ;
178
+ return Self :: gravestone ( ) ;
162
179
}
163
180
let b = unsafe { from_raw_parts ( data, len) . to_vec ( ) . into_boxed_slice ( ) } ;
164
181
let slice = Box :: leak ( b) ;
@@ -216,15 +233,18 @@ impl Clone for CSlice {
216
233
}
217
234
}
218
235
219
- impl Default for CSlice {
220
- fn default ( ) -> Self {
236
+ impl Gravestone for CSlice {
237
+ fn gravestone ( ) -> Self {
221
238
Self {
222
239
data : null ( ) ,
223
240
len : 0 ,
224
241
drop : None ,
225
242
context : null_mut ( ) ,
226
243
}
227
244
}
245
+ fn is_gravestone ( & self ) -> bool {
246
+ self . data . is_null ( )
247
+ }
228
248
}
229
249
230
250
impl Drop for CSlice {
@@ -274,7 +294,9 @@ decl_c_type!(
274
294
/// Constructs an empty view slice.
275
295
#[ no_mangle]
276
296
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 ( ) ) ;
278
300
}
279
301
280
302
/// Constructs a `len` bytes long view starting at `start`.
@@ -294,7 +316,7 @@ pub unsafe extern "C" fn z_view_slice_from_buf(
294
316
result:: Z_OK
295
317
}
296
318
Err ( e) => {
297
- this. write ( CSliceView :: default ( ) ) ;
319
+ this. write ( CSliceView :: gravestone ( ) ) ;
298
320
e
299
321
}
300
322
}
@@ -317,7 +339,7 @@ pub extern "C" fn z_view_slice_is_empty(this_: &z_view_slice_t) -> bool {
317
339
pub extern "C" fn z_slice_empty ( this_ : & mut MaybeUninit < z_owned_slice_t > ) {
318
340
this_
319
341
. as_rust_type_mut_uninit ( )
320
- . write ( CSliceOwned :: default ( ) ) ;
342
+ . write ( CSliceOwned :: gravestone ( ) ) ;
321
343
}
322
344
323
345
/// Constructs an empty `z_owned_slice_t`.
@@ -387,7 +409,7 @@ pub unsafe extern "C" fn z_slice_copy_from_buf(
387
409
result:: Z_OK
388
410
}
389
411
Err ( e) => {
390
- this. write ( CSliceOwned :: default ( ) ) ;
412
+ this. write ( CSliceOwned :: gravestone ( ) ) ;
391
413
e
392
414
}
393
415
}
@@ -417,7 +439,7 @@ pub unsafe extern "C" fn z_slice_from_buf(
417
439
result:: Z_OK
418
440
}
419
441
Err ( e) => {
420
- this. write ( CSliceOwned :: default ( ) ) ;
442
+ this. write ( CSliceOwned :: gravestone ( ) ) ;
421
443
e
422
444
}
423
445
}
@@ -427,13 +449,38 @@ pub use crate::opaque_types::{
427
449
z_loaned_string_t, z_moved_string_t, z_owned_string_t, z_view_string_t,
428
450
} ;
429
451
430
- #[ derive( Default , Clone ) ]
452
+ #[ derive( Clone ) ]
431
453
pub struct CString ( CSlice ) ;
432
- #[ derive( Default ) ]
433
454
pub struct CStringOwned ( CString ) ;
434
- #[ derive( Default ) ]
435
455
pub struct CStringView ( CString ) ;
436
456
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
+
437
484
impl CString {
438
485
pub fn new_borrowed_from_slice ( slice : & [ u8 ] ) -> Self {
439
486
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 {
551
598
pub extern "C" fn z_internal_string_null ( this_ : & mut MaybeUninit < z_owned_string_t > ) {
552
599
this_
553
600
. as_rust_type_mut_uninit ( )
554
- . write ( CStringOwned :: default ( ) ) ;
601
+ . write ( CStringOwned :: gravestone ( ) ) ;
555
602
}
556
603
557
604
/// @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 {
566
613
pub unsafe extern "C" fn z_string_empty ( this_ : & mut MaybeUninit < z_owned_string_t > ) {
567
614
this_
568
615
. as_rust_type_mut_uninit ( )
569
- . write ( CStringOwned :: default ( ) ) ;
616
+ . write ( CStringOwned :: gravestone ( ) ) ;
570
617
}
571
618
572
619
/// Constructs an empty view string.
@@ -575,7 +622,7 @@ pub unsafe extern "C" fn z_string_empty(this_: &mut MaybeUninit<z_owned_string_t
575
622
pub unsafe extern "C" fn z_view_string_empty ( this_ : & mut MaybeUninit < z_view_string_t > ) {
576
623
this_
577
624
. as_rust_type_mut_uninit ( )
578
- . write ( CStringView :: default ( ) ) ;
625
+ . write ( CStringView :: gravestone ( ) ) ;
579
626
}
580
627
581
628
/// Borrows string.
@@ -619,7 +666,7 @@ pub unsafe extern "C" fn z_string_copy_from_substr(
619
666
result:: Z_OK
620
667
}
621
668
Err ( e) => {
622
- this. write ( CStringOwned :: default ( ) ) ;
669
+ this. write ( CStringOwned :: gravestone ( ) ) ;
623
670
e
624
671
}
625
672
}
@@ -646,7 +693,7 @@ pub unsafe extern "C" fn z_string_from_str(
646
693
result:: Z_OK
647
694
}
648
695
Err ( e) => {
649
- this. write ( CStringOwned :: default ( ) ) ;
696
+ this. write ( CStringOwned :: gravestone ( ) ) ;
650
697
e
651
698
}
652
699
}
@@ -668,7 +715,7 @@ pub unsafe extern "C" fn z_view_string_from_str(
668
715
result:: Z_OK
669
716
}
670
717
Err ( e) => {
671
- this. write ( CStringView :: default ( ) ) ;
718
+ this. write ( CStringView :: gravestone ( ) ) ;
672
719
e
673
720
}
674
721
}
@@ -691,7 +738,7 @@ pub unsafe extern "C" fn z_view_string_from_substr(
691
738
result:: Z_OK
692
739
}
693
740
Err ( e) => {
694
- this. write ( CStringView :: default ( ) ) ;
741
+ this. write ( CStringView :: gravestone ( ) ) ;
695
742
e
696
743
}
697
744
}
@@ -741,10 +788,19 @@ decl_c_type!(
741
788
loaned( z_loaned_string_array_t) ,
742
789
) ;
743
790
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
+
744
800
/// Constructs a new empty string array.
745
801
#[ no_mangle]
746
802
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 ( ) ) ;
748
804
}
749
805
750
806
/// Constructs string array in its gravestone state.
0 commit comments