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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
macro_rules! impl_from_bits_ {
($id:ident[$test_tt:tt]: $from_ty:ident) => {
impl crate::api::into_bits::FromBits<$from_ty> for $id {
#[inline]
fn from_bits(x: $from_ty) -> Self {
unsafe { crate::mem::transmute(x) }
}
}
test_if! {
$test_tt:
paste::item! {
pub mod [<$id _from_bits_ $from_ty>] {
use super::*;
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test() {
use crate::{
ptr::{read_unaligned},
mem::{size_of, zeroed}
};
use crate::IntoBits;
assert_eq!(size_of::<$id>(),
size_of::<$from_ty>());
let a: $from_ty = unsafe { zeroed() };
let b_0: $id = crate::FromBits::from_bits(a);
let b_1: $id = a.into_bits();
for i in 0..size_of::<$id>() {
unsafe {
let b_0_v: u8 = read_unaligned(
(&b_0 as *const $id as *const u8)
.wrapping_add(i)
);
let b_1_v: u8 = read_unaligned(
(&b_1 as *const $id as *const u8)
.wrapping_add(i)
);
assert_eq!(b_0_v, b_1_v);
}
}
}
}
}
}
};
}
macro_rules! impl_from_bits {
($id:ident[$test_tt:tt]: $($from_ty:ident),*) => {
$(
impl_from_bits_!($id[$test_tt]: $from_ty);
)*
}
}
#[allow(unused)]
macro_rules! impl_into_bits {
($id:ident[$test_tt:tt]: $($from_ty:ident),*) => {
$(
impl_from_bits_!($from_ty[$test_tt]: $id);
)*
}
}