micromarshal/
unmarshal.rs

1// Copyright 2021 Datafuse Labs.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::io;
16
17use std::io::Error;
18use std::io::Result;
19
20use ethnum::I256;
21use ethnum::U256;
22use ordered_float::OrderedFloat;
23
24pub trait Unmarshal<T> {
25    fn unmarshal(scratch: &[u8]) -> T;
26    fn try_unmarshal(scratch: &[u8]) -> Result<T> {
27        Ok(Self::unmarshal(scratch))
28    }
29}
30
31impl Unmarshal<u8> for u8 {
32    fn unmarshal(scratch: &[u8]) -> Self {
33        scratch[0]
34    }
35}
36
37impl Unmarshal<u16> for u16 {
38    fn unmarshal(scratch: &[u8]) -> Self {
39        u16::from_le_bytes(scratch.try_into().unwrap())
40    }
41}
42
43impl Unmarshal<u32> for u32 {
44    fn unmarshal(scratch: &[u8]) -> Self {
45        u32::from_le_bytes(scratch.try_into().unwrap())
46    }
47}
48
49impl Unmarshal<u64> for u64 {
50    fn unmarshal(scratch: &[u8]) -> Self {
51        u64::from_le_bytes(scratch.try_into().unwrap())
52    }
53}
54
55impl Unmarshal<u128> for u128 {
56    fn unmarshal(scratch: &[u8]) -> Self {
57        u128::from_le_bytes(scratch.try_into().unwrap())
58    }
59}
60
61impl Unmarshal<U256> for U256 {
62    fn unmarshal(scratch: &[u8]) -> Self {
63        U256::from_le_bytes(scratch.try_into().unwrap())
64    }
65}
66
67impl Unmarshal<i8> for i8 {
68    fn unmarshal(scratch: &[u8]) -> Self {
69        i8::from_le_bytes(scratch.try_into().unwrap())
70    }
71}
72
73impl Unmarshal<i16> for i16 {
74    fn unmarshal(scratch: &[u8]) -> Self {
75        i16::from_le_bytes(scratch.try_into().unwrap())
76    }
77}
78
79impl Unmarshal<i32> for i32 {
80    fn unmarshal(scratch: &[u8]) -> Self {
81        i32::from_le_bytes(scratch.try_into().unwrap())
82    }
83}
84
85impl Unmarshal<i64> for i64 {
86    fn unmarshal(scratch: &[u8]) -> Self {
87        i64::from_le_bytes(scratch.try_into().unwrap())
88    }
89}
90
91impl Unmarshal<i128> for i128 {
92    fn unmarshal(scratch: &[u8]) -> Self {
93        i128::from_le_bytes(scratch.try_into().unwrap())
94    }
95}
96
97impl Unmarshal<I256> for I256 {
98    fn unmarshal(scratch: &[u8]) -> Self {
99        I256::from_le_bytes(scratch.try_into().unwrap())
100    }
101}
102
103impl Unmarshal<f32> for f32 {
104    fn unmarshal(scratch: &[u8]) -> Self {
105        let bits = u32::from_le_bytes(scratch.try_into().unwrap());
106        Self::from_bits(bits)
107    }
108}
109
110impl Unmarshal<f64> for f64 {
111    fn unmarshal(scratch: &[u8]) -> Self {
112        let bits = u64::from_le_bytes(scratch.try_into().unwrap());
113        Self::from_bits(bits)
114    }
115}
116
117impl Unmarshal<OrderedFloat<f32>> for OrderedFloat<f32> {
118    fn unmarshal(scratch: &[u8]) -> Self {
119        let bits = u32::from_le_bytes(scratch.try_into().unwrap());
120        f32::from_bits(bits).into()
121    }
122}
123
124impl Unmarshal<OrderedFloat<f64>> for OrderedFloat<f64> {
125    fn unmarshal(scratch: &[u8]) -> Self {
126        let bits = u64::from_le_bytes(scratch.try_into().unwrap());
127        f64::from_bits(bits).into()
128    }
129}
130
131impl Unmarshal<bool> for bool {
132    fn unmarshal(scratch: &[u8]) -> Self {
133        scratch[0] != 0
134    }
135}
136
137impl Unmarshal<char> for char {
138    fn unmarshal(_: &[u8]) -> char {
139        unimplemented!()
140    }
141
142    fn try_unmarshal(scratch: &[u8]) -> Result<char> {
143        let bits = u32::from_le_bytes(scratch.try_into().unwrap());
144        match char::from_u32(bits) {
145            Some(c) => Ok(c),
146            None => Err(Error::new(
147                io::ErrorKind::Other,
148                format!("try unmarshal u32 to char failed: {}", bits),
149            )),
150        }
151    }
152}