@@ -96,7 +96,7 @@ use core::{cmp, fmt};
96
96
use alloc:: vec:: Vec ;
97
97
98
98
mod sealed {
99
- use super :: { FeatureFlags , Features } ;
99
+ use super :: Features ;
100
100
101
101
/// The context in which [`Features`] are applicable. Defines which features are known to the
102
102
/// implementation, though specification of them as required or optional is up to the code
@@ -310,54 +310,54 @@ mod sealed {
310
310
311
311
/// Sets the feature's required (even) bit in the given flags.
312
312
#[ inline]
313
- fn set_required_bit( flags : & mut FeatureFlags ) {
314
- if flags. len( ) <= Self :: BYTE_OFFSET {
315
- flags. resize( Self :: BYTE_OFFSET + 1 , 0u8 ) ;
313
+ fn set_required_bit( obj : & mut Features < Self > ) {
314
+ if obj . flags. len( ) <= Self :: BYTE_OFFSET {
315
+ obj . flags. resize( Self :: BYTE_OFFSET + 1 , 0u8 ) ;
316
316
}
317
317
318
- flags[ Self :: BYTE_OFFSET ] |= Self :: REQUIRED_MASK ;
319
- flags[ Self :: BYTE_OFFSET ] &= !Self :: OPTIONAL_MASK ;
318
+ obj . flags[ Self :: BYTE_OFFSET ] |= Self :: REQUIRED_MASK ;
319
+ obj . flags[ Self :: BYTE_OFFSET ] &= !Self :: OPTIONAL_MASK ;
320
320
}
321
321
322
322
/// Sets the feature's optional (odd) bit in the given flags.
323
323
#[ inline]
324
- fn set_optional_bit( flags : & mut FeatureFlags ) {
325
- if flags. len( ) <= Self :: BYTE_OFFSET {
326
- flags. resize( Self :: BYTE_OFFSET + 1 , 0u8 ) ;
324
+ fn set_optional_bit( obj : & mut Features < Self > ) {
325
+ if obj . flags. len( ) <= Self :: BYTE_OFFSET {
326
+ obj . flags. resize( Self :: BYTE_OFFSET + 1 , 0u8 ) ;
327
327
}
328
328
329
- flags[ Self :: BYTE_OFFSET ] |= Self :: OPTIONAL_MASK ;
329
+ obj . flags[ Self :: BYTE_OFFSET ] |= Self :: OPTIONAL_MASK ;
330
330
}
331
331
332
332
/// Clears the feature's required (even) and optional (odd) bits from the given
333
333
/// flags.
334
334
#[ inline]
335
- fn clear_bits( flags : & mut FeatureFlags ) {
336
- if flags. len( ) > Self :: BYTE_OFFSET {
337
- flags[ Self :: BYTE_OFFSET ] &= !Self :: REQUIRED_MASK ;
338
- flags[ Self :: BYTE_OFFSET ] &= !Self :: OPTIONAL_MASK ;
335
+ fn clear_bits( obj : & mut Features < Self > ) {
336
+ if obj . flags. len( ) > Self :: BYTE_OFFSET {
337
+ obj . flags[ Self :: BYTE_OFFSET ] &= !Self :: REQUIRED_MASK ;
338
+ obj . flags[ Self :: BYTE_OFFSET ] &= !Self :: OPTIONAL_MASK ;
339
339
}
340
340
341
- let last_non_zero_byte = flags. iter( ) . rposition( |& byte| byte != 0 ) ;
341
+ let last_non_zero_byte = obj . flags. iter( ) . rposition( |& byte| byte != 0 ) ;
342
342
let size = if let Some ( offset) = last_non_zero_byte { offset + 1 } else { 0 } ;
343
- flags. resize( size, 0u8 ) ;
343
+ obj . flags. resize( size, 0u8 ) ;
344
344
}
345
345
}
346
346
347
347
impl <T : $feature> Features <T > {
348
348
/// Set this feature as optional.
349
349
pub fn $optional_setter( & mut self ) {
350
- <T as $feature>:: set_optional_bit( & mut self . flags ) ;
350
+ <T as $feature>:: set_optional_bit( self ) ;
351
351
}
352
352
353
353
/// Set this feature as required.
354
354
pub fn $required_setter( & mut self ) {
355
- <T as $feature>:: set_required_bit( & mut self . flags ) ;
355
+ <T as $feature>:: set_required_bit( self ) ;
356
356
}
357
357
358
358
/// Unsets this feature.
359
359
pub fn $clear( & mut self ) {
360
- <T as $feature>:: clear_bits( & mut self . flags ) ;
360
+ <T as $feature>:: clear_bits( self ) ;
361
361
}
362
362
363
363
/// Checks if this feature is supported.
@@ -718,15 +718,17 @@ const DIRECT_ALLOC_BYTES: usize = if sealed::MIN_FEATURES_ALLOCATION_BYTES > 8 *
718
718
} ;
719
719
const _ASSERT: ( ) = assert ! ( DIRECT_ALLOC_BYTES <= u8 :: MAX as usize ) ;
720
720
721
- /// The internal storage for feature flags. Public only for the [`sealed`] feature flag traits but
722
- /// generally should never be used directly.
723
721
#[ derive( Clone , PartialEq , Eq ) ]
724
- #[ doc( hidden) ]
725
- pub enum FeatureFlags {
722
+ enum FeatureFlagsType {
726
723
Held { bytes : [ u8 ; DIRECT_ALLOC_BYTES ] , len : u8 } ,
727
724
Heap ( Vec < u8 > ) ,
728
725
}
729
726
727
+ #[ cfg( fuzzing) ]
728
+ pub use FeatureFlagsType as FeatureFlags ;
729
+ #[ cfg( not( fuzzing) ) ]
730
+ use FeatureFlagsType as FeatureFlags ;
731
+
730
732
impl FeatureFlags {
731
733
fn empty ( ) -> Self {
732
734
Self :: Held { bytes : [ 0 ; DIRECT_ALLOC_BYTES ] , len : 0 }
@@ -826,7 +828,7 @@ impl fmt::Debug for FeatureFlags {
826
828
///
827
829
/// This is not exported to bindings users as we map the concrete feature types below directly instead
828
830
#[ derive( Eq ) ]
829
- pub struct Features < T : sealed:: Context > {
831
+ pub struct Features < T : sealed:: Context + ? Sized > {
830
832
/// Note that, for convenience, flags is LITTLE endian (despite being big-endian on the wire)
831
833
flags : FeatureFlags ,
832
834
mark : PhantomData < T > ,
@@ -865,7 +867,7 @@ impl<T: sealed::Context> Hash for Features<T> {
865
867
nonzero_flags. hash ( hasher) ;
866
868
}
867
869
}
868
- impl < T : sealed:: Context > PartialEq for Features < T > {
870
+ impl < T : sealed:: Context + ? Sized > PartialEq for Features < T > {
869
871
fn eq ( & self , o : & Self ) -> bool {
870
872
let mut o_iter = o. flags . iter ( ) ;
871
873
let mut self_iter = self . flags . iter ( ) ;
@@ -1000,17 +1002,15 @@ impl ChannelTypeFeatures {
1000
1002
/// Constructs a ChannelTypeFeatures with only static_remotekey set
1001
1003
pub fn only_static_remote_key ( ) -> Self {
1002
1004
let mut ret = Self :: empty ( ) ;
1003
- <sealed:: ChannelTypeContext as sealed:: StaticRemoteKey >:: set_required_bit ( & mut ret. flags ) ;
1005
+ <sealed:: ChannelTypeContext as sealed:: StaticRemoteKey >:: set_required_bit ( & mut ret) ;
1004
1006
ret
1005
1007
}
1006
1008
1007
1009
/// Constructs a ChannelTypeFeatures with anchors support
1008
1010
pub fn anchors_zero_htlc_fee_and_dependencies ( ) -> Self {
1009
1011
let mut ret = Self :: empty ( ) ;
1010
- <sealed:: ChannelTypeContext as sealed:: StaticRemoteKey >:: set_required_bit ( & mut ret. flags ) ;
1011
- <sealed:: ChannelTypeContext as sealed:: AnchorsZeroFeeHtlcTx >:: set_required_bit (
1012
- & mut ret. flags ,
1013
- ) ;
1012
+ <sealed:: ChannelTypeContext as sealed:: StaticRemoteKey >:: set_required_bit ( & mut ret) ;
1013
+ <sealed:: ChannelTypeContext as sealed:: AnchorsZeroFeeHtlcTx >:: set_required_bit ( & mut ret) ;
1014
1014
ret
1015
1015
}
1016
1016
}
0 commit comments