diff options
author | HampusM <hampus@hampusmat.com> | 2025-03-30 22:52:15 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-03-30 22:52:15 +0200 |
commit | f5a482faa1e4302e879533b9ce4e5a956af00cee (patch) | |
tree | dfddac9e3f7084a2601b01157d0002646e01d263 | |
parent | 0a05701441c84773849e74cec168cb8852d58218 (diff) |
feat: add into_boxed fn to FieldOwned
-rw-r--r-- | src/lib.rs | 12 | ||||
-rw-r--r-- | src/util.rs | 5 |
2 files changed, 17 insertions, 0 deletions
@@ -812,6 +812,18 @@ impl FieldOwned<'_> unsafe { &*ptr.cast::<T>() } } + + pub fn into_boxed<T: 'static>(mut self) -> Box<T> + { + assert_eq!(TypeId::of::<T>(), self.field_metadata.type_id); + + let layout = self.bytes.layout(); + + assert_eq!(layout.size(), size_of::<T>()); + assert_eq!(layout.align(), align_of::<T>()); + + unsafe { Box::from_raw(self.bytes.as_slice_mut().as_mut_ptr().cast::<T>()) } + } } #[derive(Debug)] diff --git a/src/util.rs b/src/util.rs index 242df23..22f5fb7 100644 --- a/src/util.rs +++ b/src/util.rs @@ -54,6 +54,11 @@ impl AnonUnique } } + pub fn layout(&self) -> &Layout + { + &self.layout + } + pub fn as_slice_mut(&mut self) -> &mut [MaybeUninit<u8>] { unsafe { std::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.layout.size()) } |