mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-21 22:32:29 +00:00
Merge pull request #3069 from zjp-CN/waker-getters
minimize cfg code in task_from_waker
This commit is contained in:
commit
8d79679bb2
@ -32,6 +32,7 @@ pub(crate) unsafe fn from_task(p: TaskRef) -> Waker {
|
||||
///
|
||||
/// Panics if the waker is not created by the Embassy executor.
|
||||
pub fn task_from_waker(waker: &Waker) -> TaskRef {
|
||||
let (vtable, data) = {
|
||||
#[cfg(not(feature = "nightly"))]
|
||||
{
|
||||
struct WakerHack {
|
||||
@ -44,23 +45,19 @@ pub fn task_from_waker(waker: &Waker) -> TaskRef {
|
||||
// indeed the case in the current implementation.
|
||||
// TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992
|
||||
let hack: &WakerHack = unsafe { core::mem::transmute(waker) };
|
||||
if hack.vtable != &VTABLE {
|
||||
panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.")
|
||||
}
|
||||
|
||||
// safety: our wakers are always created with `TaskRef::as_ptr`
|
||||
unsafe { TaskRef::from_ptr(hack.data as *const TaskHeader) }
|
||||
(hack.vtable, hack.data)
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
{
|
||||
let raw_waker = waker.as_raw();
|
||||
(raw_waker.vtable(), raw_waker.data())
|
||||
}
|
||||
};
|
||||
|
||||
if raw_waker.vtable() != &VTABLE {
|
||||
if vtable != &VTABLE {
|
||||
panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.")
|
||||
}
|
||||
|
||||
// safety: our wakers are always created with `TaskRef::as_ptr`
|
||||
unsafe { TaskRef::from_ptr(raw_waker.data() as *const TaskHeader) }
|
||||
}
|
||||
unsafe { TaskRef::from_ptr(data as *const TaskHeader) }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user