Merge pull request #1205 from TheBlueMatt/2021-12-new-feature-bit
[rust-lightning] / lightning / src / util / ser.rs
index ba8a9fd0d3551efe8062e907f4d52d5f2b635d51..6a52e5e1c898230c0d31a4cb9ce20c6acc236076 100644 (file)
@@ -27,6 +27,7 @@ use bitcoin::consensus::Encodable;
 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
 use bitcoin::hash_types::{Txid, BlockHash};
 use core::marker::Sized;
+use core::time::Duration;
 use ln::msgs::DecodeError;
 use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
 
@@ -474,10 +475,9 @@ macro_rules! impl_array {
        );
 }
 
-//TODO: performance issue with [u8; size] with impl_array!()
 impl_array!(3); // for rgb
 impl_array!(4); // for IPv4
-impl_array!(10); // for OnionV2
+impl_array!(12); // for OnionV2
 impl_array!(16); // for IPv6
 impl_array!(32); // for channel id & hmac
 impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
@@ -911,3 +911,19 @@ impl Readable for String {
                Ok(ret)
        }
 }
+
+impl Writeable for Duration {
+       #[inline]
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+               self.as_secs().write(w)?;
+               self.subsec_nanos().write(w)
+       }
+}
+impl Readable for Duration {
+       #[inline]
+       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               let secs = Readable::read(r)?;
+               let nanos = Readable::read(r)?;
+               Ok(Duration::new(secs, nanos))
+       }
+}