forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#91828 - oxalica:feat/waker-getters, r=dtolnay
Implement `RawWaker` and `Waker` getters for underlying pointers implement rust-lang#87021 New APIs: - `RawWaker::data(&self) -> *const ()` - `RawWaker::vtable(&self) -> &'static RawWakerVTable` - ~`Waker::as_raw_waker(&self) -> &RawWaker`~ `Waker::as_raw(&self) -> &RawWaker` This third one is an auxiliary function to make the two APIs above more useful. Since we can only get `&Waker` in `Future::poll`, without this, we need to `transmute` it into `&RawWaker` (relying on `repr(transparent)`) in order to access its data/vtable pointers. ~Not sure if it should be named `as_raw` or `as_raw_waker`. Seems we always use `as_<something-raw>` instead of just `as_raw`. But `as_raw_waker` seems not quite consistent with `Waker::from_raw`.~ As suggested in rust-lang#91828 (comment), use `as_raw`.
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::ptr; | ||
use std::task::{RawWaker, RawWakerVTable, Waker}; | ||
|
||
#[test] | ||
fn test_waker_getters() { | ||
let raw_waker = RawWaker::new(42usize as *mut (), &WAKER_VTABLE); | ||
assert_eq!(raw_waker.data() as usize, 42); | ||
assert!(ptr::eq(raw_waker.vtable(), &WAKER_VTABLE)); | ||
|
||
let waker = unsafe { Waker::from_raw(raw_waker) }; | ||
let waker2 = waker.clone(); | ||
let raw_waker2 = waker2.as_raw(); | ||
assert_eq!(raw_waker2.data() as usize, 43); | ||
assert!(ptr::eq(raw_waker2.vtable(), &WAKER_VTABLE)); | ||
} | ||
|
||
static WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new( | ||
|data| RawWaker::new((data as usize + 1) as *mut (), &WAKER_VTABLE), | ||
|_| {}, | ||
|_| {}, | ||
|_| {}, | ||
); |