Skip to content

Commit

Permalink
Use efficient serialization for non-human-readable formats
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Jan 11, 2021
1 parent fcbebc6 commit 3450ca8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -34,6 +34,7 @@ serde = { version = "1", features = [ "derive" ], optional = true }
[dev-dependencies]
serde_json = "<1.0.45"
serde_test = "1"
bincode = "1.3.1"
secp256k1 = { version = "0.19.0", features = [ "recovery", "rand-std" ] }
# We need to pin ryu (transitive dep from serde_json) to stay compatible with Rust 1.22.0
ryu = "<1.0.5"
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -47,6 +47,7 @@ pub extern crate bech32;
#[cfg(feature = "serde")] #[macro_use] extern crate serde;
#[cfg(all(test, feature = "serde"))] extern crate serde_json;
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
#[cfg(all(test, feature = "serde"))] extern crate bincode;
#[cfg(all(test, feature = "unstable"))] extern crate test;

#[cfg(target_pointer_width = "16")]
Expand Down
28 changes: 25 additions & 3 deletions src/util/uint.rs
Expand Up @@ -436,7 +436,12 @@ macro_rules! construct_uint {
S: $crate::serde::Serializer,
{
use $crate::hashes::hex::ToHex;
serializer.serialize_str(&self.to_be_bytes().to_hex())
let bytes = self.to_be_bytes();
if serializer.is_human_readable() {
serializer.serialize_str(&bytes.to_hex())
} else {
serializer.serialize_bytes(&bytes)
}
}
}

Expand All @@ -453,7 +458,7 @@ macro_rules! construct_uint {
type Value = $name;

fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "hex string with {} characters ({} bytes", $n_words * 8 * 2, $n_words * 8)
write!(f, "{} bytes or a hex string with {} characters", $n_words * 8, $n_words * 8 * 2)
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
Expand All @@ -465,8 +470,21 @@ macro_rules! construct_uint {
$name::from_be_slice(&bytes)
.map_err(|_| de::Error::invalid_length(bytes.len() * 2, &self))
}

fn visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
$name::from_be_slice(bytes)
.map_err(|_| de::Error::invalid_length(bytes.len(), &self))
}
}

if deserializer.is_human_readable() {
deserializer.deserialize_str(Visitor)
} else {
deserializer.deserialize_bytes(Visitor)
}
deserializer.deserialize_str(Visitor)
}
}
);
Expand Down Expand Up @@ -703,6 +721,10 @@ mod tests {
let json = format!("\"{}\"", hex);
assert_eq!(::serde_json::to_string(&uint).unwrap(), json);
assert_eq!(::serde_json::from_str::<Uint256>(&json).unwrap(), uint);

let bin_encoded = ::bincode::serialize(&uint).unwrap();
let bin_decoded: Uint256 = ::bincode::deserialize(&bin_encoded).unwrap();
assert_eq!(bin_decoded, uint);
};

check(
Expand Down

0 comments on commit 3450ca8

Please sign in to comment.