forked from not-fl3/macroquad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtobytes.rs
40 lines (36 loc) · 969 Bytes
/
tobytes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::math::{Mat4, Vec2, Vec3, Vec4};
pub trait ToBytes {
fn to_bytes(&self) -> &[u8];
}
macro_rules! impl_tobytes {
($t:tt) => {
impl ToBytes for $t {
fn to_bytes(&self) -> &[u8] {
unsafe {
std::slice::from_raw_parts(self as *const _ as *const u8, size_of::<$t>())
}
}
}
};
}
impl_tobytes!(i32);
impl_tobytes!(f32);
impl_tobytes!(Vec2);
impl_tobytes!(Vec3);
impl_tobytes!(Vec4);
impl_tobytes!(Mat4);
impl<T: ToBytes, const N: usize> ToBytes for &[T; N] {
fn to_bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(*self as *const _ as *const u8, size_of::<T>() * N) }
}
}
impl<T: ToBytes> ToBytes for &[T] {
fn to_bytes(&self) -> &[u8] {
unsafe {
std::slice::from_raw_parts(
self.as_ptr() as *const _ as *const u8,
size_of::<T>() * self.len(),
)
}
}
}