|
| 1 | +//! Module for [`new_boxed`]. |
| 2 | +
|
| 3 | +use crate::{increase_to_alignment, Header, MaybeDynSized, ALIGNMENT}; |
| 4 | +use alloc::boxed::Box; |
| 5 | +use core::alloc::Layout; |
| 6 | +use core::mem; |
| 7 | +use core::ops::Deref; |
| 8 | +use core::ptr; |
| 9 | + |
| 10 | +/// Creates a new tag implementing [`MaybeDynSized`] on the heap. This works for |
| 11 | +/// sized and unsized tags. However, it only makes sense to use this for tags |
| 12 | +/// that are DSTs (unsized). For regular sized structs, you can just create a |
| 13 | +/// typical constructor and box the result. |
| 14 | +/// |
| 15 | +/// The provided `header`' total size (see [`Header`]) will be set dynamically |
| 16 | +/// by this function using [`Header::set_size`]. However, it must contain all |
| 17 | +/// other relevant metadata or update it in the `set_size` callback. |
| 18 | +/// |
| 19 | +/// # Parameters |
| 20 | +/// - `additional_bytes_slices`: Array of byte slices that should be included |
| 21 | +/// without additional padding in-between. You don't need to add the bytes |
| 22 | +/// for [`Header`], but only additional payload. |
| 23 | +#[must_use] |
| 24 | +pub fn new_boxed<T: MaybeDynSized<Metadata = usize> + ?Sized>( |
| 25 | + mut header: T::Header, |
| 26 | + additional_bytes_slices: &[&[u8]], |
| 27 | +) -> Box<T> { |
| 28 | + let additional_size = additional_bytes_slices |
| 29 | + .iter() |
| 30 | + .map(|b| b.len()) |
| 31 | + .sum::<usize>(); |
| 32 | + |
| 33 | + let tag_size = mem::size_of::<T::Header>() + additional_size; |
| 34 | + header.set_size(tag_size); |
| 35 | + |
| 36 | + // Allocation size is multiple of alignment. |
| 37 | + // See <https://doc.rust-lang.org/reference/type-layout.html> |
| 38 | + let alloc_size = increase_to_alignment(tag_size); |
| 39 | + let layout = Layout::from_size_align(alloc_size, ALIGNMENT).unwrap(); |
| 40 | + let heap_ptr = unsafe { alloc::alloc::alloc(layout) }; |
| 41 | + assert!(!heap_ptr.is_null()); |
| 42 | + |
| 43 | + // write header |
| 44 | + { |
| 45 | + let len = mem::size_of::<T::Header>(); |
| 46 | + let ptr = core::ptr::addr_of!(header); |
| 47 | + unsafe { |
| 48 | + ptr::copy_nonoverlapping(ptr.cast::<u8>(), heap_ptr, len); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // write body |
| 53 | + { |
| 54 | + let mut write_offset = mem::size_of::<T::Header>(); |
| 55 | + for &bytes in additional_bytes_slices { |
| 56 | + let len = bytes.len(); |
| 57 | + let src = bytes.as_ptr(); |
| 58 | + unsafe { |
| 59 | + let dst = heap_ptr.add(write_offset); |
| 60 | + ptr::copy_nonoverlapping(src, dst, len); |
| 61 | + write_offset += len; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // This is a fat pointer for DSTs and a thin pointer for sized `T`s. |
| 67 | + let ptr: *mut T = ptr_meta::from_raw_parts_mut(heap_ptr.cast(), T::dst_len(&header)); |
| 68 | + let reference = unsafe { Box::from_raw(ptr) }; |
| 69 | + |
| 70 | + // If this panic triggers, there is a fundamental flaw in my logic. This is |
| 71 | + // not the fault of an API user. |
| 72 | + assert_eq!( |
| 73 | + mem::size_of_val(reference.deref()), |
| 74 | + alloc_size, |
| 75 | + "Allocation should match Rusts expectation" |
| 76 | + ); |
| 77 | + |
| 78 | + reference |
| 79 | +} |
| 80 | + |
| 81 | +/// Clones a [`MaybeDynSized`] by calling [`new_boxed`]. |
| 82 | +#[must_use] |
| 83 | +pub fn clone_dyn<T: MaybeDynSized<Metadata = usize> + ?Sized>(tag: &T) -> Box<T> { |
| 84 | + new_boxed(tag.header().clone(), &[tag.payload()]) |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod tests { |
| 89 | + use super::*; |
| 90 | + use crate::test_utils::{DummyDstTag, DummyTestHeader}; |
| 91 | + use crate::Tag; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_new_boxed() { |
| 95 | + let header = DummyTestHeader::new(DummyDstTag::ID, 0); |
| 96 | + let tag = new_boxed::<DummyDstTag>(header, &[&[0, 1, 2, 3]]); |
| 97 | + assert_eq!(tag.header().typ(), 42); |
| 98 | + assert_eq!(tag.payload(), &[0, 1, 2, 3]); |
| 99 | + |
| 100 | + // Test that bytes are added consecutively without gaps. |
| 101 | + let header = DummyTestHeader::new(0xdead_beef, 0); |
| 102 | + let tag = new_boxed::<DummyDstTag>(header, &[&[0], &[1], &[2, 3]]); |
| 103 | + assert_eq!(tag.header().typ(), 0xdead_beef); |
| 104 | + assert_eq!(tag.payload(), &[0, 1, 2, 3]); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_clone_tag() { |
| 109 | + let header = DummyTestHeader::new(DummyDstTag::ID, 0); |
| 110 | + let tag = new_boxed::<DummyDstTag>(header, &[&[0, 1, 2, 3]]); |
| 111 | + assert_eq!(tag.header().typ(), 42); |
| 112 | + assert_eq!(tag.payload(), &[0, 1, 2, 3]); |
| 113 | + |
| 114 | + let _cloned = clone_dyn(tag.as_ref()); |
| 115 | + } |
| 116 | +} |
0 commit comments