-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally implement bytemuck traits for Endians (#10)
Add impls of `bytemuck::Pod` and `bytemuck::Zeroable` for BigEndian and LittleEndian. This enables use of `bytemuck::cast*`-methods to safely transmute to and from these types.
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// implementations of bytemuck Zeroable and Pod traits for BigEndian and LittleEndian variants of | ||
// types which are themselves Zeroable and Pod. This enables use of bytemuck::cast* methods to | ||
// safely transmute to and from these types. | ||
|
||
use bytemuck::{Zeroable, Pod}; | ||
use crate::{Primitive, BigEndian, LittleEndian}; | ||
|
||
// SAFETY: T and T::Storage is Zeroable and Pod. LittleEndian is repr(transparent) | ||
// this satisfies the safety contracts of Zeroable and Pod. | ||
#[cfg(feature = "bytemuck")] | ||
unsafe impl<T> Zeroable for LittleEndian<T> | ||
where T: Primitive + Zeroable, | ||
T::Storage: Zeroable {} | ||
#[cfg(feature = "bytemuck")] | ||
unsafe impl<T> Pod for LittleEndian<T> | ||
where T: Primitive + Pod + Copy + 'static, | ||
T::Storage: Pod {} | ||
|
||
|
||
// SAFETY: T and T::Storage is Zeroable and Pod. BigEndian is repr(transparent) | ||
// this satisfies the safety contracts of Zeroable and Pod. | ||
#[cfg(feature = "bytemuck")] | ||
unsafe impl<T> Zeroable for BigEndian<T> | ||
where T: Primitive + Zeroable, | ||
T::Storage: Zeroable {} | ||
#[cfg(feature = "bytemuck")] | ||
unsafe impl<T> Pod for BigEndian<T> | ||
where T: Primitive + Pod + Copy + 'static, | ||
T::Storage: Pod {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::*; | ||
use bytemuck::Pod; | ||
|
||
#[test] | ||
fn check_impl_pod() { | ||
fn assert_impl_pod<T: Pod>() {} | ||
|
||
assert_impl_pod::<u16_le>(); | ||
assert_impl_pod::<u16_be>(); | ||
assert_impl_pod::<u32_le>(); | ||
assert_impl_pod::<u32_be>(); | ||
assert_impl_pod::<u64_le>(); | ||
assert_impl_pod::<u64_be>(); | ||
|
||
assert_impl_pod::<i16_le>(); | ||
assert_impl_pod::<i16_be>(); | ||
assert_impl_pod::<i32_le>(); | ||
assert_impl_pod::<i32_be>(); | ||
assert_impl_pod::<i64_le>(); | ||
assert_impl_pod::<i64_be>(); | ||
|
||
assert_impl_pod::<f32_le>(); | ||
assert_impl_pod::<f32_be>(); | ||
assert_impl_pod::<f64_le>(); | ||
assert_impl_pod::<f64_be>(); | ||
|
||
// char is not Pod, so these types shouldn't be either | ||
//assert_impl_pod::<char_be>(); | ||
//assert_impl_pod::<char_le>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters