summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-03-30 22:52:15 +0200
committerHampusM <hampus@hampusmat.com>2025-03-30 22:52:15 +0200
commitf5a482faa1e4302e879533b9ce4e5a956af00cee (patch)
treedfddac9e3f7084a2601b01157d0002646e01d263
parent0a05701441c84773849e74cec168cb8852d58218 (diff)
feat: add into_boxed fn to FieldOwned
-rw-r--r--src/lib.rs12
-rw-r--r--src/util.rs5
2 files changed, 17 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index dbce335..f482b5c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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()) }