@@ -555,59 +555,49 @@ impl Readable for bool {
555555
556556// u8 arrays
557557macro_rules! impl_array {
558- ( $size: expr ) => (
559- impl Writeable for [ u8 ; $size]
560- {
558+ ( $size: expr, $ty: ty) => (
559+ impl Writeable for [ $ty; $size] {
561560 #[ inline]
562561 fn write<W : Writer >( & self , w: & mut W ) -> Result <( ) , io:: Error > {
563- w. write_all( self )
562+ let mut out = [ 0 ; $size * core:: mem:: size_of:: <$ty>( ) ] ;
563+ for ( idx, v) in self . iter( ) . enumerate( ) {
564+ let startpos = idx * core:: mem:: size_of:: <$ty>( ) ;
565+ out[ startpos..startpos + core:: mem:: size_of:: <$ty>( ) ] . copy_from_slice( & v. to_be_bytes( ) ) ;
566+ }
567+ w. write_all( & out)
564568 }
565569 }
566570
567- impl Readable for [ u8 ; $size]
568- {
571+ impl Readable for [ $ty; $size] {
569572 #[ inline]
570573 fn read<R : Read >( r: & mut R ) -> Result <Self , DecodeError > {
571- let mut buf = [ 0u8 ; $size] ;
574+ let mut buf = [ 0u8 ; $size * core :: mem :: size_of :: <$ty> ( ) ] ;
572575 r. read_exact( & mut buf) ?;
573- Ok ( buf)
576+ let mut res = [ 0 ; $size] ;
577+ for ( idx, v) in res. iter_mut( ) . enumerate( ) {
578+ let startpos = idx * core:: mem:: size_of:: <$ty>( ) ;
579+ let mut arr = [ 0 ; core:: mem:: size_of:: <$ty>( ) ] ;
580+ arr. copy_from_slice( & buf[ startpos..startpos + core:: mem:: size_of:: <$ty>( ) ] ) ;
581+ * v = <$ty>:: from_be_bytes( arr) ;
582+ }
583+ Ok ( res)
574584 }
575585 }
576586 ) ;
577587}
578588
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
589+ impl_array ! ( 3 , u8 ) ; // for rgb, ISO 4712 code
590+ impl_array ! ( 4 , u8 ) ; // for IPv4
591+ impl_array ! ( 12 , u8 ) ; // for OnionV2
592+ impl_array ! ( 16 , u8 ) ; // for IPv6
593+ impl_array ! ( 32 , u8 ) ; // for channel id & hmac
594+ impl_array ! ( PUBLIC_KEY_SIZE , u8 ) ; // for PublicKey
595+ impl_array ! ( 64 , u8 ) ; // for ecdsa::Signature and schnorr::Signature
596+ impl_array ! ( 66 , u8 ) ; // for MuSig2 nonces
597+ impl_array ! ( 1300 , u8 ) ; // for OnionPacket.hop_data
588598
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- }
599+ impl_array ! ( 8 , u16 ) ;
600+ impl_array ! ( 32 , u16 ) ;
611601
612602/// A type for variable-length values within TLV record where the length is encoded as part of the record.
613603/// Used to prevent encoding the length twice.
0 commit comments