@@ -555,65 +555,49 @@ impl Readable for bool {
555
555
556
556
// u8 arrays
557
557
macro_rules! impl_array {
558
- ( $size: expr ) => (
559
- impl Writeable for [ u8 ; $size]
560
- {
558
+ ( $size: expr, $ty: ty) => (
559
+ impl Writeable for [ $ty; $size] {
561
560
#[ inline]
562
561
fn write<W : Writer >( & self , w: & mut W ) -> Result <( ) , io:: Error > {
563
- w. write_all( self )
564
- }
565
- }
566
-
567
- impl Readable for [ u8 ; $size]
568
- {
569
- #[ inline]
570
- fn read<R : Read >( r: & mut R ) -> Result <Self , DecodeError > {
571
- let mut buf = [ 0u8 ; $size] ;
572
- r. read_exact( & mut buf) ?;
573
- Ok ( buf)
574
- }
575
- }
576
- ) ;
577
- }
578
-
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
-
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( ) ) ?
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( ) ) ;
596
566
}
597
- Ok ( ( ) )
567
+ w . write_all ( & out )
598
568
}
599
569
}
600
570
601
- impl Readable for [ u16 ; $len ] {
571
+ impl Readable for [ $ty ; $size ] {
602
572
#[ inline]
603
573
fn read<R : Read >( r: & mut R ) -> Result <Self , DecodeError > {
604
- let mut buf = [ 0u8 ; $len * 2 ] ;
574
+ let mut buf = [ 0u8 ; $size * core :: mem :: size_of :: <$ty> ( ) ] ;
605
575
r. read_exact( & mut buf) ?;
606
- let mut res = [ 0u16 ; $len ] ;
576
+ let mut res = [ 0 ; $size ] ;
607
577
for ( idx, v) in res. iter_mut( ) . enumerate( ) {
608
- * v = ( buf[ idx* 2 ] as u16 ) << 8 | ( buf[ idx* 2 + 1 ] as u16 )
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) ;
609
582
}
610
583
Ok ( res)
611
584
}
612
585
}
613
- }
586
+ ) ;
614
587
}
615
- impl_u16_array ! ( 8 ) ;
616
- impl_u16_array ! ( 32 ) ;
588
+
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
598
+
599
+ impl_array ! ( 8 , u16 ) ;
600
+ impl_array ! ( 32 , u16 ) ;
617
601
618
602
/// A type for variable-length values within TLV record where the length is encoded as part of the record.
619
603
/// Used to prevent encoding the length twice.
0 commit comments