Skip to content

Commit

Permalink
Make TryLock::new const fn (#2)
Browse files Browse the repository at this point in the history
Requires Rust 1.31.0 or newer.
  • Loading branch information
yvt authored Dec 7, 2023
1 parent 3ec20ad commit 1fe343d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- stable
- beta
- nightly
- 1.21.0
- 1.31.0

runs-on: ubuntu-latest

Expand Down
12 changes: 2 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct TryLock<T> {
impl<T> TryLock<T> {
/// Create a `TryLock` around the value.
#[inline]
pub fn new(val: T) -> TryLock<T> {
pub const fn new(val: T) -> TryLock<T> {
TryLock {
is_locked: AtomicBool::new(false),
value: UnsafeCell::new(val),
Expand Down Expand Up @@ -179,15 +179,7 @@ impl<T> TryLock<T> {
#[inline]
pub fn into_inner(self) -> T {
debug_assert!(!self.is_locked.load(Ordering::Relaxed), "TryLock was mem::forgotten");
// Since the compiler can statically determine this is the only owner,
// it's safe to take the value out. In fact, in newer versions of Rust,
// `UnsafeCell::into_inner` has been marked safe.
//
// To support older version (1.21), the unsafe block is still here.
#[allow(unused_unsafe)]
unsafe {
self.value.into_inner()
}
self.value.into_inner()
}
}

Expand Down

0 comments on commit 1fe343d

Please sign in to comment.