Add serialize_hex method to do string conversion for serializations

This commit is contained in:
Andrew Poelstra 2014-08-22 20:53:49 -07:00
parent 18a61dd560
commit 7eadf72a1a
1 changed files with 7 additions and 0 deletions

View File

@ -21,6 +21,7 @@
use collections::Vec; use collections::Vec;
use std::io::{IoError, IoResult, OtherIoError, MemReader, MemWriter}; use std::io::{IoError, IoResult, OtherIoError, MemReader, MemWriter};
use serialize::hex::ToHex;
use network::encodable::{ConsensusDecodable, ConsensusEncodable}; use network::encodable::{ConsensusDecodable, ConsensusEncodable};
use util::hash::Sha256dHash; use util::hash::Sha256dHash;
@ -44,6 +45,12 @@ pub fn serialize<T: ConsensusEncodable<RawEncoder<MemWriter>, IoError>>(obj: &T)
Ok(encoder.unwrap().unwrap()) Ok(encoder.unwrap().unwrap())
} }
/// Encode an object into a hex-encoded string
pub fn serialize_hex<T: ConsensusEncodable<RawEncoder<MemWriter>, IoError>>(obj: &T) -> IoResult<String> {
let serial = try!(serialize(obj));
Ok(serial.as_slice().to_hex())
}
/// Deserialize an object from a vector /// Deserialize an object from a vector
pub fn deserialize<T: ConsensusDecodable<RawDecoder<MemReader>, IoError>>(data: Vec<u8>) -> IoResult<T> { pub fn deserialize<T: ConsensusDecodable<RawDecoder<MemReader>, IoError>>(data: Vec<u8>) -> IoResult<T> {
let mut decoder = RawDecoder::new(MemReader::new(data)); let mut decoder = RawDecoder::new(MemReader::new(data));