This commit is contained in:
numbers
2023-09-04 06:26:35 -04:00
commit a5fe71ebb3
26 changed files with 1186 additions and 0 deletions

40
src/data.rs Normal file
View File

@@ -0,0 +1,40 @@
use core::cmp::Ordering;
use crate::upcast::IntoUsize;
//noinspection SpellCheckingInspection
/// Converts reference of struct to binary slice
pub fn slicify<T>(value: &T) -> &[u8] {
let ptr = value as *const T as *const u8;
unsafe { core::slice::from_raw_parts(ptr, core::mem::size_of::<T>()) }
}
/// Converts reference of struct to binary slice
pub unsafe fn slicify_mut<T>(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::<T>())
}
/// converts a non mutable reference into a mutable one
pub unsafe fn mutify<T>(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,
}
}