wasm/execution/
little_endian.rs1use super::value::{F32, F64};
5
6macro_rules! impl_LittleEndianBytes{
13 [$($type:ty),+] => {
14
15 $(impl LittleEndianBytes<{ ::core::mem::size_of::<$type>() }> for $type {
16 fn from_le_bytes(bytes: [u8; ::core::mem::size_of::<$type>()]) -> Self {
17 Self::from_le_bytes(bytes)
18 }
19
20 fn to_le_bytes(self) -> [u8; ::core::mem::size_of::<$type>()] {
21 self.to_le_bytes()
22 }
23 })+
24 }
25 }
26
27pub trait LittleEndianBytes<const N: usize> {
31 fn from_le_bytes(bytes: [u8; N]) -> Self;
33
34 fn to_le_bytes(self) -> [u8; N];
36}
37
38impl_LittleEndianBytes![i8, i16, i32, i64, i128, u8, u16, u32, u64, u128];
40
41impl LittleEndianBytes<4> for F32 {
42 fn from_le_bytes(bytes: [u8; 4]) -> Self {
43 F32(f32::from_le_bytes(bytes))
44 }
45
46 fn to_le_bytes(self) -> [u8; 4] {
47 self.0.to_le_bytes()
48 }
49}
50
51impl LittleEndianBytes<8> for F64 {
52 fn from_le_bytes(bytes: [u8; 8]) -> Self {
53 F64(f64::from_le_bytes(bytes))
54 }
55
56 fn to_le_bytes(self) -> [u8; 8] {
57 self.0.to_le_bytes()
58 }
59}