1 // Copyright 2019 Google LLC
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 // https://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
15 use crate::bitwidth::BitWidth::*;
16 use std::slice::Iter;
17
18 /// Represents the size of Flexbuffers data.
19 ///
20 /// Flexbuffers automatically compresses numbers to the smallest possible width
21 /// (`250u64` is stored as `250u8`).
22 #[derive(
23 Debug,
24 Clone,
25 Copy,
26 PartialEq,
27 Eq,
28 PartialOrd,
29 Serialize,
30 Deserialize,
31 Ord,
32 num_enum::TryFromPrimitive,
33 )]
34 #[repr(u8)]
35 pub enum BitWidth {
36 W8 = 0,
37 W16 = 1,
38 W32 = 2,
39 W64 = 3,
40 }
41 impl BitWidth {
iter() -> Iter<'static, Self>42 pub(crate) fn iter() -> Iter<'static, Self> {
43 [W8, W16, W32, W64].iter()
44 }
n_bytes(self) -> usize45 pub fn n_bytes(self) -> usize {
46 1 << self as usize
47 }
from_nbytes(n: impl std::convert::Into<usize>) -> Option<Self>48 pub fn from_nbytes(n: impl std::convert::Into<usize>) -> Option<Self> {
49 match n.into() {
50 1 => Some(W8),
51 2 => Some(W16),
52 4 => Some(W32),
53 8 => Some(W64),
54 _ => None,
55 }
56 }
57 }
58
59 impl Default for BitWidth {
default() -> Self60 fn default() -> Self {
61 W8
62 }
63 }
64
65 // TODO(cneo): Overloading with `from` is probably not the most readable idea in hindsight.
66 macro_rules! impl_bitwidth_from {
67 ($from: ident, $w64: ident, $w32: ident, $w16: ident, $w8: ident) => {
68 impl From<$from> for BitWidth {
69 fn from(x: $from) -> BitWidth {
70 let x = x as $w64;
71 if x >= $w8::min_value() as $w64 && x <= $w8::max_value() as $w64 {
72 return W8;
73 }
74 if x >= $w16::min_value() as $w64 && x <= $w16::max_value() as $w64 {
75 return W16;
76 }
77 if x >= $w32::min_value() as $w64 && x <= $w32::max_value() as $w64 {
78 return W32;
79 }
80 W64
81 }
82 }
83 };
84 }
85 impl_bitwidth_from!(u64, u64, u32, u16, u8);
86 impl_bitwidth_from!(usize, u64, u32, u16, u8);
87 impl_bitwidth_from!(i64, i64, i32, i16, i8);
88
89 #[allow(clippy::float_cmp)]
90 impl From<f64> for BitWidth {
from(x: f64) -> BitWidth91 fn from(x: f64) -> BitWidth {
92 if x != x as f32 as f64 {
93 W64
94 } else {
95 W32
96 }
97 }
98 }
99 impl From<f32> for BitWidth {
from(_: f32) -> BitWidth100 fn from(_: f32) -> BitWidth {
101 W32
102 }
103 }
104
105 /// Zero pad `v` until `T` will be byte aligned when pushed.
align(buffer: &mut Vec<u8>, width: BitWidth)106 pub fn align(buffer: &mut Vec<u8>, width: BitWidth) {
107 let bytes = 1 << width as u8;
108 let alignment = (bytes - buffer.len() % bytes) % bytes;
109 // Profiling reveals the loop is faster than Vec::resize.
110 for _ in 0..alignment as usize {
111 buffer.push(0);
112 }
113 }
114