@@ -553,61 +553,50 @@ impl Readable for bool {
553553 }
554554}
555555
556- // u8 arrays
557556macro_rules! impl_array {
558- ( $size: expr ) => (
559- impl Writeable for [ u8 ; $size]
560- {
557+ ( $size: expr, $ty: ty) => (
558+ impl Writeable for [ $ty; $size] {
561559 #[ inline]
562560 fn write<W : Writer >( & self , w: & mut W ) -> Result <( ) , io:: Error > {
563- w. write_all( self )
561+ let mut out = [ 0 ; $size * core:: mem:: size_of:: <$ty>( ) ] ;
562+ for ( idx, v) in self . iter( ) . enumerate( ) {
563+ let startpos = idx * core:: mem:: size_of:: <$ty>( ) ;
564+ out[ startpos..startpos + core:: mem:: size_of:: <$ty>( ) ] . copy_from_slice( & v. to_be_bytes( ) ) ;
565+ }
566+ w. write_all( & out)
564567 }
565568 }
566569
567- impl Readable for [ u8 ; $size]
568- {
570+ impl Readable for [ $ty; $size] {
569571 #[ inline]
570572 fn read<R : Read >( r: & mut R ) -> Result <Self , DecodeError > {
571- let mut buf = [ 0u8 ; $size] ;
573+ let mut buf = [ 0u8 ; $size * core :: mem :: size_of :: <$ty> ( ) ] ;
572574 r. read_exact( & mut buf) ?;
573- Ok ( buf)
575+ let mut res = [ 0 ; $size] ;
576+ for ( idx, v) in res. iter_mut( ) . enumerate( ) {
577+ let startpos = idx * core:: mem:: size_of:: <$ty>( ) ;
578+ let mut arr = [ 0 ; core:: mem:: size_of:: <$ty>( ) ] ;
579+ arr. copy_from_slice( & buf[ startpos..startpos + core:: mem:: size_of:: <$ty>( ) ] ) ;
580+ * v = <$ty>:: from_be_bytes( arr) ;
581+ }
582+ Ok ( res)
574583 }
575584 }
576585 ) ;
577586}
578587
579- impl_array ! ( 3 ) ; // for rgb, ISO 4712 code
580- impl_array ! ( 4 ) ; // for IPv4
581- impl_array ! ( 12 ) ; // for OnionV2
582- impl_array ! ( 16 ) ; // for IPv6
583- impl_array ! ( 32 ) ; // for channel id & hmac
584- impl_array ! ( PUBLIC_KEY_SIZE ) ; // for PublicKey
585- impl_array ! ( 64 ) ; // for ecdsa::Signature and schnorr::Signature
586- impl_array ! ( 66 ) ; // for MuSig2 nonces
587- impl_array ! ( 1300 ) ; // for OnionPacket.hop_data
588+ impl_array ! ( 3 , u8 ) ; // for rgb, ISO 4712 code
589+ impl_array ! ( 4 , u8 ) ; // for IPv4
590+ impl_array ! ( 12 , u8 ) ; // for OnionV2
591+ impl_array ! ( 16 , u8 ) ; // for IPv6
592+ impl_array ! ( 32 , u8 ) ; // for channel id & hmac
593+ impl_array ! ( PUBLIC_KEY_SIZE , u8 ) ; // for PublicKey
594+ impl_array ! ( 64 , u8 ) ; // for ecdsa::Signature and schnorr::Signature
595+ impl_array ! ( 66 , u8 ) ; // for MuSig2 nonces
596+ impl_array ! ( 1300 , u8 ) ; // for OnionPacket.hop_data
588597
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 ( ) ) ?
594- }
595- Ok ( ( ) )
596- }
597- }
598-
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 )
607- }
608- Ok ( res)
609- }
610- }
598+ impl_array ! ( 8 , u16 ) ;
599+ impl_array ! ( 32 , u16 ) ;
611600
612601/// A type for variable-length values within TLV record where the length is encoded as part of the record.
613602/// Used to prevent encoding the length twice.
0 commit comments