|
| 1 | +use core::{ |
| 2 | + fmt::Display, |
| 3 | + num::Wrapping, |
| 4 | + ops::{Add, AddAssign, Sub, SubAssign}, |
| 5 | +}; |
| 6 | + |
| 7 | +use num_traits::float::FloatCore; |
| 8 | +use num_traits::Zero; |
| 9 | +use serde::{de::Visitor, Deserialize, Serialize}; |
| 10 | + |
| 11 | +/// A checksum as received from the sync service. |
| 12 | +/// |
| 13 | +/// Conceptually, we use unsigned 32 bit integers to represent checksums, and adding checksums |
| 14 | +/// should be a wrapping add. |
| 15 | +#[repr(transparent)] |
| 16 | +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)] |
| 17 | +pub struct Checksum(Wrapping<u32>); |
| 18 | + |
| 19 | +impl Checksum { |
| 20 | + pub const fn value(self) -> u32 { |
| 21 | + self.0 .0 |
| 22 | + } |
| 23 | + |
| 24 | + pub const fn from_value(value: u32) -> Self { |
| 25 | + Self(Wrapping(value)) |
| 26 | + } |
| 27 | + |
| 28 | + pub const fn from_i32(value: i32) -> Self { |
| 29 | + Self::from_value(value as u32) |
| 30 | + } |
| 31 | + |
| 32 | + pub const fn bitcast_i32(self) -> i32 { |
| 33 | + self.value() as i32 |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl Zero for Checksum { |
| 38 | + fn zero() -> Self { |
| 39 | + const { Self::from_value(0) } |
| 40 | + } |
| 41 | + |
| 42 | + fn is_zero(&self) -> bool { |
| 43 | + self.value() == 0 |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl Add for Checksum { |
| 48 | + type Output = Self; |
| 49 | + |
| 50 | + #[inline] |
| 51 | + fn add(self, rhs: Self) -> Self::Output { |
| 52 | + Self(self.0 + rhs.0) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl AddAssign for Checksum { |
| 57 | + #[inline] |
| 58 | + fn add_assign(&mut self, rhs: Self) { |
| 59 | + self.0 += rhs.0 |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl Sub for Checksum { |
| 64 | + type Output = Self; |
| 65 | + |
| 66 | + fn sub(self, rhs: Self) -> Self::Output { |
| 67 | + Self(self.0 - rhs.0) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl SubAssign for Checksum { |
| 72 | + fn sub_assign(&mut self, rhs: Self) { |
| 73 | + self.0 -= rhs.0; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl From<u32> for Checksum { |
| 78 | + fn from(value: u32) -> Self { |
| 79 | + Self::from_value(value) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl Display for Checksum { |
| 84 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 85 | + write!(f, "{:#010x}", self.value()) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl<'de> Deserialize<'de> for Checksum { |
| 90 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 91 | + where |
| 92 | + D: serde::Deserializer<'de>, |
| 93 | + { |
| 94 | + struct MyVisitor; |
| 95 | + |
| 96 | + impl<'de> Visitor<'de> for MyVisitor { |
| 97 | + type Value = Checksum; |
| 98 | + |
| 99 | + fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 100 | + write!(formatter, "a number to interpret as a checksum") |
| 101 | + } |
| 102 | + |
| 103 | + fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E> |
| 104 | + where |
| 105 | + E: serde::de::Error, |
| 106 | + { |
| 107 | + Ok(v.into()) |
| 108 | + } |
| 109 | + |
| 110 | + fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> |
| 111 | + where |
| 112 | + E: serde::de::Error, |
| 113 | + { |
| 114 | + let as_u32: u32 = v.try_into().map_err(|_| { |
| 115 | + E::invalid_value(serde::de::Unexpected::Unsigned(v), &"a 32-bit int") |
| 116 | + })?; |
| 117 | + Ok(as_u32.into()) |
| 118 | + } |
| 119 | + |
| 120 | + fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E> |
| 121 | + where |
| 122 | + E: serde::de::Error, |
| 123 | + { |
| 124 | + Ok(Checksum::from_i32(v)) |
| 125 | + } |
| 126 | + |
| 127 | + fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> |
| 128 | + where |
| 129 | + E: serde::de::Error, |
| 130 | + { |
| 131 | + // This is supposed to be an u32, but it could also be a i32 that we need to |
| 132 | + // normalize. |
| 133 | + let min: i64 = u32::MIN.into(); |
| 134 | + let max: i64 = u32::MAX.into(); |
| 135 | + |
| 136 | + if v >= min && v <= max { |
| 137 | + return Ok(Checksum::from(v as u32)); |
| 138 | + } |
| 139 | + |
| 140 | + let as_i32: i32 = v.try_into().map_err(|_| { |
| 141 | + E::invalid_value(serde::de::Unexpected::Signed(v), &"a 32-bit int") |
| 142 | + })?; |
| 143 | + Ok(Checksum::from_i32(as_i32)) |
| 144 | + } |
| 145 | + |
| 146 | + fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> |
| 147 | + where |
| 148 | + E: serde::de::Error, |
| 149 | + { |
| 150 | + if !v.is_finite() || f64::trunc(v) != v { |
| 151 | + return Err(E::invalid_value( |
| 152 | + serde::de::Unexpected::Float(v), |
| 153 | + &"a whole number", |
| 154 | + )); |
| 155 | + } |
| 156 | + |
| 157 | + self.visit_i64(v as i64) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + deserializer.deserialize_u32(MyVisitor) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +#[cfg(test)] |
| 166 | +mod test { |
| 167 | + use super::Checksum; |
| 168 | + |
| 169 | + #[test] |
| 170 | + pub fn test_binary_representation() { |
| 171 | + assert_eq!(Checksum::from_i32(-1).value(), u32::MAX); |
| 172 | + assert_eq!(Checksum::from(u32::MAX).value(), u32::MAX); |
| 173 | + assert_eq!(Checksum::from(u32::MAX).bitcast_i32(), -1); |
| 174 | + } |
| 175 | + |
| 176 | + fn deserialize(from: &str) -> Checksum { |
| 177 | + serde_json::from_str(from).expect("should deserialize") |
| 178 | + } |
| 179 | + |
| 180 | + #[test] |
| 181 | + pub fn test_deserialize() { |
| 182 | + assert_eq!(deserialize("0").value(), 0); |
| 183 | + assert_eq!(deserialize("-1").value(), u32::MAX); |
| 184 | + assert_eq!(deserialize("-1.0").value(), u32::MAX); |
| 185 | + |
| 186 | + assert_eq!(deserialize("3573495687").value(), 3573495687); |
| 187 | + assert_eq!(deserialize("3573495687.0").value(), 3573495687); |
| 188 | + assert_eq!(deserialize("-721471609.0").value(), 3573495687); |
| 189 | + } |
| 190 | +} |
0 commit comments