Skip to content

Add value accessor methods to Mutex and RwLock #133406

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

Merged
merged 1 commit into from
Dec 15, 2024

Conversation

EFanZh
Copy link
Contributor

@EFanZh EFanZh commented Nov 24, 2024

This PR adds get, set and replace methods to the Mutex and RwLock types for quick access to their contained values.

One possible optimization would be to check for poisoning first and return an error immediately, without attempting to acquire the lock. I didn’t implement this because I consider poisoning to be relatively rare, adding this extra check could slow down common use cases.

@rustbot
Copy link
Collaborator

rustbot commented Nov 24, 2024

r? @Noratrieb

rustbot has assigned @Noratrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 24, 2024
@EFanZh EFanZh force-pushed the lock-value-accessors branch from 5d2f4af to 2c98df2 Compare November 24, 2024 05:08
@EFanZh EFanZh marked this pull request as ready for review November 24, 2024 06:17
@Noratrieb Noratrieb added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 24, 2024
@EFanZh EFanZh force-pushed the lock-value-accessors branch from 2c98df2 to b6446a1 Compare November 24, 2024 11:59
@EFanZh
Copy link
Contributor Author

EFanZh commented Nov 24, 2024

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 24, 2024
/// assert_eq!(mutex.get_cloned().unwrap(), 11);
/// ```
#[unstable(feature = "lock_value_accessors", issue = "133407")]
pub fn set(&self, value: T) -> Result<(), PoisonError<T>> {
Copy link
Member

Choose a reason for hiding this comment

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

Just to avoid the discussion getting lost - as I mentioned in rust-lang/libs-team#497, the type PoisonError<T> assumes the T is the result if you simply disregard poisoning. In this PR these are used to store the original input instead.

I suggest we either reword the documentation of PoisonError

Consumes this error indicating that a lock is poisoned, returning the underlying guard to allow access regardless.

or create a different error type that is not associated with LockResult.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the documentation.

@EFanZh EFanZh force-pushed the lock-value-accessors branch 2 times, most recently from f0d47da to 85fd82b Compare November 30, 2024 11:20
@EFanZh EFanZh force-pushed the lock-value-accessors branch from 85fd82b to 242c6c3 Compare November 30, 2024 13:06
Copy link
Member

@Noratrieb Noratrieb left a comment

Choose a reason for hiding this comment

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

The docs changes to the lock result are fine, moving the writing about the guard being contained to the methods explicitly probably makes it more clear even without the new use of the result.

@Noratrieb
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 14, 2024

📌 Commit 242c6c3 has been approved by Noratrieb

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 14, 2024
@bors bors merged commit 6667908 into rust-lang:master Dec 15, 2024
6 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Dec 15, 2024
@EFanZh EFanZh deleted the lock-value-accessors branch December 15, 2024 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants