Skip to content

Commit 36546c8

Browse files
committed
refactor: improve shortcutting deser
1 parent 24bd4e5 commit 36546c8

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/support/serde.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,15 @@ impl<'de, const BITS: usize, const LIMBS: usize> Visitor<'de> for StrVisitor<BIT
120120
where
121121
E: Error,
122122
{
123-
// ensure the string is the correct length (two characters in the string per
124-
// byte) exception: zero, for a Uint where BITS == 0
125-
if BITS == 0 {
126-
// special case, this class of ints has one member, zero.
127-
// zero is represented as "0x0" only
128-
if value != ZERO_STR {
129-
return Err(Error::invalid_value(Unexpected::Str(value), &self));
130-
}
123+
// Shortcut for common case
124+
if value == ZERO_STR {
131125
return Ok(Uint::<BITS, LIMBS>::ZERO);
132126
}
127+
// `ZERO_STR` is the only valid serialization of `Uint<0, 0>`, so if we
128+
// have not shortcut, we are in an error case
129+
if BITS == 0 {
130+
return Err(Error::invalid_value(Unexpected::Str(value), &self));
131+
}
133132

134133
let value = trim_hex_prefix(value);
135134
if nbytes(BITS) * 2 < value.len() {
@@ -150,7 +149,7 @@ impl<'de, const BITS: usize, const LIMBS: usize> Visitor<'de> for StrVisitor<BIT
150149
}
151150
limbs[i] = limb;
152151
}
153-
if BITS > 0 && limbs[LIMBS - 1] > Self::Value::MASK {
152+
if limbs[LIMBS - 1] > Self::Value::MASK {
154153
return Err(Error::invalid_value(Unexpected::Str(value), &self));
155154
}
156155
Ok(Uint::from_limbs(limbs))

0 commit comments

Comments
 (0)