-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathchecksum.rs
More file actions
190 lines (157 loc) · 4.89 KB
/
checksum.rs
File metadata and controls
190 lines (157 loc) · 4.89 KB
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
use core::{
fmt::Display,
num::Wrapping,
ops::{Add, AddAssign, Sub, SubAssign},
};
use num_traits::float::FloatCore;
use num_traits::Zero;
use serde::{de::Visitor, Deserialize, Serialize};
/// A checksum as received from the sync service.
///
/// Conceptually, we use unsigned 32 bit integers to represent checksums, and adding checksums
/// should be a wrapping add.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub struct Checksum(Wrapping<u32>);
impl Checksum {
pub const fn value(self) -> u32 {
self.0 .0
}
pub const fn from_value(value: u32) -> Self {
Self(Wrapping(value))
}
pub const fn from_i32(value: i32) -> Self {
Self::from_value(value as u32)
}
pub const fn bitcast_i32(self) -> i32 {
self.value() as i32
}
}
impl Zero for Checksum {
fn zero() -> Self {
const { Self::from_value(0) }
}
fn is_zero(&self) -> bool {
self.value() == 0
}
}
impl Add for Checksum {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Checksum {
#[inline]
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0
}
}
impl Sub for Checksum {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl SubAssign for Checksum {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}
impl From<u32> for Checksum {
fn from(value: u32) -> Self {
Self::from_value(value)
}
}
impl Display for Checksum {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:#010x}", self.value())
}
}
impl<'de> Deserialize<'de> for Checksum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct MyVisitor;
impl<'de> Visitor<'de> for MyVisitor {
type Value = Checksum;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(formatter, "a number to interpret as a checksum")
}
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.into())
}
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let as_u32: u32 = v.try_into().map_err(|_| {
E::invalid_value(serde::de::Unexpected::Unsigned(v), &"a 32-bit int")
})?;
Ok(as_u32.into())
}
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Checksum::from_i32(v))
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
// This is supposed to be an u32, but it could also be a i32 that we need to
// normalize.
let min: i64 = u32::MIN.into();
let max: i64 = u32::MAX.into();
if v >= min && v <= max {
return Ok(Checksum::from(v as u32));
}
let as_i32: i32 = v.try_into().map_err(|_| {
E::invalid_value(serde::de::Unexpected::Signed(v), &"a 32-bit int")
})?;
Ok(Checksum::from_i32(as_i32))
}
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if !v.is_finite() || f64::trunc(v) != v {
return Err(E::invalid_value(
serde::de::Unexpected::Float(v),
&"a whole number",
));
}
self.visit_i64(v as i64)
}
}
deserializer.deserialize_u32(MyVisitor)
}
}
#[cfg(test)]
mod test {
use super::Checksum;
#[test]
pub fn test_binary_representation() {
assert_eq!(Checksum::from_i32(-1).value(), u32::MAX);
assert_eq!(Checksum::from(u32::MAX).value(), u32::MAX);
assert_eq!(Checksum::from(u32::MAX).bitcast_i32(), -1);
}
fn deserialize(from: &str) -> Checksum {
serde_json::from_str(from).expect("should deserialize")
}
#[test]
pub fn test_deserialize() {
assert_eq!(deserialize("0").value(), 0);
assert_eq!(deserialize("-1").value(), u32::MAX);
assert_eq!(deserialize("-1.0").value(), u32::MAX);
assert_eq!(deserialize("3573495687").value(), 3573495687);
assert_eq!(deserialize("3573495687.0").value(), 3573495687);
assert_eq!(deserialize("-721471609.0").value(), 3573495687);
}
}