Skip to content

Commit 76389a0

Browse files
committed
Consistent unsigned representation
Fix: #43 Fix: #38
1 parent 0fd7f13 commit 76389a0

File tree

2 files changed

+72
-73
lines changed

2 files changed

+72
-73
lines changed

mina-p2p-messages/src/number.rs

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ pub struct Number<T>(pub T);
99

1010
impl<T: std::fmt::Debug> std::fmt::Debug for Number<T> {
1111
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12-
let Self(inner) = self;
1312
// Avoid vertical alignment
14-
f.write_fmt(format_args!("Number({:?})", inner))
13+
f.write_fmt(format_args!("Number({inner:?})", inner = self.0))
1514
}
1615
}
1716

@@ -139,85 +138,85 @@ where
139138
}
140139
}
141140

142-
impl<T> binprot::BinProtRead for Number<T>
143-
where
144-
T: binprot::BinProtRead,
145-
{
146-
fn binprot_read<R: std::io::Read + ?Sized>(r: &mut R) -> Result<Self, binprot::Error>
147-
where
148-
Self: Sized,
149-
{
150-
T::binprot_read(r).map(Self)
151-
}
152-
}
141+
macro_rules! binprot_number {
142+
($base_type:ident, $binprot_type:ident) => {
143+
impl binprot::BinProtRead for Number<$base_type> {
144+
fn binprot_read<R: std::io::Read + ?Sized>(r: &mut R) -> Result<Self, binprot::Error>
145+
where
146+
Self: Sized,
147+
{
148+
$binprot_type::binprot_read(r).map(|v| Self(v as $base_type))
149+
}
150+
}
153151

154-
impl<T> binprot::BinProtWrite for Number<T>
155-
where
156-
T: binprot::BinProtWrite,
157-
{
158-
fn binprot_write<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
159-
self.0.binprot_write(w)
160-
}
152+
impl binprot::BinProtWrite for Number<$base_type> {
153+
fn binprot_write<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
154+
(self.0 as $binprot_type).binprot_write(w)
155+
}
156+
}
157+
};
161158
}
162159

160+
binprot_number!(i32, i32);
161+
binprot_number!(i64, i64);
162+
binprot_number!(u32, i32);
163+
binprot_number!(u64, i64);
164+
binprot_number!(f64, f64);
165+
163166
#[cfg(test)]
164167
mod tests {
165168
use binprot::{BinProtRead, BinProtWrite};
166169

167-
#[test]
168-
fn u32_roundtrip() {
169-
for u32 in [
170-
0,
171-
1,
172-
u8::MAX as u32,
173-
u16::MAX as u32,
174-
u32::MAX,
175-
i8::MAX as u32,
176-
i16::MAX as u32,
177-
i32::MAX as u32,
178-
] {
179-
let mut buf = Vec::new();
180-
u32.binprot_write(&mut buf).unwrap();
181-
let mut r = buf.as_slice();
182-
if u32 <= 0x7f {
183-
assert_eq!(r[0], u32 as u8);
184-
} else {
185-
assert!(matches!(r[0], 0xfe | 0xfd | 0xfc));
170+
macro_rules! number_test {
171+
($name:ident, $ty:ident) => {
172+
#[test]
173+
fn $name() {
174+
for n in [
175+
0,
176+
1,
177+
u8::MAX as $ty,
178+
u16::MAX as $ty,
179+
u32::MAX as $ty,
180+
u64::MAX as $ty,
181+
i8::MAX as $ty,
182+
i16::MAX as $ty,
183+
i32::MAX as $ty,
184+
i64::MAX as $ty,
185+
] {
186+
let n: super::Number<$ty> = n.into();
187+
let mut buf = Vec::new();
188+
n.binprot_write(&mut buf).unwrap();
189+
let mut r = buf.as_slice();
190+
let n_ = super::Number::<$ty>::binprot_read(&mut r).unwrap();
191+
assert_eq!(r.len(), 0);
192+
assert_eq!(n, n_);
193+
}
186194
}
187-
let u32_ = u32::binprot_read(&mut r).unwrap();
188-
assert_eq!(r.len(), 0);
189-
assert_eq!(u32, u32_);
190-
}
195+
};
191196
}
192197

193-
#[test]
194-
fn i32_roundtrip() {
195-
for i32 in [
196-
0,
197-
1,
198-
u8::MAX as i32,
199-
u16::MAX as i32,
200-
u32::MAX as i32,
201-
i8::MAX as i32,
202-
i16::MAX as i32,
203-
i32::MAX as i32,
204-
i8::MIN as i32,
205-
i16::MIN as i32,
206-
i32::MIN as i32,
207-
] {
208-
let mut buf = Vec::new();
209-
i32.binprot_write(&mut buf).unwrap();
210-
let mut r = buf.as_slice();
211-
if -0x80 <= i32 && i32 < 0 {
212-
assert_eq!(r[0], 0xff);
213-
} else if 0 <= i32 && i32 <= 0x80 {
214-
assert_eq!(r[0], i32 as u8);
215-
} else {
216-
assert!(matches!(r[0], 0xfe | 0xfd | 0xfc));
198+
macro_rules! max_number_test {
199+
($name:ident, $ty:ident) => {
200+
#[test]
201+
fn $name() {
202+
let binprot = b"\xff\xff";
203+
let mut r = &binprot[..];
204+
let n = super::Number::<$ty>::binprot_read(&mut r).unwrap();
205+
assert_eq!(n.0, $ty::MAX);
206+
207+
let n: super::Number<$ty> = $ty::MAX.into();
208+
let mut buf = Vec::new();
209+
n.binprot_write(&mut buf).unwrap();
210+
assert_eq!(buf.as_slice(), b"\xff\xff");
217211
}
218-
let i32_ = i32::binprot_read(&mut r).unwrap();
219-
assert_eq!(r.len(), 0);
220-
assert_eq!(i32, i32_);
221-
}
212+
};
222213
}
214+
215+
number_test!(i32_roundtrip, i32);
216+
number_test!(u32_roundtrip, u32);
217+
number_test!(i64_roundtrip, i64);
218+
number_test!(u64_roundtrip, u64);
219+
220+
max_number_test!(u32_max, u32);
221+
max_number_test!(u64_max, u64);
223222
}

mina-p2p-messages/src/v2/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ pub struct NonZeroCurvePointUncompressedStableV1 {
792792
/// Gid: `125`
793793
/// Location: [src/int64.ml:6:6](https://github.com/MinaProtocol/mina/blob/14047c5551/src/int64.ml#L6)
794794
#[derive(Clone, PartialEq, Serialize, Deserialize, BinProtRead, BinProtWrite, Deref)]
795-
pub struct UnsignedExtendedUInt64Int64ForVersionTagsStableV1(pub crate::number::Int64);
795+
pub struct UnsignedExtendedUInt64Int64ForVersionTagsStableV1(pub crate::number::UInt64);
796796

797797
/// **OCaml name**: `Unsigned_extended.UInt32.Stable.V1`
798798
///
@@ -803,7 +803,7 @@ pub struct UnsignedExtendedUInt64Int64ForVersionTagsStableV1(pub crate::number::
803803
/// Gid: `119`
804804
/// Location: [src/int32.ml:6:6](https://github.com/MinaProtocol/mina/blob/14047c5551/src/int32.ml#L6)
805805
#[derive(Clone, PartialEq, Serialize, Deserialize, BinProtRead, BinProtWrite, Deref, Default)]
806-
pub struct UnsignedExtendedUInt32StableV1(pub crate::number::Int32);
806+
pub struct UnsignedExtendedUInt32StableV1(pub crate::number::UInt32);
807807

808808
/// **OCaml name**: `Mina_numbers__Nat.Make32.Stable.V1`
809809
///

0 commit comments

Comments
 (0)