refactor: move macro

This commit is contained in:
Reed Rosenbluth
2021-05-10 16:34:20 -04:00
parent 2ceb461e0d
commit 45f9d12a7e
2 changed files with 22 additions and 21 deletions

View File

@@ -13,3 +13,25 @@ macro_rules! impl_stacks_message_codec_for_int {
}
};
}
macro_rules! impl_byte_array_message_codec {
($thing:ident, $len:expr) => {
impl ::codec::StacksMessageCodec for $thing {
fn consensus_serialize<W: std::io::Write>(
&self,
fd: &mut W,
) -> Result<(), ::codec::Error> {
fd.write_all(self.as_bytes())
.map_err(::codec::Error::WriteError)
}
fn consensus_deserialize<R: std::io::Read>(
fd: &mut R,
) -> Result<$thing, ::codec::Error> {
let mut buf = [0u8; ($len as usize)];
fd.read_exact(&mut buf).map_err(::codec::Error::ReadError)?;
let ret = $thing::from_bytes(&buf).expect("BUG: buffer is not the right size");
Ok(ret)
}
}
};
}

View File

@@ -1515,27 +1515,6 @@ pub const GETPOXINV_MAX_BITLEN: u64 = 8;
// message.
pub const BLOCKS_PUSHED_MAX: u32 = 32;
macro_rules! impl_byte_array_message_codec {
($thing:ident, $len:expr) => {
impl ::codec::StacksMessageCodec for $thing {
fn consensus_serialize<W: std::io::Write>(
&self,
fd: &mut W,
) -> Result<(), ::codec::Error> {
fd.write_all(self.as_bytes())
.map_err(::codec::Error::WriteError)
}
fn consensus_deserialize<R: std::io::Read>(
fd: &mut R,
) -> Result<$thing, ::codec::Error> {
let mut buf = [0u8; ($len as usize)];
fd.read_exact(&mut buf).map_err(::codec::Error::ReadError)?;
let ret = $thing::from_bytes(&buf).expect("BUG: buffer is not the right size");
Ok(ret)
}
}
};
}
impl_byte_array_message_codec!(ConsensusHash, 20);
impl_byte_array_message_codec!(Hash160, 20);
impl_byte_array_message_codec!(BurnchainHeaderHash, 32);