1pub(crate) trait Imm {
10 fn bytes(&self) -> &[u8];
12}
13
14macro_rules! impl_imm {
15 (#[$doc:meta] $name:ident, $size:expr, from: { $( $from:ty ),* $(,)? }) => {
16 #[$doc]
17 pub struct $name([u8; $size]);
18
19 impl Imm for $name {
20 fn bytes(&self) -> &[u8] {
22 &self.0
23 }
24 }
25
26 impl $name {
27 pub fn splat_u8(val: u8) -> $name {
28 $name([val; $size])
29 }
30 }
31
32 $(
33 impl From<$from> for $name {
34 fn from(imm: $from) -> Self {
35 let mut buf = [0u8; $size];
36 let imm = imm.to_ne_bytes();
37 buf[0..imm.len()].copy_from_slice(&imm);
38 $name(buf)
39 }
40 }
41 )*
42 }
43}
44
45impl_imm!(
46 Imm8, 1, from: { u8, i8 }
48);
49impl_imm!(
50 Imm16, 2, from: { u16, i16, u8, i8 }
52);
53impl_imm!(
54 Imm32, 4, from: { u32, i32, u16, i16, u8, i8 }
56);
57impl_imm!(
58 Imm64, 8, from: { u64, i64, u32, i32, u16, i16, u8, i8, usize, isize }
60);
61
62#[cfg(test)]
63mod test {
64 use super::*;
65 use std::mem::size_of;
66
67 #[test]
68 fn test_usize_isize() {
69 assert_eq!(size_of::<usize>(), size_of::<Imm64>());
71 assert_eq!(size_of::<isize>(), size_of::<Imm64>());
72 }
73}