1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use bytes::varnum::*;
use binjs_shared::F64;
use std;
use std::io::Write;
const NONE_FLOAT_REPR: u64 = 0x7FF0000000000001;
const VARNUM_PREFIX_FLOAT: [u8; 2] = VARNUM_INVALID_ZERO_1;
const VARNUM_NULL: [u8; 3] = VARNUM_INVALID_ZERO_2;
pub fn varbytes_of_float(value: Option<f64>) -> Box<[u8]> {
let mut buf = Vec::with_capacity(4);
buf.write_maybe_varfloat(value).unwrap();
buf.into()
}
pub fn bytes_of_float(value: Option<f64>) -> [u8; 8] {
let mut as_u64: u64 = match value {
None => NONE_FLOAT_REPR,
Some(value) => unsafe { std::mem::transmute::<f64, u64>(value) },
};
let mut buf: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
for i in 0..8 {
buf[i] = (as_u64 % 256) as u8;
as_u64 >>= 8;
}
buf
}
pub trait WriteVarFloat {
fn write_maybe_varfloat(&mut self, value: Option<f64>) -> Result<usize, std::io::Error>;
fn write_varfloat(&mut self, num: f64) -> Result<usize, std::io::Error>;
fn write_maybe_varfloat2(&mut self, value: Option<F64>) -> Result<usize, std::io::Error> {
self.write_maybe_varfloat(value.map(Into::<f64>::into))
}
fn write_varfloat2(&mut self, num: F64) -> Result<usize, std::io::Error> {
self.write_varfloat(num.into())
}
}
impl<T> WriteVarFloat for T
where
T: Write,
{
fn write_maybe_varfloat(&mut self, value: Option<f64>) -> Result<usize, std::io::Error> {
match value {
None => {
self.write_all(&VARNUM_NULL)?;
Ok(VARNUM_NULL.len())
}
Some(v) => self.write_varfloat(v),
}
}
fn write_varfloat(&mut self, value: f64) -> Result<usize, std::io::Error> {
{
let as_signed_integer = value as i32;
if as_signed_integer as f64 == value
&& (as_signed_integer != 0 || value.is_sign_positive())
{
return self.write_signed_varnum(as_signed_integer);
}
}
let bytes = bytes_of_float(Some(value));
self.write_all(&VARNUM_PREFIX_FLOAT)?;
self.write_all(&bytes)?;
Ok(bytes.len() + VARNUM_PREFIX_FLOAT.len())
}
}
pub trait ReadVarFloat {
fn read_maybe_varfloat(&mut self) -> Result<Option<f64>, std::io::Error>;
}
impl<T> ReadVarFloat for T
where
T: std::io::Read,
{
fn read_maybe_varfloat(&mut self) -> Result<Option<f64>, std::io::Error> {
let mut as_i32 = 0;
let bytes_read = self.read_extended_signed_varnum_to(&mut as_i32)?;
if as_i32 == 0 {
match bytes_read {
1 => {
return Ok(Some(0.));
}
2 => {
let mut buf: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
self.read_exact(&mut buf)?;
return Ok(float_of_bytes(&buf));
}
3 => {
return Ok(None);
}
_ => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Invalid varfloat",
));
}
}
}
Ok(Some(as_i32 as f64))
}
}
pub fn float_of_bytes(buf: &[u8; 8]) -> Option<f64> {
let as_u64 = ((buf[0] as u64) << 0)
| ((buf[1] as u64) << 8)
| ((buf[2] as u64) << 16)
| ((buf[3] as u64) << 24)
| ((buf[4] as u64) << 32)
| ((buf[5] as u64) << 40)
| ((buf[6] as u64) << 48)
| ((buf[7] as u64) << 56);
if as_u64 == NONE_FLOAT_REPR {
None
} else {
let as_f64 = unsafe { std::mem::transmute::<_, f64>(as_u64) };
Some(as_f64)
}
}
#[test]
fn test_floats() {
use std::f64::*;
for x in &[0., 100., 10., 1000., INFINITY, MIN, MAX, NEG_INFINITY] {
let value = Some(*x);
let encoded = bytes_of_float(value);
let decoded = float_of_bytes(&encoded);
println!("Encoded {:?} as {:?}, decoded as {:?}", x, encoded, decoded);
assert_eq!(decoded, value);
}
assert_eq!(float_of_bytes(&bytes_of_float(None)), None);
}
#[test]
fn test_var_floats() {
fn single_value(value: Option<f64>) -> usize {
let mut buf = Vec::new();
buf.write_maybe_varfloat(value).unwrap();
let size = buf.len();
let mut input = std::io::Cursor::new(buf);
let decoded = input.read_maybe_varfloat().unwrap();
assert_eq!(decoded, value);
size
}
use std::f64::*;
for i in -63..63 {
let bytes = single_value(Some(i as f64));
assert_eq!(
bytes, 1,
"Integer values between -63 and 63 should fit in one byte: {}",
i
);
}
for x in &[
-256.,
1000.,
0.5,
1.7,
3.8,
11.1,
INFINITY,
MIN,
MAX,
NEG_INFINITY,
] {
single_value(Some(*x));
}
single_value(None);
}