msg.0[..2].copy_from_slice(&(len as u16 - 2).to_be_bytes());
msg.0
}
+
+ /// Gets the length of this object after it has been serialized. This can be overridden to
+ /// optimize cases where we prepend an object with its length.
+ // Note that LLVM optimizes this away in most cases! Check that it isn't before you override!
+ #[inline]
+ fn serialized_length(&self) -> usize {
+ let mut len_calc = LengthCalculatingWriter(0);
+ self.write(&mut len_calc).expect("No in-memory data may fail to serialize");
+ len_calc.0
+ }
}
impl<'a, T: Writeable> Writeable for &'a T {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
self.serialize().write(w)
}
+ #[inline]
+ fn serialized_length(&self) -> usize {
+ PUBLIC_KEY_SIZE
+ }
}
impl Readable for PublicKey {
ser.copy_from_slice(&self[..]);
ser.write(w)
}
+ #[inline]
+ fn serialized_length(&self) -> usize {
+ 32
+ }
}
impl Readable for SecretKey {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
self.serialize_compact().write(w)
}
+ #[inline]
+ fn serialized_length(&self) -> usize {
+ COMPACT_SIGNATURE_SIZE
+ }
}
impl Readable for Signature {
match *self {
None => 0u8.write(w)?,
Some(ref data) => {
- let mut len_calc = LengthCalculatingWriter(0);
- data.write(&mut len_calc).expect("No in-memory data may fail to serialize");
- BigSize(len_calc.0 as u64 + 1).write(w)?;
+ BigSize(data.serialized_length() as u64 + 1).write(w)?;
data.write(w)?;
}
}
macro_rules! encode_tlv {
($stream: expr, {$(($type: expr, $field: expr)),*}, {$(($optional_type: expr, $optional_field: expr)),*}) => { {
#[allow(unused_imports)]
- use util::ser::{BigSize, LengthCalculatingWriter};
+ use util::ser::BigSize;
// Fields must be serialized in order, so we have to potentially switch between optional
// fields and normal fields while serializing. Thus, we end up having to loop over the type
// counts.
$(
if i == $type {
BigSize($type).write($stream)?;
- let mut len_calc = LengthCalculatingWriter(0);
- $field.write(&mut len_calc)?;
- BigSize(len_calc.0 as u64).write($stream)?;
+ BigSize($field.serialized_length() as u64).write($stream)?;
$field.write($stream)?;
}
)*
if i == $optional_type {
if let Some(ref field) = $optional_field {
BigSize($optional_type).write($stream)?;
- let mut len_calc = LengthCalculatingWriter(0);
- field.write(&mut len_calc)?;
- BigSize(len_calc.0 as u64).write($stream)?;
+ BigSize(field.serialized_length() as u64).write($stream)?;
field.write($stream)?;
}
}
{
$(
BigSize($type).write(&mut len)?;
- let mut field_len = LengthCalculatingWriter(0);
- $field.write(&mut field_len)?;
- BigSize(field_len.0 as u64).write(&mut len)?;
- len.0 += field_len.0;
+ let field_len = $field.serialized_length();
+ BigSize(field_len as u64).write(&mut len)?;
+ len.0 += field_len;
)*
$(
if let Some(ref field) = $optional_field {
BigSize($optional_type).write(&mut len)?;
- let mut field_len = LengthCalculatingWriter(0);
- field.write(&mut field_len)?;
- BigSize(field_len.0 as u64).write(&mut len)?;
- len.0 += field_len.0;
+ let field_len = field.serialized_length();
+ BigSize(field_len as u64).write(&mut len)?;
+ len.0 += field_len;
}
)*
}