Skip to content

Commit

Permalink
Update to stabilized const_fn_trait_bound
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Mar 10, 2022
1 parent 7bded13 commit 2c8dc2b
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 17 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ lock.

There are a few restrictions when using this library on stable Rust:

- You will have to use the `const_*` functions (e.g. `const_mutex(val)`) to
statically initialize the locking primitives. Using e.g. `Mutex::new(val)`
does not work on stable Rust yet.
- The `wasm32-unknown-unknown` target is only fully supported on nightly with
`-C target-feature=+atomics` in `RUSTFLAGS` and `-Z build-std` passed to cargo.
parking_lot will work mostly fine on stable, the only difference is it will
Expand Down
3 changes: 3 additions & 0 deletions lock_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ owning_ref = { version = "0.4.1", optional = true }
# support, just pass "--features serde" when building this crate.
serde = { version = "1.0.126", default-features = false, optional = true }

[build-dependencies]
autocfg = "1.1.0"

[features]
nightly = []
arc_lock = []
16 changes: 16 additions & 0 deletions lock_api/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn main() {
let cfg = match autocfg::AutoCfg::new() {
Ok(cfg) => cfg,
Err(e) => {
println!(
"cargo:warning=lock_api: unable to determine rustc version: {}",
e
);
return;
}
};

if cfg.probe_rustc_version(1, 61) {
println!("cargo:rustc-cfg=has_const_fn_trait_bound");
}
}
3 changes: 0 additions & 3 deletions lock_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,10 @@
//! - `owning_ref`: Allows your lock types to be used with the `owning_ref` crate.
//! - `arc_lock`: Enables locking from an `Arc`. This enables types such as `ArcMutexGuard`. Note that this
//! requires the `alloc` crate to be present.
//! - `nightly`: Enables nightly-only features. At the moment the only such
//! feature is `const fn` constructors for lock types.
#![no_std]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![cfg_attr(feature = "nightly", feature(const_fn_trait_bound))]

#[macro_use]
extern crate scopeguard;
Expand Down
4 changes: 2 additions & 2 deletions lock_api/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ unsafe impl<R: RawMutex + Sync, T: ?Sized + Send> Sync for Mutex<R, T> {}

impl<R: RawMutex, T> Mutex<R, T> {
/// Creates a new mutex in an unlocked state ready for use.
#[cfg(feature = "nightly")]
#[cfg(has_const_fn_trait_bound)]
#[inline]
pub const fn new(val: T) -> Mutex<R, T> {
Mutex {
Expand All @@ -159,7 +159,7 @@ impl<R: RawMutex, T> Mutex<R, T> {
}

/// Creates a new mutex in an unlocked state ready for use.
#[cfg(not(feature = "nightly"))]
#[cfg(not(has_const_fn_trait_bound))]
#[inline]
pub fn new(val: T) -> Mutex<R, T> {
Mutex {
Expand Down
4 changes: 2 additions & 2 deletions lock_api/src/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ unsafe impl<R: RawMutex + Sync, G: GetThreadId + Sync, T: ?Sized + Send> Sync

impl<R: RawMutex, G: GetThreadId, T> ReentrantMutex<R, G, T> {
/// Creates a new reentrant mutex in an unlocked state ready for use.
#[cfg(feature = "nightly")]
#[cfg(has_const_fn_trait_bound)]
#[inline]
pub const fn new(val: T) -> ReentrantMutex<R, G, T> {
ReentrantMutex {
Expand All @@ -245,7 +245,7 @@ impl<R: RawMutex, G: GetThreadId, T> ReentrantMutex<R, G, T> {
}

/// Creates a new reentrant mutex in an unlocked state ready for use.
#[cfg(not(feature = "nightly"))]
#[cfg(not(has_const_fn_trait_bound))]
#[inline]
pub fn new(val: T) -> ReentrantMutex<R, G, T> {
ReentrantMutex {
Expand Down
6 changes: 3 additions & 3 deletions lock_api/src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ unsafe impl<R: RawRwLock + Sync, T: ?Sized + Send + Sync> Sync for RwLock<R, T>

impl<R: RawRwLock, T> RwLock<R, T> {
/// Creates a new instance of an `RwLock<T>` which is unlocked.
#[cfg(feature = "nightly")]
#[cfg(has_const_fn_trait_bound)]
#[inline]
pub const fn new(val: T) -> RwLock<R, T> {
RwLock {
Expand All @@ -376,7 +376,7 @@ impl<R: RawRwLock, T> RwLock<R, T> {
}

/// Creates a new instance of an `RwLock<T>` which is unlocked.
#[cfg(not(feature = "nightly"))]
#[cfg(not(has_const_fn_trait_bound))]
#[inline]
pub fn new(val: T) -> RwLock<R, T> {
RwLock {
Expand Down Expand Up @@ -892,7 +892,7 @@ impl<R: RawRwLockRecursive, T: ?Sized> RwLock<R, T> {
/// Attempts to lock this `RwLock` with shared read access, through an `Arc`.
///
/// This method is similar to the `try_read_recursive` method; however, it requires the `RwLock` to be inside
/// of an `Arc` and the resulting read guard has no lifetime requirements.
/// of an `Arc` and the resulting read guard has no lifetime requirements.
#[cfg(feature = "arc_lock")]
#[inline]
pub fn try_read_recursive_arc(self: &Arc<Self>) -> Option<ArcRwLockReadGuard<R, T>> {
Expand Down
2 changes: 1 addition & 1 deletion src/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl WaitTimeoutResult {
/// woken up.
/// - Only requires 1 word of space, whereas the standard library boxes the
/// `Condvar` due to platform limitations.
/// - Can be statically constructed (requires the `const_fn` nightly feature).
/// - Can be statically constructed.
/// - Does not require any drop glue when dropped.
/// - Inline fast path for the uncontended case.
///
Expand Down
2 changes: 1 addition & 1 deletion src/fair_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use lock_api;
/// - No poisoning, the lock is released normally on panic.
/// - Only requires 1 byte of space, whereas the standard library boxes the
/// `FairMutex` due to platform limitations.
/// - Can be statically constructed (requires the `const_fn` nightly feature).
/// - Can be statically constructed.
/// - Does not require any drop glue when dropped.
/// - Inline fast path for the uncontended case.
/// - Efficient handling of micro-contention using adaptive spinning.
Expand Down
2 changes: 1 addition & 1 deletion src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use lock_api;
/// - No poisoning, the lock is released normally on panic.
/// - Only requires 1 byte of space, whereas the standard library boxes the
/// `Mutex` due to platform limitations.
/// - Can be statically constructed (requires the `const_fn` nightly feature).
/// - Can be statically constructed.
/// - Does not require any drop glue when dropped.
/// - Inline fast path for the uncontended case.
/// - Efficient handling of micro-contention using adaptive spinning.
Expand Down
2 changes: 1 addition & 1 deletion src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use lock_api;
/// - No poisoning, the lock is released normally on panic.
/// - Only requires 1 word of space, whereas the standard library boxes the
/// `RwLock` due to platform limitations.
/// - Can be statically constructed (requires the `const_fn` nightly feature).
/// - Can be statically constructed.
/// - Does not require any drop glue when dropped.
/// - Inline fast path for the uncontended case.
/// - Efficient handling of micro-contention using adaptive spinning.
Expand Down

0 comments on commit 2c8dc2b

Please sign in to comment.