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

Add a note comparing against std types #58

Merged
merged 8 commits into from
Nov 14, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
//! * [`Mutex`] - a mutual exclusion lock.
//! * [`RwLock`] - a reader-writer lock, allowing any number of readers or a single writer.
//! * [`Semaphore`] - limits the number of concurrent operations.
//!
//! ## Relationship with `std::sync`
//!
//! In general, you should consider using [`std::sync`] types over types from this crate.
//!
//! There are two primary use cases for types from this crate:
//!
//! - You need to use a synchronization primitive in a `no_std` environment.
//! - You need to hold a lock across an `.await` point.
notgull marked this conversation as resolved.
Show resolved Hide resolved
//!
//! If you already use `libstd` and you aren't holding locks across await points, you should consider
//! [`std::sync`] instead of this crate. Those types are optimized for operating system use cases,
//! are less complex and are generally much faster. In contrast, `async-lock`'s notification system
//! uses `std::sync::Mutex` under the hood if the `std` feature is enabled, and will fall back to a
//! significantly slower strategy if it is not. So, there are few cases where `async-lock` is a
//! win for performance over [`std::sync`].
//!
//! When using [`std::sync`], you should be careful to not hold any locks over an `.await` point.
//! In modern Rust, there is a Clippy lint called [`await_holding_lock`] that emits warnings for this
//! scenario for [`std::sync`] and some other synchronization crates. Still, when deciding to use
//! [`std::sync`] over `async-lock`, you must be careful to not hold any locks past an `.await` point.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is quite repetitive, I think we can have something much shorter. For example, you could extend the sentence on line 19 to If you already use libstd and you aren't holding locks across await points (there is a [Clippy lint](link) to check for this), you should consider std::sync instead of this crate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Amended this

//!
//! In short, you should prefer using [`std::sync`] over any of the types in this module.
//!
//! [`std::sync`]: https://doc.rust-lang.org/std/sync/index.html
//! [`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/stable/index.html#/await_holding_lock

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
Expand Down
Loading