Add ZcashSerialization impls for some std types.

This commit is contained in:
Henry de Valence 2019-09-14 08:57:08 -07:00
parent d202573f23
commit 2a7a1b60a8
1 changed files with 52 additions and 0 deletions

View File

@ -167,3 +167,55 @@ pub trait ZcashSerialization: Sized {
version: Version,
) -> Result<Self, SerializationError>;
}
impl ZcashSerialization for std::net::IpAddr {
fn write<W: io::Write>(
&self,
mut writer: W,
_magic: Magic,
_version: Version,
) -> Result<(), SerializationError> {
use std::net::IpAddr::*;
let v6_addr = match *self {
V4(ref addr) => addr.to_ipv6_mapped(),
V6(addr) => addr,
};
writer.write_all(&v6_addr.octets())?;
Ok(())
}
/// Try to read `self` from the given `reader`.
fn try_read<R: io::Read>(
_reader: R,
_magic: Magic,
_version: Version,
) -> Result<Self, SerializationError> {
unimplemented!()
}
}
// XXX because the serialization trait has both read and write methods, we have
// to implement it for String rather than impl<'a> ... for &'a str (as in that
// case we can't deserialize into a borrowed &'str, only an owned String), so we
// can't serialize 'static str
impl ZcashSerialization for String {
fn write<W: io::Write>(
&self,
mut writer: W,
_magic: Magic,
_version: Version,
) -> Result<(), SerializationError> {
writer.write_compactsize(self.len() as u64)?;
writer.write_all(self.as_bytes())?;
Ok(())
}
/// Try to read `self` from the given `reader`.
fn try_read<R: io::Read>(
_reader: R,
_magic: Magic,
_version: Version,
) -> Result<Self, SerializationError> {
unimplemented!()
}
}