File tree Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Original file line number Diff line number Diff line change @@ -993,6 +993,13 @@ impl ConstantTimeLess for cmp::Ordering {
993
993
pub struct BlackBox < T : Copy > ( T ) ;
994
994
995
995
impl < T : Copy > BlackBox < T > {
996
+ /// Constructs a new instance of `BlackBox` which will wrap the specified value.
997
+ ///
998
+ /// All access to the inner value will be mediated by a `black_box` optimization barrier.
999
+ pub const fn new ( value : T ) -> Self {
1000
+ Self ( value)
1001
+ }
1002
+
996
1003
/// Read the inner value, applying an optimization barrier on access.
997
1004
pub fn get ( self ) -> T {
998
1005
black_box ( self . 0 )
Original file line number Diff line number Diff line change @@ -423,3 +423,10 @@ fn less_than_ordering() {
423
423
assert_eq ! ( cmp:: Ordering :: Greater . ct_lt( & cmp:: Ordering :: Less ) . unwrap_u8( ) , 0 ) ;
424
424
assert_eq ! ( cmp:: Ordering :: Less . ct_lt( & cmp:: Ordering :: Greater ) . unwrap_u8( ) , 1 ) ;
425
425
}
426
+
427
+ #[ test]
428
+ fn black_box_round_trip ( ) {
429
+ let n = 42u64 ;
430
+ let black_box = BlackBox :: new ( n) ;
431
+ assert_eq ! ( n, black_box. get( ) ) ;
432
+ }
You can’t perform that action at this time.
0 commit comments