-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
921 lines (874 loc) · 31.8 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
/*! [`FamBox`] datastructure to enable ergonomic and safe usage of c structs with a [flexible array member](https://en.wikipedia.org/wiki/Flexible_array_member).
[`FamBox`] comes in several flavors:
- [`FamBoxOwned`] for owned data.
- [`FamBoxMut`] for exclusively referenced data.
- [`FamBoxShared`] for aliased data.
Say you have this c struct:
```c
struct protocol_msg {
uint8_t version; /* Protocol version */
uint8_t ty; /* Protocol message type */
uint16_t length; /* Length in bytes including the flexible array member. */
uint8_t elements[0]; /* msg data */
};
```
which [bindgen](https://crates.io/crates/bindgen) will translate to
```rust
# struct __IncompleteArrayField<T>([T;0]);
#[repr(C)]
pub struct protocol_msg {
#[doc = " Protocol version"]
version: u8,
#[doc = " Protocol message type"]
ty: u8,
#[doc = " Length in bytes including the flexible array member."]
length: u16,
#[doc = " msg data"]
elements: __IncompleteArrayField<u8>,
}
```
A [`FamBoxOwned`] can be used to safely construct a new instance of this type.
```rust
# #![allow(non_camel_case_types)]
use core::mem::size_of;
use fambox::FamBoxOwned;
#
// To be used safely, [`protocol_msg`] can't provide any safe mutable access to `length`.
pub use encapsulated_struct::protocol_msg;
mod encapsulated_struct {
# use hidden::*;
# pub mod hidden {
# #[derive(Debug)]
# pub struct Error;
# #[derive(Debug, PartialEq)]
# pub struct __IncompleteArrayField<T>([T;0]);
# impl<T> __IncompleteArrayField<T> {
# pub fn new() -> Self {
# Self([])
# }
# }
# }
use core::mem::size_of;
use fambox::FamHeader;
# #[repr(C)]
# #[derive(Debug, PartialEq)]
# pub struct protocol_msg {
# #[doc = " Protocol version"]
# pub version: u8,
# #[doc = " Protocol message type"]
# pub ty: u8,
# #[doc = " Length in bytes including the flexible array member."]
# length: u16,
# #[doc = " msg data"]
# elements: __IncompleteArrayField<u8>,
# }
#
pub struct InvalidLength;
# impl From<InvalidLength> for Error {
# fn from(_: InvalidLength) -> Self {
# Error
# }
# }
#
impl protocol_msg {
pub fn new(version: u8, ty: u8, buffer_len: usize) -> Result<Self, InvalidLength> {
Ok(Self {
version,
ty,
length: (size_of::<Self>()
+ buffer_len * size_of::<<Self as FamHeader>::Element>())
.try_into()
.or(Err(InvalidLength))?,
elements: __IncompleteArrayField::new(),
})
}
pub fn length(&self) -> &u16 {
&self.length
}
}
/// Safety:
/// `protocol_msg` doesn't expose a mutable setter which would make the length inconsistent.
unsafe impl FamHeader for protocol_msg {
type Element = u8;
fn fam_len(&self) -> usize {
let bytes_in_fam = usize::from(self.length) - size_of::<Self>();
bytes_in_fam / size_of::<Self::Element>()
}
}
}
# fn test() -> Result<(), encapsulated_struct::hidden::Error> {
let data_buffer = [0, 1, 2, 3, 4, 5];
let header = encapsulated_struct::protocol_msg::new(1, 2, data_buffer.len())?;
let header_and_fam = FamBoxOwned::from_fn(header, |i| data_buffer[i]);
let header = encapsulated_struct::protocol_msg::new(1, 2, data_buffer.len())?;
assert_eq!(header_and_fam.header(), &header);
assert_eq!(header_and_fam.fam(), data_buffer);
assert_eq!(
usize::from(*header_and_fam.header().length()),
size_of::<protocol_msg>() + core::mem::size_of_val(&data_buffer)
);
# Ok(())
# }
# test().unwrap()
```
or a [`FamBoxShared`]/[`FamBoxMut`] can be used to easily access and manipulate a fam containing struct from c
```rust,no_run
# use core::ptr::NonNull;
# use fambox::{FamBoxShared, FamBoxMut, FamHeader};
#
# #[repr(C)]
# #[derive(Debug, PartialEq)]
# struct __IncompleteArrayField<T>([T; 0]);
# #[repr(C)]
# #[derive(Debug, PartialEq)]
# pub struct protocol_msg {
# #[doc = " Protocol version"]
# version: u8,
# #[doc = " Protocol message type"]
# ty: u8,
# #[doc = " Length in bytes including the flexible array member."]
# length: u16,
# #[doc = " msg data"]
# elements: __IncompleteArrayField<u8>,
# }
# unsafe impl FamHeader for protocol_msg {
# type Element = u8;
#
# fn fam_len(&self) -> usize {
# let bytes_in_fam = usize::from(self.length) - core::mem::size_of::<Self>();
# bytes_in_fam / core::mem::size_of::<Self::Element>()
# }
# }
extern "C" {
fn original_header() -> protocol_msg;
fn original_data() -> *const u8;
fn original_data_len() -> usize;
fn aliased_ptr_from_c() -> NonNull<protocol_msg>;
fn alloc_in_c() -> NonNull<protocol_msg>;
fn free_in_c(ptr: *mut protocol_msg);
}
let header_and_fam = unsafe { FamBoxShared::from_raw(aliased_ptr_from_c()) };
assert_eq!(header_and_fam.as_parts(), unsafe {
(&original_header(), unsafe { core::slice::from_raw_parts(original_data(), original_data_len()) })
});
let ptr = unsafe { alloc_in_c() };
let mut header_and_fam = unsafe { FamBoxMut::from_raw(ptr) };
assert_eq!(header_and_fam.as_parts(), unsafe {
(&original_header(), unsafe { core::slice::from_raw_parts(original_data(), original_data_len()) })
});
header_and_fam.fam_mut()[2] = 10;
drop(header_and_fam);
unsafe { free_in_c(ptr.as_ptr()) }
```
# Feature Flags
- `serde`: provide `Serialize` and `Deserialize` implementations for `FamBoxOwned`
*/
// Enable `cargo +nightly doc --all-features` to show all items and their feature requirements.
// `doccfg` is set by the `build.rs` when supported (nightly)
#![cfg_attr(doccfg, feature(doc_auto_cfg))]
#![cfg_attr(doccfg, feature(doc_cfg))]
#![no_std]
#[cfg(feature = "serde")]
mod serde;
/// Builder to create a new `FamBoxOwned` 1 element at a time.
// Also repurposed for its destructor
mod builder;
pub use builder::FamBoxBuilder;
extern crate alloc;
use core::{
any,
marker::PhantomData,
mem::{self, align_of, align_of_val, size_of, size_of_val, ManuallyDrop},
ops::ControlFlow,
ptr::NonNull,
};
#[repr(C)]
#[derive(Debug)]
struct __IncompleteArrayField<T>([T; 0]);
impl<T> __IncompleteArrayField<T> {
#[allow(dead_code)]
const fn new() -> Self {
Self([])
}
}
/// A trait to enable usage of a struct which contains a [flexible array member](https://en.wikipedia.org/wiki/Flexible_array_member) in a [`FamBox`].
/// The struct will have some metadata which enables knowing how large the flexible array member is.
/// # Safety:
/// 1. The implementation for `fam_len` and `total_size` must be
/// - consistent with each other
/// - consistent across calls
///
/// or else undefined behavior may occur.
///
/// 2. Additionally, if `p` is a valid pointer to `H` then `p` + `size_of::<H>` must be a valid pointer to an `H::Element`.
/// This can be handled by placing a bindgen generated `__IncompleteArrayField<H::Element>` or [`H::Element; 0`] as the last field of the `repr(C)` `H`.
pub unsafe trait FamHeader {
/// The element type of the flexible array member.
type Element;
/// The length, in `size_of::<H::Element>()` increments, of the flexible array member.
fn fam_len(&self) -> usize;
/// The total size of this struct, in bytes, including the header and the flexible array member.
#[inline]
fn total_size(&self) -> usize {
debug_assert_eq!(
(align_of_val(&self) + size_of_val(&self)) % align_of::<Self::Element>(),
0,
"FamHeader implemented for {} but its align + size doesn't match trait requirement 2",
any::type_name_of_val(&self)
);
self.fam_len() * mem::size_of::<Self::Element>() + mem::size_of_val(self)
}
}
mod sealed {
pub trait Sealed {}
impl Sealed for super::Owned {}
impl<'a> Sealed for super::BorrowedMut<'a> {}
impl<'a> Sealed for super::BorrowedShared<'a> {}
}
/// The [`FamBox`] owns its buffer and will deallocate it when dropped.
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Owned;
#[derive(Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
/// The [`FamBox`] has an exclusive reference to its buffer.
pub struct BorrowedMut<'a>(core::marker::PhantomData<&'a ()>);
// Don't write Phantom.
impl<'a> core::fmt::Debug for BorrowedMut<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BorrowedMut").finish()
}
}
#[derive(Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
/// The [`FamBox`] has a shared reference to its buffer.
pub struct BorrowedShared<'a>(core::marker::PhantomData<&'a ()>);
// Don't write Phantom.
impl<'a> core::fmt::Debug for BorrowedShared<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BorrowedShared").finish()
}
}
/// Sealed Marker trait representing if a buffer is [`Owned`] or [`BorrowedMut`] or [`BorrowedShared`].
pub trait Owner: sealed::Sealed {
const OWNED: bool;
}
/// Sealed Marker trait representing that the buffer is exclusive.
pub trait Exclusive: Owner {}
impl Owner for Owned {
const OWNED: bool = true;
}
impl Exclusive for Owned {}
impl<'a> Owner for BorrowedMut<'a> {
const OWNED: bool = false;
}
impl<'a> Exclusive for BorrowedMut<'a> {}
impl<'a> Owner for BorrowedShared<'a> {
const OWNED: bool = false;
}
/// Sealed Marker trait representing that the buffer is borrowed from somewhere else.
pub trait Borrowed: Owner {}
impl<'a> Borrowed for BorrowedMut<'a> {}
impl<'a> Borrowed for BorrowedShared<'a> {}
/// A smartpointer to a buffer which contains a [`FamHeader`] followed by a flexible array member in a continuous allocation.
pub struct FamBox<H: FamHeader, O: Owner> {
// Pointer to backing buffer including fam.
ptr: NonNull<u8>,
// Type markers
ty: PhantomData<(H, O)>,
}
/** [`Owned`] impls */
impl<H: FamHeader + Clone> Clone for FamBox<H, Owned>
where
H::Element: Clone,
{
/// The buffer, including the header and fam, is copied into a new allocation.
#[inline]
fn clone(&self) -> Self {
self.into_owned()
}
}
/** [`Owned`] impls */
impl<H: FamHeader + Default> Default for FamBox<H, Owned>
where
H::Element: Default,
{
fn default() -> Self {
Self::from_fn(H::default(), |_| H::Element::default())
}
}
/** [`Owned`] impls */
impl<H: FamHeader + PartialEq> PartialEq for FamBox<H, Owned>
where
H::Element: PartialEq,
{
/// Compares parts.
fn eq(&self, other: &Self) -> bool {
self.as_parts() == other.as_parts()
}
}
/** [`Owned`] impls */
impl<H: FamHeader> FamBox<H, Owned> {
/// Allocate a new [`Owned`] buffer and create [`Self`] from a valid header and a callback for initializing the flexible array member.
///
/// `cb` is passed the index being initialized.
pub fn from_fn<F>(header: H, mut cb: F) -> Self
where
F: FnMut(usize) -> H::Element,
{
// By using the builder the memory will be correctly freed in the case of a `cb` panic.
let mut builder = match FamBoxBuilder::new(header) {
ControlFlow::Continue(builder) => builder,
ControlFlow::Break(x) => return x.build(),
};
let mut i = 0;
loop {
builder = match builder.add_element(cb(i)) {
ControlFlow::Continue(unfinished) => unfinished,
ControlFlow::Break(finished) => return finished.build(),
};
i += 1;
}
}
/// Leak [`Self`].
pub fn leak(self) -> NonNull<H> {
ManuallyDrop::new(self).ptr.cast()
}
}
/** [`BorrowedMut`] impls */
impl<'a, H: FamHeader> FamBox<H, BorrowedMut<'a>> {
/// Create a [`FamBoxMut<H>`] from a reference to a buffer containing `H` followed by `H::Element` of `H::fam_len()` length.
/// # Safety
/// - `slice` must match the above description (i.e. aligned to `H`, containing `H` followed by `H::fam_len()` `H::Element`s).
#[inline]
pub unsafe fn from_slice_mut(slice: &'a mut [u8]) -> Self {
// Check that either the alignment is correct or `align_offset()` gave up.
debug_assert!(
matches!(slice.as_ptr().align_offset(align_of::<H>()), 0 | usize::MAX),
"alignment doesn't match H"
);
Self {
ty: PhantomData,
ptr: NonNull::from(slice).cast(),
}
}
}
/** [`BorrowedShared`] impls */
impl<'a, H: FamHeader> Clone for FamBox<H, BorrowedShared<'a>> {
/// Since the buffer is shared, clones the pointer but not the underlying buffer.
#[inline]
fn clone(&self) -> Self {
Self {
ptr: self.ptr,
ty: self.ty,
}
}
}
/** [`BorrowedShared`] impls */
impl<'a, H: FamHeader> FamBox<H, BorrowedShared<'a>> {
/// Create a [`FamBoxShared<H>`] from a reference to a buffer containing `H` followed by `H::Element` of `H::fam_len()` length.
/// # Safety
/// - `slice` must match the above description (i.e. aligned to `H`, containing `H` followed by `H::fam_len()` `H::Element`s).
#[inline]
pub unsafe fn from_slice(slice: &'a [u8]) -> Self {
// Check that either the alignment is correct or `align_offset()` gave up.
debug_assert!(
matches!(slice.as_ptr().align_offset(align_of::<H>()), 0 | usize::MAX),
"alignment doesn't match H"
);
Self {
ty: PhantomData,
ptr: NonNull::from(slice).cast(),
}
}
}
/** [`Borrowed`] impls */
impl<H: FamHeader + PartialEq, O: Borrowed> PartialEq for FamBox<H, O>
where
H::Element: PartialEq,
{
/// Compares pointer addresses and if not equal compares parts.
fn eq(&self, other: &Self) -> bool {
self.ptr == other.ptr || self.as_parts() == other.as_parts()
}
}
/** [`Exclusive`] impls */
impl<H: FamHeader, O: Exclusive> FamBox<H, O> {
#[inline]
pub fn header_mut(&mut self) -> &mut H {
// Safety: The head of the buffer contains a valid `H` and buffer is [`Exclusive`].
unsafe { self.ptr.cast().as_mut() }
}
#[inline]
pub fn fam_mut(&mut self) -> &mut [H::Element] {
let fam_len = self.header().fam_len();
// Safety: By construction `self.ptr` has provenance over the entire buffer and `self.ptr+size_of::<H>()` is the start of the fam.
// Taking by `&mut self` on [`Exclusive`] buffer guarantees required exclusivity.
unsafe {
let fam = self.ptr.as_ptr().add(size_of::<H>()).cast();
core::slice::from_raw_parts_mut(fam, fam_len)
}
}
#[inline]
pub fn as_parts_mut(&mut self) -> (&mut H, &mut [H::Element]) {
// Safety: Inlined [`self.header_mut()`] to satisfy borrow checker.
// Have to get header pointer before `fam` because `fam` takes by `&mut self`.
// Have to get fam before `header.as_mut()` because `self.fam_mut()` reads header transiently.
let mut header = self.ptr.cast();
let fam = self.fam_mut();
(unsafe { header.as_mut() }, fam)
}
}
/** General impls for [`Owned`], [`BorrowedMut`], [`BorrowedShared`] */
impl<H: FamHeader + core::fmt::Debug, O: Owner + core::fmt::Debug + Default> core::fmt::Debug
for FamBox<H, O>
where
H::Element: core::fmt::Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("FamBox")
.field("ty", &O::default())
.field("header", self.header())
.field("fam", &self.fam())
.finish()
}
}
/** General impls for [`Owned`], [`BorrowedMut`], [`BorrowedShared`] */
impl<H: FamHeader, O: Owner> FamBox<H, O> {
/// Create [`Self`] from a pointer to a buffer containing `H` followed by `H::Element` of `H::fam_len()` length.
/// # Safety
/// - `ptr` must match the above description.
/// - the buffer must be valid for the lifetime of `Self`.
/// - `ptr` must be exclusive if `O: Exclusive`.
/// - `ptr` must have been allocated with [`alloc::alloc::GlobalAlloc`] if `O` = [`Owned`].
///
/// The pointer from [`Self::leak()`] satisfies all of these.
pub unsafe fn from_raw(ptr: NonNull<H>) -> Self {
Self {
ty: PhantomData,
ptr: ptr.cast(),
}
}
/// Copy to a [`FamBoxOwned<H>`] by copying the buffer into a newly allocated buffer.
// Can't implement `ToOwned` because of conflict with blanket `impl<T: Clone> ToOwned for T {}`
pub fn into_owned(&self) -> FamBox<H, Owned>
where
H: Clone,
H::Element: Clone,
{
let (header, fam) = self.as_parts();
FamBox::from_fn(header.clone(), |i| fam[i].clone())
}
/// Get a reference to the underlying buffer.
/// # Safety
/// - Only safe if `H` and `H::Element` don't have uninit bytes (e.g. padding).
#[inline]
pub unsafe fn buffer(&self) -> &[u8] {
// Safety: Valid len by contract of [`FamHeader`] trait.
unsafe { core::slice::from_raw_parts(self.ptr.as_ptr(), self.header().total_size()) }
}
#[inline]
pub fn header(&self) -> &H {
// Safety: The head of the buffer contains a valid `H`.
unsafe { self.ptr.cast().as_ref() }
}
#[inline]
pub fn fam(&self) -> &[H::Element] {
let fam_len = self.header().fam_len();
// Safety: By construction `self.ptr` has provenance over the entire buffer and `self.ptr+size_of::<H>()` is the start of the fam.
unsafe {
let fam = self.ptr.as_ptr().add(size_of::<H>()).cast();
core::slice::from_raw_parts(fam, fam_len)
}
}
#[inline]
pub fn as_parts(&self) -> (&H, &[H::Element]) {
// Safety: Inlined [`self.header()`] to satisfy borrow checker.
(unsafe { self.ptr.cast().as_ref() }, self.fam())
}
}
/** General impls for [`Owned`], [`BorrowedMut`], [`BorrowedShared`] */
impl<H: FamHeader, O: Owner> Drop for FamBox<H, O> {
#[inline]
fn drop(&mut self) {
// Only deallocate if the buffer is owned (created with the same underlying allocator).
// Would like to specialize the drop implementation, but that isn't currently possible in Rust.
// See <https://github.com/rust-lang/rust/issues/8142>
// and <https://internals.rust-lang.org/t/can-we-fix-drop-to-allow-specialization/12873/5>
// So instead do this and expect that `#[inline]` means the impl is optimized out.
if !O::OWNED {
return;
}
// Safety: self is valid and so self.ptr is a valid pointer to the buffer.
drop(unsafe { FamBoxBuilder::from_built(self.ptr.cast::<H>()) });
}
}
/* Send/Sync impls */
// Exclusive [`FamBox`] can be used to get `&H`/`&mut H`/`&H::Element`/`&mut H::Element`.
unsafe impl<H: FamHeader + Send> Send for FamBox<H, Owned> where H::Element: Send {}
unsafe impl<'a, H: FamHeader + Send> Send for FamBox<H, BorrowedMut<'a>> where H::Element: Send {}
// Exclusive [`&FamBox`] can be used to get `&H`/`&H::Element`.
unsafe impl<H: FamHeader + Sync> Sync for FamBox<H, Owned> where H::Element: Sync {}
unsafe impl<'a, H: FamHeader + Sync> Sync for FamBox<H, BorrowedMut<'a>> where H::Element: Sync {}
// Shared [`FamBox`] can be used to get `&H`/`&H::Element`.
unsafe impl<'a, H: FamHeader + Sync> Send for FamBox<H, BorrowedShared<'a>> where H::Element: Sync {}
// Shared [`&FamBox`] can be used to get `&H`/`&H::Element`.
unsafe impl<'a, H: FamHeader + Sync> Sync for FamBox<H, BorrowedShared<'a>> where H::Element: Sync {}
/// A [`FamBox`] which owns its buffer.
pub type FamBoxOwned<H> = FamBox<H, Owned>;
/// A [`FamBox`] which has exclusive access to a buffer.
pub type FamBoxMut<'a, H> = FamBox<H, BorrowedMut<'a>>;
/// A [`FamBox`] which has shared access to a buffer.
pub type FamBoxShared<'a, H> = FamBox<H, BorrowedShared<'a>>;
#[cfg(test)]
mod tests {
use super::*;
use alloc::{rc::Rc, string::String};
use core::{
cell::Cell, fmt::Write, ops::ControlFlow, panic::AssertUnwindSafe, sync::atomic::AtomicBool,
};
/// Test struct.
#[repr(C)]
#[derive(Debug)]
struct MsgHeader {
this: u128,
/// Bytes in this message.
/// If this is changed may cause undefined behavior so it should only be accessible through a &self getter.
len: u64,
that: u8,
val: __IncompleteArrayField<u16>,
}
/// Same as MsgHeader but no padding
#[repr(C)]
#[derive(Debug)]
struct MsgNoPadding {
this: u128, // 16
len: u64, // 22
that: u8, // 23
padding: [u8; 7], // 30
val: __IncompleteArrayField<u16>, // 32
}
impl Clone for MsgHeader {
fn clone(&self) -> Self {
Self {
this: self.this.clone(),
len: self.len.clone(),
that: self.that.clone(),
val: __IncompleteArrayField::new(),
}
}
}
impl PartialEq for MsgHeader {
fn eq(&self, other: &Self) -> bool {
self.this == other.this && self.len == other.len && self.that == other.that
}
}
impl Eq for MsgHeader {}
// Safety: matches trait contract
unsafe impl FamHeader for MsgHeader {
type Element = u16;
#[inline]
fn fam_len(&self) -> usize {
let bytes_in_fam = self.len as usize - size_of::<Self>();
bytes_in_fam / size_of::<u16>()
}
}
impl Clone for MsgNoPadding {
fn clone(&self) -> Self {
Self {
this: self.this.clone(),
len: self.len.clone(),
that: self.that.clone(),
padding: self.padding.clone(),
val: __IncompleteArrayField::new(),
}
}
}
impl PartialEq for MsgNoPadding {
fn eq(&self, other: &Self) -> bool {
self.this == other.this && self.len == other.len && self.that == other.that
}
}
impl Eq for MsgNoPadding {}
// Safety: matches trait contract
unsafe impl FamHeader for MsgNoPadding {
type Element = u16;
#[inline]
fn fam_len(&self) -> usize {
let bytes_in_fam = self.len as usize - size_of::<Self>();
bytes_in_fam / size_of::<u16>()
}
}
const TEST_MSG: MsgHeader = MsgHeader {
this: 42,
len: size_of::<MsgHeader>() as u64 + 13,
that: 55,
val: __IncompleteArrayField::new(),
};
const TEST_MSG_NO_PADDING: MsgNoPadding = MsgNoPadding {
this: 42,
len: size_of::<MsgHeader>() as u64 + 13,
that: 55,
padding: [0; 7],
val: __IncompleteArrayField::new(),
};
unsafe fn copy_for_test(buffer: &[u8]) -> &'static mut [u8] {
let layout =
alloc::alloc::Layout::from_size_align(buffer.len(), align_of::<MsgHeader>()).unwrap();
let ptr = alloc::alloc::alloc(layout);
core::ptr::copy(buffer.as_ptr(), ptr, buffer.len());
core::slice::from_raw_parts_mut(ptr, buffer.len())
}
unsafe fn free_for_test(ptr: *mut u8, len: usize) {
let layout = alloc::alloc::Layout::from_size_align(len, align_of::<MsgHeader>()).unwrap();
alloc::alloc::dealloc(ptr, layout);
}
#[test]
fn fambox_builder() {
let ControlFlow::Continue(mut builder) = FamBoxBuilder::new(TEST_MSG) else {
panic!("done early")
};
let mut i = 0;
let mut next = builder.add_element(0);
while let ControlFlow::Continue(x) = next {
i += 1;
builder = x;
next = builder.add_element(i);
}
let ControlFlow::Break(x) = next else {
panic!("loop ended")
};
x.build();
}
#[test]
fn fambox_builder_no_elements() {
struct H(u8, [u32; 0]);
unsafe impl FamHeader for H {
type Element = u32;
fn fam_len(&self) -> usize {
self.0.into()
}
}
assert!(FamBoxBuilder::new(H(0, [])).is_break())
}
#[test]
fn fambox_builder_zst_header_and_some_elements() {
struct H([u32; 0]);
unsafe impl FamHeader for H {
type Element = u32;
fn fam_len(&self) -> usize {
5
}
}
assert!(FamBoxBuilder::new(H([])).is_continue())
}
#[test]
fn fambox_builder_zst() {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ZST;
unsafe impl FamHeader for ZST {
type Element = ();
fn fam_len(&self) -> usize {
0
}
}
let ControlFlow::Break(builder) = FamBoxBuilder::new(ZST) else {
panic!("done late")
};
let fambox = builder.build();
drop(fambox);
}
#[test]
fn fambox_builder_drop() {
let ControlFlow::Continue(builder) = FamBoxBuilder::new(TEST_MSG) else {
panic!("done early")
};
let next = builder.add_element(0);
drop(next)
}
#[test]
fn parts_with_padding() {
let own = FamBox::from_fn(TEST_MSG, |i| i as _);
let (header, fam) = own.as_parts();
assert_eq!(*header, TEST_MSG);
assert_eq!(fam, core::array::from_fn::<_, 6, _>(|i| i as _));
}
#[test]
fn parts_eq() {
let own = FamBox::from_fn(TEST_MSG_NO_PADDING, |i| i as _);
let share = unsafe { FamBox::<MsgNoPadding, _>::from_slice(own.buffer()) };
// Copy the buffer to a new buffer with a valid size/alignment to test exclusive shared.
let buffer_mut = unsafe { copy_for_test(own.buffer()) };
let exl = unsafe { FamBox::<MsgNoPadding, _>::from_slice_mut(buffer_mut) };
assert_eq!(own.as_parts(), share.as_parts());
assert_eq!(own.as_parts(), exl.as_parts());
// Don't forget to free manually allocated memory.
drop(exl);
unsafe { free_for_test(buffer_mut.as_mut_ptr(), buffer_mut.len()) };
}
#[test]
fn debug_impls() {
let own = FamBox::from_fn(TEST_MSG_NO_PADDING, |i| i as _);
let share = unsafe { FamBox::<MsgNoPadding, _>::from_slice(own.buffer()) };
let buffer_mut = unsafe { copy_for_test(own.buffer()) };
let exl = unsafe { FamBox::<MsgNoPadding, _>::from_slice_mut(buffer_mut) };
let mut s = String::new();
writeln!(s, "{own:?}").unwrap();
writeln!(s, "{share:?}").unwrap();
writeln!(s, "{exl:?}").unwrap();
drop(exl);
unsafe { free_for_test(buffer_mut.as_mut_ptr(), buffer_mut.len()) };
}
#[test]
fn clone_impls() {
let own = FamBox::from_fn(TEST_MSG_NO_PADDING, |i| i as _);
let share = unsafe { FamBox::<MsgNoPadding, _>::from_slice(own.buffer()) };
assert_eq!(own.clone().as_parts(), share.clone().as_parts());
assert_eq!(unsafe { own.clone().buffer() }, unsafe {
share.clone().buffer()
});
assert_eq!(own, own.clone());
assert_eq!(share, share.clone());
}
#[test]
fn zst() {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ZST;
unsafe impl FamHeader for ZST {
type Element = ();
fn fam_len(&self) -> usize {
0
}
}
let own = FamBox::from_fn(ZST, |_| ());
let share = unsafe { FamBox::<ZST, _>::from_slice(own.buffer()) };
let mut buf = unsafe { own.buffer() }.to_vec();
let exl = unsafe { FamBox::<ZST, _>::from_slice_mut(&mut buf) };
assert_eq!(unsafe { own.buffer() }.len(), 0);
assert_eq!(own.as_parts(), share.as_parts());
assert_eq!(own.as_parts(), exl.as_parts());
let mut s = String::new();
writeln!(s, "{own:?}").unwrap();
writeln!(s, "{share:?}").unwrap();
writeln!(s, "{exl:?}").unwrap();
}
#[test]
fn zst_fam_element() {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ZST(u8);
unsafe impl FamHeader for ZST {
type Element = ();
fn fam_len(&self) -> usize {
0
}
}
let own = FamBox::from_fn(ZST(0), |_| ());
let share = unsafe { FamBox::<ZST, _>::from_slice(own.buffer()) };
let mut buf = unsafe { own.buffer() }.to_vec();
let exl = unsafe { FamBox::<ZST, _>::from_slice_mut(&mut buf) };
assert_eq!(unsafe { own.buffer() }.len(), 1);
assert_eq!(own.as_parts(), share.as_parts());
assert_eq!(own.as_parts(), exl.as_parts());
let mut s = String::new();
writeln!(s, "{own:?}").unwrap();
writeln!(s, "{share:?}").unwrap();
writeln!(s, "{exl:?}").unwrap();
}
#[test]
fn zst_header() {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ZST;
unsafe impl FamHeader for ZST {
type Element = u8;
fn fam_len(&self) -> usize {
3
}
}
let own = FamBox::from_fn(ZST, |i| i as _);
let share = unsafe { FamBox::<ZST, _>::from_slice(own.buffer()) };
let mut buf = unsafe { own.buffer() }.to_vec();
let exl = unsafe { FamBox::<ZST, _>::from_slice_mut(&mut buf) };
assert_eq!(unsafe { own.buffer() }.len(), 3);
assert_eq!(own.as_parts(), share.as_parts());
assert_eq!(own.as_parts(), exl.as_parts());
let mut s = String::new();
writeln!(s, "{own:?}").unwrap();
writeln!(s, "{share:?}").unwrap();
writeln!(s, "{exl:?}").unwrap();
}
#[test]
fn leak_from_raw() {
let own = FamBox::from_fn(TEST_MSG, |i| i as _);
let own_clone = own.clone();
let from_leak = unsafe { FamBox::from_raw(own.leak()) };
assert_eq!(own_clone, from_leak);
}
#[test]
fn drop_cnt() {
struct H(u8, [DropCnt; 0]);
unsafe impl FamHeader for H {
type Element = DropCnt;
fn fam_len(&self) -> usize {
self.0.into()
}
}
static H_DROPPED: AtomicBool = AtomicBool::new(false);
impl Drop for H {
fn drop(&mut self) {
H_DROPPED.store(true, core::sync::atomic::Ordering::Relaxed);
}
}
#[derive(Debug, Clone)]
struct DropCnt(Rc<Cell<u8>>);
impl Drop for DropCnt {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
let item = DropCnt(Rc::new(Cell::new(0)));
let own = FamBox::from_fn(H(13, []), |_| item.clone());
drop(own);
assert_eq!(item.0.get(), 13);
assert!(H_DROPPED.load(core::sync::atomic::Ordering::Relaxed));
}
#[test]
fn callback_panic() {
struct H(u8, Rc<Cell<u8>>, [DropCnt; 0]);
unsafe impl FamHeader for H {
type Element = DropCnt;
fn fam_len(&self) -> usize {
self.0.into()
}
}
static H_DROPPED: AtomicBool = AtomicBool::new(false);
impl Drop for H {
fn drop(&mut self) {
H_DROPPED.store(true, core::sync::atomic::Ordering::Relaxed);
self.1.set(self.1.get() + 1);
}
}
#[derive(Debug, Clone)]
struct DropCnt(Rc<Cell<u8>>);
impl Drop for DropCnt {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
extern crate std;
let item = DropCnt(Rc::new(Cell::new(0)));
let h_drop = Rc::new(Cell::new(0));
if !std::panic::catch_unwind(AssertUnwindSafe(|| {
drop(FamBox::from_fn(H(4, h_drop.clone(), []), |i| {
if i > 2 {
panic!("oops")
} else {
item.clone()
}
}))
}))
.is_err()
{
panic!("didn't panic when constructing")
}
assert_eq!(item.0.get(), 3);
assert!(H_DROPPED.load(core::sync::atomic::Ordering::Relaxed));
assert_eq!(h_drop.get(), 1);
}
}