diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 994c540..54ca4ba 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -1,3 +1,5 @@ +use core::mem; +use core::slice; use core::ops::{Deref, DerefMut, Range}; use alloc::Vec; @@ -89,6 +91,24 @@ where } } +impl<'a> BufferSlice<'a, u8> { + pub fn dynamic_cast(&self) -> (T, usize) { + assert!(self.inner.len() >= mem::size_of::()); + let index = self.index; + let cast = unsafe { + mem::transmute_copy(self.inner.as_ptr().as_ref().unwrap()) + }; + (cast, index) + } + + pub fn from_cast(cast: &T, index: usize) -> BufferSlice<'a, u8> { + let len = mem::size_of::(); + let ptr = cast as *const T as *const u8; + let slice = unsafe { slice::from_raw_parts(ptr, len) }; + BufferSlice::new(slice, index) + } +} + impl<'a, T> BufferSlice<'a, T> where [T]: ToOwned>,