Skip to content

Commit f527719

Browse files
committed
Merge remote-tracking branch 'tarcieri/black-box' into develop
2 parents 0375871 + c0774e9 commit f527719

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,13 @@ impl ConstantTimeLess for cmp::Ordering {
993993
pub struct BlackBox<T: Copy>(T);
994994

995995
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+
9961003
/// Read the inner value, applying an optimization barrier on access.
9971004
pub fn get(self) -> T {
9981005
black_box(self.0)

tests/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,10 @@ fn less_than_ordering() {
423423
assert_eq!(cmp::Ordering::Greater.ct_lt(&cmp::Ordering::Less).unwrap_u8(), 0);
424424
assert_eq!(cmp::Ordering::Less.ct_lt(&cmp::Ordering::Greater).unwrap_u8(), 1);
425425
}
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+
}

0 commit comments

Comments
 (0)