summaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-03-26 23:28:02 +0100
committerHampusM <hampus@hampusmat.com>2025-03-28 11:52:41 +0100
commitfa20f1839448f9d5c7ccb9dcbabeb6d0785f6083 (patch)
treef938cd8fac6519380942bc6d3e6d43484214eb17 /src/util.rs
parentf0b4f036504a823ca673fee8f3bae47e1ad30894 (diff)
feat: add get_field function
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..6b7180a
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,31 @@
+use std::mem::MaybeUninit;
+
+pub trait MaybeUninitByteSlice
+{
+ unsafe fn cast<Item: 'static>(&self) -> &[Item];
+}
+
+impl MaybeUninitByteSlice for &[MaybeUninit<u8>]
+{
+ unsafe fn cast<Item: 'static>(&self) -> &[Item]
+ {
+ assert_eq!(self.len() % size_of::<Item>(), 0, "Invalid item size");
+
+ assert_eq!(
+ self.as_ptr().addr() % align_of::<Item>(),
+ 0,
+ "Invalid item alignment"
+ );
+
+ if size_of::<Item>() == 0 {
+ return &[];
+ }
+
+ unsafe {
+ std::slice::from_raw_parts(
+ self.as_ptr().cast::<Item>(),
+ self.len() / size_of::<Item>(),
+ )
+ }
+ }
+}