Skip to content

Commit

Permalink
array_as_u8_slice_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Aug 31, 2024
1 parent 2868ea0 commit e8ca400
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fyrox-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ pub fn array_as_u8_slice<T: Sized + Pod>(v: &[T]) -> &'_ [u8] {
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, std::mem::size_of_val(v)) }
}

/// "Transmutes" array of any sized type to a slice of bytes.
pub fn array_as_u8_slice_mut<T: Sized + Pod>(v: &mut [T]) -> &'_ mut [u8] {
// SAFETY: It is safe to reinterpret memory region of POD type.
unsafe { std::slice::from_raw_parts_mut(v.as_mut_ptr() as *mut u8, std::mem::size_of_val(v)) }
}

/// "Transmutes" array of any sized type to a slice of some other type.
pub fn transmute_slice<T: Sized, U: Sized>(v: &[T]) -> &'_ [U] {
// SAFETY: It is safe to reinterpret data to read it.
Expand Down

0 comments on commit e8ca400

Please sign in to comment.