Skip to content

Commit

Permalink
Check at compile time if mutual transmutation involves same-sized types
Browse files Browse the repository at this point in the history
  • Loading branch information
jdh8 committed May 12, 2024
1 parent ebe5e48 commit 47009aa
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ impl<const E: u32, const M: u32> F16<E, M> {
}
}

trait SameSized<T> {}

impl<T, U> SameSized<T> for U where Check<{ mem::size_of::<T>() == mem::size_of::<Self>() }>: True {}

/// Mutual transmutation
///
/// This trait provides an interface of mutual raw transmutation. The methods
Expand All @@ -547,12 +551,7 @@ impl<const E: u32, const M: u32> F16<E, M> {
///
/// In this crate, all [`F8`] types implement `Transmute<u8>`, and all [`F16`]
/// types implement `Transmute<u16>`.
pub trait Transmute<T>: Copy {
/// Assert the same size between `T` and `Self`
///
/// Do not override this constant.
const _SAME_SIZE: () = assert!(mem::size_of::<T>() == mem::size_of::<Self>());

pub trait Transmute<T>: Copy + SameSized<T> {
/// Raw transmutation from `T`
fn from_bits(v: T) -> Self {
unsafe { mem::transmute_copy(&v) }
Expand All @@ -567,6 +566,7 @@ pub trait Transmute<T>: Copy {
impl<const E: u32, const M: u32, const N: NanStyle, const B: i32> Transmute<u8> for F8<E, M, N, B>
where
Check<{ Self::VALID }>: True,
Self: SameSized<u8>,
{
fn from_bits(v: u8) -> Self {
Self::from_bits(v)
Expand All @@ -580,6 +580,7 @@ where
impl<const E: u32, const M: u32> Transmute<u16> for F16<E, M>
where
Check<{ Self::VALID }>: True,
Self: SameSized<u16>,
{
fn from_bits(v: u16) -> Self {
Self::from_bits(v)
Expand Down

0 comments on commit 47009aa

Please sign in to comment.