use core::cmp::Ordering; use crate::upcast::IntoUsize; //noinspection SpellCheckingInspection /// Converts reference of struct to binary slice pub fn slicify(value: &T) -> &[u8] { let ptr = value as *const T as *const u8; unsafe { core::slice::from_raw_parts(ptr, core::mem::size_of::()) } } /// Converts reference of struct to binary slice pub unsafe fn slicify_mut(value: &mut T) -> &mut [u8] { let ptr = value as *mut T as *mut u8; core::slice::from_raw_parts_mut(ptr, core::mem::size_of::()) } /// converts a non mutable reference into a mutable one pub unsafe fn mutify(nr: &T) -> &mut T { &mut *(nr as *const T as *mut T) } /// converts a reference of any lifetime to 'static pub unsafe fn statify<'a, T>(nr: &'a T) -> &'static T { &*(nr as *const T) } /// converts mutable a reference of any lifetime to 'static pub unsafe fn statify_mut<'a, T>(nr: &'a mut T) -> &'static mut T { &mut *(nr as *mut T) } /// gets the distance between two references pub fn distance(p1: impl IntoUsize, p2: impl IntoUsize) -> usize { let (p1, p2) = (p1.into_usize(), p2.into_usize()); match p1.cmp(&p2) { Ordering::Less => p2 - p1, Ordering::Greater => p1 - p2, Ordering::Equal => 0, } }