-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add Wake trait for safe construction of Wakers. #68700
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
Changes from 1 commit
06ede35
d8a835f
c9acdb0
ede03a4
3ae74ca
a4875a7
caff9f9
32f5724
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,20 +59,20 @@ fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker { | |
// Increment the reference count of the arc to clone it. | ||
unsafe fn clone_waker<W: Wake + Send + Sync + 'static>(waker: *const ()) -> RawWaker { | ||
let waker: Arc<W> = Arc::from_raw(waker as *const W); | ||
mem::forget(waker.clone()); | ||
mem::forget(Arc::clone(&waker)); | ||
raw_waker(waker) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you use let waker: ManuallyDrop<Arc<W>> = ManuallyDrop::new(Arc::from_raw(waker as *const W));
raw_waker(waker.clone()) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I find this less clear than mem::forgetting the clone. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm. I find it really confusing to create a clone only to forget it immediately. Conceptually speaking, you want to return the clone (that's what the function name says), but without dropping the original. If you stick to the current code, I think this needs a comment explaining why it makes any sense to create a clone that is immediately forgotten. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me clone + mem forget an Arc is a pretty clear "increment the ref count" signal (TBH just having unsafe methods to fiddle with Arc's ref count would be nicer than any of this). What I would find non-obvious is why you need to put self into a manually drop. There is a comment on this function indicating that it's purpose is to increment the refcount. |
||
} | ||
|
||
// Wake by value, moving the Arc into the Wake::wake function | ||
unsafe fn wake<W: Wake + Send + Sync + 'static>(waker: *const ()) { | ||
let waker: Arc<W> = Arc::from_raw(waker as *const W); | ||
Wake::wake(waker); | ||
<W as Wake>::wake(waker); | ||
} | ||
|
||
// Wake by reference, wrap the waker in ManuallyDrop to avoid dropping it | ||
unsafe fn wake_by_ref<W: Wake + Send + Sync + 'static>(waker: *const ()) { | ||
let waker: ManuallyDrop<Arc<W>> = ManuallyDrop::new(Arc::from_raw(waker as *const W)); | ||
Wake::wake_by_ref(&waker); | ||
<W as Wake>::wake_by_ref(&waker); | ||
} | ||
|
||
// Decrement the reference count of the Arc on drop | ||
|
Uh oh!
There was an error while loading. Please reload this page.