Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to stabilized const_fn_trait_bound #325

Merged
merged 1 commit into from Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
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
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 = []
7 changes: 7 additions & 0 deletions lock_api/build.rs
@@ -0,0 +1,7 @@
fn main() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a build script will make building a bit slower as the build script has to be compiled and the main crate has to wait on running the build script.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. An alternative approach without such drawbacks is to use the options feature. If the maintainer prefers that approach, I'm happy to switch to using it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally don't mind this: the build script has no dependencies and can be compiled in parallel with other crates, so it is usually not noticable.

let cfg = autocfg::new();

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
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
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
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
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
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
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
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
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