Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/tobytes.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::math::{Mat4, Vec2, Vec3, Vec4};

pub trait ToBytes {
/// # Safety
///
/// Implementing this trait declares that the type in question has no padding
/// bytes and can be safely transmuted into `[u8; N]` of the appropriate size.
pub unsafe trait ToBytes {
fn to_bytes(&self) -> &[u8];
}

macro_rules! impl_tobytes {
($t:tt) => {
impl ToBytes for $t {
unsafe impl ToBytes for $t {
fn to_bytes(&self) -> &[u8] {
unsafe {
std::slice::from_raw_parts(self as *const _ as *const u8, size_of::<$t>())
Expand All @@ -22,13 +27,13 @@ impl_tobytes!(Vec3);
impl_tobytes!(Vec4);
impl_tobytes!(Mat4);

impl<T: ToBytes, const N: usize> ToBytes for &[T; N] {
unsafe 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) }
unsafe { std::slice::from_raw_parts(self as *const _ as *const u8, size_of::<T>() * N) }
}
}

impl<T: ToBytes> ToBytes for &[T] {
unsafe impl<T: ToBytes> ToBytes for [T] {
fn to_bytes(&self) -> &[u8] {
unsafe {
std::slice::from_raw_parts(
Expand Down