Skip to content

Commit f683b9d

Browse files
committed
Make it explicit that serialization is in u32
Add explicit type annotations to make sure we serialize `BigUint` like a `Vec<u32>`. If we ever change the internal representation, we still need to remain compatible with serialized data. Also, deserializing `BigUint` and `BigInt` now uses their normal constructor methods, to make sure they are normalized. We shouldn't assume that all incoming data is already well-formed.
1 parent e22271c commit f683b9d

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/bigint.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ impl serde::Serialize for Sign {
7373
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
7474
where S: serde::Serializer
7575
{
76+
// Note: do not change the serialization format, or it may break
77+
// forward and backward compatibility of serialized data!
7678
match *self {
7779
Sign::Minus => (-1i8).serialize(serializer),
7880
Sign::NoSign => 0i8.serialize(serializer),
@@ -1701,6 +1703,8 @@ impl serde::Serialize for BigInt {
17011703
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
17021704
where S: serde::Serializer
17031705
{
1706+
// Note: do not change the serialization format, or it may break
1707+
// forward and backward compatibility of serialized data!
17041708
(self.sign, &self.data).serialize(serializer)
17051709
}
17061710
}
@@ -1711,10 +1715,7 @@ impl<'de> serde::Deserialize<'de> for BigInt {
17111715
where D: serde::Deserializer<'de>
17121716
{
17131717
let (sign, data) = serde::Deserialize::deserialize(deserializer)?;
1714-
Ok(BigInt {
1715-
sign: sign,
1716-
data: data,
1717-
})
1718+
Ok(BigInt::from_biguint(sign, data))
17181719
}
17191720
}
17201721

src/biguint.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,11 @@ impl serde::Serialize for BigUint {
17451745
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
17461746
where S: serde::Serializer
17471747
{
1748-
self.data.serialize(serializer)
1748+
// Note: do not change the serialization format, or it may break forward
1749+
// and backward compatibility of serialized data! If we ever change the
1750+
// internal representation, we should still serialize in base-`u32`.
1751+
let data: &Vec<u32> = &self.data;
1752+
data.serialize(serializer)
17491753
}
17501754
}
17511755

@@ -1754,8 +1758,8 @@ impl<'de> serde::Deserialize<'de> for BigUint {
17541758
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
17551759
where D: serde::Deserializer<'de>
17561760
{
1757-
let data = try!(Vec::deserialize(deserializer));
1758-
Ok(BigUint { data: data })
1761+
let data: Vec<u32> = try!(Vec::deserialize(deserializer));
1762+
Ok(BigUint::new(data))
17591763
}
17601764
}
17611765

0 commit comments

Comments
 (0)