-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 into_inner
and get_mut
to Mutex
and RwLock
#29031
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
// The following code is like `let Mutex { inner, data } = self`. | ||
let mut inner = mem::uninitialized::<Box<StaticMutex>>(); | ||
let mut data = mem::uninitialized::<UnsafeCell<T>>(); | ||
ptr::copy_nonoverlapping(&self.inner as *const Box<StaticMutex>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use ptr::read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also note that &T coerces to *T
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. (re: coercion, just doing &self.inner
didn't type check for some reason---maybe some interaction with type inference.)
Other than the ptr::read stuff, this looks great! Really thorough. I'm not really qualified to review this kind of code, though. |
Thanks @gankro! I changed to |
Local (full) |
@aturon mentioned in a different bug he is going to be unavailable. r? @alexcrichton |
This shouldn't close #28968 if it is using it as a tracking issue. |
The libs team talked about this today, and the conclusion is that this is good to go. The changes we'd like to see, however, are:
let (a, b) = {
let Mutex { ref a, ref b } = self; // note exhaustive match
(ptr::read(a), ptr::read(b))
};
mem::forget(self);
|
@huonw Good point, I edited the description to not close the issue. @alexcrichton That all makes sense, I'll do all of that when I get back from work, thanks! |
@alexcrichton I implemented everything you asked for, please take a look. Thanks! |
/// | ||
/// If another user of this mutex panicked while holding the mutex, then | ||
/// this call will return an error instead. | ||
#[inline] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah as one final piece, could these #[inline]
annotations be left out? These are pretty hefty functions and the inline shouldn't be needed anyway as a result of the generic parameter on Mutex
@alexcrichton Done and squashed. Don't feel strongly, but I had added The fact that it has a type param doesn't quite imply |
into_inner
and get_mut
to sync::Mutex
into_inner
and get_mut
to Mutex
and RwLock
The implementation for `into_inner` was a bit more complex than I had hoped for---is there any simpler, less unsafe way of getting around the fact that one can't move out of a `Drop` struct? See #28968 and rust-lang/rfcs#1269 .
The implementation for
into_inner
was a bit more complex than I had hoped for---is there any simpler, less unsafe way of getting around the fact that one can't move out of aDrop
struct?See #28968 and rust-lang/rfcs#1269 .