-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement From<MutexGuard<'a, T>> for &'a Mutex<T> #134048
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
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @thomcc (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
This makes sense to me, let's see what T-libs-api has to say about it. |
The job Click to see the possible cause of the failure (guessed by this bot)
|
This seems useful. It might make more sense to have an associated function on |
To elaborate on that: MutexGuard represents a locked mutex but the function returns an unlocked mutex. This IMO violates From's value preservation principle:
Perhaps it should be called |
Given the discussed alternatives here, it is probably best for you to file an ACP. @rubcc95 could you do this? It is an issue template at https://github.com/rust-lang/libs-team/issues. |
@tgross35 Of course. I'm on mobile, so I’ll do it tomorrow. As a side note, I take this opportunity to learn from your knowledge. I’m a self-taught programmer, and sometimes these community aspects seem a bit cumbersome to me, as I’m just starting with them... Would the correct approach in this situation have been to open a pull request directly instead of posting here?" |
You'll learn about the processes as you go :) There are some details here https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html. The gist is that all new API needs to get approved by the libs-api team. Opening a PR and then requesting libs-api review is one way to do this and is usually okay for minimal changes. The other way is with an ACP, which is a better platform to discuss new API because (1) people are looking for API proposals there and can chime in, and (2) it avoids getting into the details of how something is implemented. Also an ACP is a good idea for bigger changes so you don't spend time implementing something that will need to change or may wind up rejected. So there was nothing wrong with opening this PR for a simple change. But since a couple alternatives have come up, might as well move discussion to a dedicated place for feedback. |
☔ The latest upstream changes (presumably #134692) made this pull request unmergeable. Please resolve the merge conflicts. |
@rubcc95 any updates on the ACP? if you have opened one already kindly link it in this issue (preferably in the issue description). Thanks |
Current Problem
Consider the following common pattern:
Current patterns for working with mutex locks require passing both the mutex and its guard between methods, which leads to verbose code. This proposal aims to simplify and optimize mutex handling by allowing direct conversion from a
MutexGuard
to a reference to its originalMutex
. The MutexGuard already contains an immutable reference to the Mutex. Moreover, since the MutexGuard is consumed when delivering the immutable reference to the Mutex, this feature does not incentivize the occurrence of deadlocks.Proposed Solution
Implement
From
trait to allow direct conversion fromMutexGuard
to&Mutex
:After implementation, the code would simplify to:
Benefits