blob: 8e0bfedbe835b0363e8320d47bc779682c5fb0f6 [file] [log] [blame]
Austin Schuh272c6132020-11-14 16:37:52 -08001// 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
15use crate::bitwidth::BitWidth::*;
16use 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)]
35pub enum BitWidth {
36 W8 = 0,
37 W16 = 1,
38 W32 = 2,
39 W64 = 3,
40}
41impl BitWidth {
42 pub(crate) fn iter() -> Iter<'static, Self> {
43 [W8, W16, W32, W64].iter()
44 }
45 pub fn n_bytes(self) -> usize {
46 1 << self as usize
47 }
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
59impl Default for BitWidth {
60 fn default() -> Self {
61 W8
62 }
63}
64
65// TODO(cneo): Overloading with `from` is probably not the most readable idea in hindsight.
66macro_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}
85impl_bitwidth_from!(u64, u64, u32, u16, u8);
86impl_bitwidth_from!(usize, u64, u32, u16, u8);
87impl_bitwidth_from!(i64, i64, i32, i16, i8);
88
89#[allow(clippy::float_cmp)]
90impl From<f64> for BitWidth {
91 fn from(x: f64) -> BitWidth {
92 if x != x as f32 as f64 {
93 W64
94 } else {
95 W32
96 }
97 }
98}
99impl From<f32> for BitWidth {
100 fn from(_: f32) -> BitWidth {
101 W32
102 }
103}
104
105/// Zero pad `v` until `T` will be byte aligned when pushed.
106pub 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}