Skip to content

Commit d330672

Browse files
committed
Implement serialization for [u16; 32], DRYing it with [u8; *]
In the next commit we'll need serialization for `[u16; 32]`, which we add here, unifying it with the `[u8; *]` serialization macro.
1 parent 3dffe54 commit d330672

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

lightning/src/util/ser.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -586,28 +586,34 @@ impl_array!(64); // for ecdsa::Signature and schnorr::Signature
586586
impl_array!(66); // for MuSig2 nonces
587587
impl_array!(1300); // for OnionPacket.hop_data
588588

589-
impl Writeable for [u16; 8] {
590-
#[inline]
591-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
592-
for v in self.iter() {
593-
w.write_all(&v.to_be_bytes())?
589+
macro_rules! impl_u16_array {
590+
($len: expr) => {
591+
impl Writeable for [u16; $len] {
592+
#[inline]
593+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
594+
for v in self.iter() {
595+
w.write_all(&v.to_be_bytes())?
596+
}
597+
Ok(())
598+
}
594599
}
595-
Ok(())
596-
}
597-
}
598600

599-
impl Readable for [u16; 8] {
600-
#[inline]
601-
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
602-
let mut buf = [0u8; 16];
603-
r.read_exact(&mut buf)?;
604-
let mut res = [0u16; 8];
605-
for (idx, v) in res.iter_mut().enumerate() {
606-
*v = (buf[idx*2] as u16) << 8 | (buf[idx*2 + 1] as u16)
601+
impl Readable for [u16; $len] {
602+
#[inline]
603+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
604+
let mut buf = [0u8; $len*2];
605+
r.read_exact(&mut buf)?;
606+
let mut res = [0u16; $len];
607+
for (idx, v) in res.iter_mut().enumerate() {
608+
*v = (buf[idx*2] as u16) << 8 | (buf[idx*2 + 1] as u16)
609+
}
610+
Ok(res)
611+
}
607612
}
608-
Ok(res)
609613
}
610614
}
615+
impl_u16_array!(8);
616+
impl_u16_array!(32);
611617

612618
/// A type for variable-length values within TLV record where the length is encoded as part of the record.
613619
/// Used to prevent encoding the length twice.

0 commit comments

Comments
 (0)