mirror of
https://git.intege.rs/xlib/x.git
synced 2025-12-05 20:35:01 +00:00
initial
This commit is contained in:
40
src/data.rs
Normal file
40
src/data.rs
Normal 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user