Serialize Sha256Hash without allocations

This commit is contained in:
Andrew Poelstra 2016-05-03 20:16:30 +00:00
parent f906c2fddd
commit 96b4b050fd
2 changed files with 11 additions and 2 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "bitcoin" name = "bitcoin"
version = "0.5.6" version = "0.5.7"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"] authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0" license = "CC0-1.0"
homepage = "https://github.com/apoelstra/rust-bitcoin/" homepage = "https://github.com/apoelstra/rust-bitcoin/"

View File

@ -231,7 +231,16 @@ impl serde::Serialize for Sha256dHash {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: serde::Serializer, where S: serde::Serializer,
{ {
serializer.visit_str(&self.be_hex_string()) unsafe {
use std::{char, mem, str};
let mut string: [u8; 64] = mem::uninitialized();
for i in 0..32 {
string[2 * i] = char::from_digit((self.0[31 - i] / 0x10) as u32, 16).unwrap() as u8;
string[2 * i + 1] = char::from_digit((self.0[31 - i] & 0x0f) as u32, 16).unwrap() as u8;
}
serializer.visit_str(str::from_utf8_unchecked(&string))
}
} }
} }