Simlify consensus_encode vectors
This commit is contained in:
parent
1b946b0034
commit
eb7369b1db
|
@ -150,7 +150,8 @@ impl From<psbt::Error> for Error {
|
||||||
/// Encode an object into a vector
|
/// Encode an object into a vector
|
||||||
pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
|
pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
|
||||||
let mut encoder = Cursor::new(vec![]);
|
let mut encoder = Cursor::new(vec![]);
|
||||||
data.consensus_encode(&mut encoder).unwrap();
|
let len = data.consensus_encode(&mut encoder).unwrap();
|
||||||
|
assert_eq!(len, encoder.get_ref().len());
|
||||||
encoder.into_inner()
|
encoder.into_inner()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +279,7 @@ impl<W: Write> WriteExt for W {
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn emit_bool(&mut self, v: bool) -> Result<(), Error> {
|
fn emit_bool(&mut self, v: bool) -> Result<(), Error> {
|
||||||
self.write_all(&[if v {1} else {0}]).map_err(Error::Io)
|
self.write_all(&[v as u8]).map_err(Error::Io)
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn emit_slice(&mut self, v: &[u8]) -> Result<(), Error> {
|
fn emit_slice(&mut self, v: &[u8]) -> Result<(), Error> {
|
||||||
|
@ -350,7 +351,6 @@ macro_rules! impl_int_encodable{
|
||||||
ReadExt::$meth_dec(&mut d).map($ty::from_le)
|
ReadExt::$meth_dec(&mut d).map($ty::from_le)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for $ty {
|
impl Encodable for $ty {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: WriteExt>(
|
fn consensus_encode<S: WriteExt>(
|
||||||
|
@ -454,7 +454,7 @@ impl Decodable for VarInt {
|
||||||
impl Encodable for bool {
|
impl Encodable for bool {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: WriteExt>(&self, mut s: S) -> Result<usize, Error> {
|
fn consensus_encode<S: WriteExt>(&self, mut s: S) -> Result<usize, Error> {
|
||||||
s.emit_u8(if *self {1} else {0})?;
|
s.emit_bool(*self)?;
|
||||||
Ok(1)
|
Ok(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -462,7 +462,7 @@ impl Encodable for bool {
|
||||||
impl Decodable for bool {
|
impl Decodable for bool {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_decode<D: io::Read>(mut d: D) -> Result<bool, Error> {
|
fn consensus_decode<D: io::Read>(mut d: D) -> Result<bool, Error> {
|
||||||
ReadExt::read_u8(&mut d).map(|n| n != 0)
|
ReadExt::read_bool(&mut d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -575,7 +575,6 @@ macro_rules! impl_vec {
|
||||||
Ok(len)
|
Ok(len)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decodable for Vec<$type> {
|
impl Decodable for Vec<$type> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
|
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
|
||||||
|
@ -606,12 +605,17 @@ impl_vec!(Vec<u8>);
|
||||||
impl_vec!((u32, Address));
|
impl_vec!((u32, Address));
|
||||||
impl_vec!(u64);
|
impl_vec!(u64);
|
||||||
|
|
||||||
|
fn consensus_encode_with_size<S: io::Write>(data: &[u8], mut s: S) -> Result<usize, Error> {
|
||||||
|
let vi_len = VarInt(data.len() as u64).consensus_encode(&mut s)?;
|
||||||
|
s.emit_slice(&data)?;
|
||||||
|
Ok(vi_len + data.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Encodable for Vec<u8> {
|
impl Encodable for Vec<u8> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, Error> {
|
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
|
||||||
let vi_len = VarInt(self.len() as u64).consensus_encode(&mut s)?;
|
consensus_encode_with_size(self, s)
|
||||||
s.emit_slice(&self)?;
|
|
||||||
Ok(vi_len + self.len())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -622,8 +626,7 @@ impl Decodable for Vec<u8> {
|
||||||
if len > MAX_VEC_SIZE {
|
if len > MAX_VEC_SIZE {
|
||||||
return Err(self::Error::OversizedVectorAllocation { requested: len, max: MAX_VEC_SIZE })
|
return Err(self::Error::OversizedVectorAllocation { requested: len, max: MAX_VEC_SIZE })
|
||||||
}
|
}
|
||||||
let mut ret = Vec::with_capacity(len);
|
let mut ret = vec![0u8; len];
|
||||||
ret.resize(len, 0);
|
|
||||||
d.read_slice(&mut ret)?;
|
d.read_slice(&mut ret)?;
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
@ -631,25 +634,15 @@ impl Decodable for Vec<u8> {
|
||||||
|
|
||||||
impl Encodable for Box<[u8]> {
|
impl Encodable for Box<[u8]> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, Error> {
|
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, Error> {
|
||||||
let vi_len = VarInt(self.len() as u64).consensus_encode(&mut s)?;
|
consensus_encode_with_size(self, s)
|
||||||
s.emit_slice(&self)?;
|
|
||||||
Ok(vi_len + self.len())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decodable for Box<[u8]> {
|
impl Decodable for Box<[u8]> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
|
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
|
||||||
let len = VarInt::consensus_decode(&mut d)?.0;
|
<Vec<u8>>::consensus_decode(d).map(From::from)
|
||||||
let len = len as usize;
|
|
||||||
if len > MAX_VEC_SIZE {
|
|
||||||
return Err(self::Error::OversizedVectorAllocation { requested: len, max: MAX_VEC_SIZE })
|
|
||||||
}
|
|
||||||
let mut ret = Vec::with_capacity(len);
|
|
||||||
ret.resize(len, 0);
|
|
||||||
d.read_slice(&mut ret)?;
|
|
||||||
Ok(ret.into_boxed_slice())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -682,8 +675,7 @@ impl Decodable for CheckedData {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let checksum = <[u8; 4]>::consensus_decode(&mut d)?;
|
let checksum = <[u8; 4]>::consensus_decode(&mut d)?;
|
||||||
let mut ret = Vec::with_capacity(len as usize);
|
let mut ret = vec![0u8; len as usize];
|
||||||
ret.resize(len as usize, 0);
|
|
||||||
d.read_slice(&mut ret)?;
|
d.read_slice(&mut ret)?;
|
||||||
let expected_checksum = sha2_checksum(&ret);
|
let expected_checksum = sha2_checksum(&ret);
|
||||||
if expected_checksum != checksum {
|
if expected_checksum != checksum {
|
||||||
|
|
Loading…
Reference in New Issue