Skip to content

Commit bae0da8

Browse files
committed
Implement data and vtable getters for RawWaker
1 parent 58457bb commit bae0da8

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

library/core/src/task/wake.rs

+24
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ impl RawWaker {
4343
pub const fn new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker {
4444
RawWaker { data, vtable }
4545
}
46+
47+
/// Get the `data` pointer used to create this `RawWaker`.
48+
#[inline]
49+
#[must_use]
50+
#[unstable(feature = "waker_getters", issue = "87021")]
51+
pub fn data(&self) -> *const () {
52+
self.data
53+
}
54+
55+
/// Get the `vtable` pointer used to create this `RawWaker`.
56+
#[inline]
57+
#[must_use]
58+
#[unstable(feature = "waker_getters", issue = "87021")]
59+
pub fn vtable(&self) -> &'static RawWakerVTable {
60+
self.vtable
61+
}
4662
}
4763

4864
/// A virtual function pointer table (vtable) that specifies the behavior
@@ -260,6 +276,14 @@ impl Waker {
260276
pub unsafe fn from_raw(waker: RawWaker) -> Waker {
261277
Waker { waker }
262278
}
279+
280+
/// Get a reference to the underlying [`RawWaker`].
281+
#[inline]
282+
#[must_use]
283+
#[unstable(feature = "waker_getters", issue = "87021")]
284+
pub fn as_raw(&self) -> &RawWaker {
285+
&self.waker
286+
}
263287
}
264288

265289
#[stable(feature = "futures_api", since = "1.36.0")]

library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#![feature(unzip_option)]
8282
#![feature(const_array_from_ref)]
8383
#![feature(const_slice_from_ref)]
84+
#![feature(waker_getters)]
8485
#![deny(unsafe_op_in_unsafe_fn)]
8586

8687
extern crate test;
@@ -121,3 +122,4 @@ mod task;
121122
mod time;
122123
mod tuple;
123124
mod unicode;
125+
mod waker;

library/core/tests/waker.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::ptr;
2+
use std::task::{RawWaker, RawWakerVTable, Waker};
3+
4+
#[test]
5+
fn test_waker_getters() {
6+
let raw_waker = RawWaker::new(42usize as *mut (), &WAKER_VTABLE);
7+
assert_eq!(raw_waker.data() as usize, 42);
8+
assert!(ptr::eq(raw_waker.vtable(), &WAKER_VTABLE));
9+
10+
let waker = unsafe { Waker::from_raw(raw_waker) };
11+
let waker2 = waker.clone();
12+
let raw_waker2 = waker2.as_raw();
13+
assert_eq!(raw_waker2.data() as usize, 43);
14+
assert!(ptr::eq(raw_waker2.vtable(), &WAKER_VTABLE));
15+
}
16+
17+
static WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
18+
|data| RawWaker::new((data as usize + 1) as *mut (), &WAKER_VTABLE),
19+
|_| {},
20+
|_| {},
21+
|_| {},
22+
);

0 commit comments

Comments
 (0)