Skip to content

Make the ToBytes trait unsafe #956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Loading