Skip to content

Commit

Permalink
Merge remote-tracking branch 'tarcieri/black-box' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
isislovecruft committed Jun 19, 2024
2 parents 0375871 + c0774e9 commit f527719
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,13 @@ impl ConstantTimeLess for cmp::Ordering {
pub struct BlackBox<T: Copy>(T);

impl<T: Copy> BlackBox<T> {
/// Constructs a new instance of `BlackBox` which will wrap the specified value.
///
/// All access to the inner value will be mediated by a `black_box` optimization barrier.
pub const fn new(value: T) -> Self {
Self(value)
}

/// Read the inner value, applying an optimization barrier on access.
pub fn get(self) -> T {
black_box(self.0)
Expand Down
7 changes: 7 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,10 @@ fn less_than_ordering() {
assert_eq!(cmp::Ordering::Greater.ct_lt(&cmp::Ordering::Less).unwrap_u8(), 0);
assert_eq!(cmp::Ordering::Less.ct_lt(&cmp::Ordering::Greater).unwrap_u8(), 1);
}

#[test]
fn black_box_round_trip() {
let n = 42u64;
let black_box = BlackBox::new(n);
assert_eq!(n, black_box.get());
}

0 comments on commit f527719

Please sign in to comment.