mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-24 15:52:34 +00:00
Merge pull request #3059 from zjp-CN/waker-getters
use nightly waker_getters APIs
This commit is contained in:
commit
e80ca5fc67
@ -1,4 +1,5 @@
|
||||
#![cfg_attr(not(any(feature = "arch-std", feature = "arch-wasm")), no_std)]
|
||||
#![cfg_attr(feature = "nightly", feature(waker_getters))]
|
||||
#![allow(clippy::new_without_default)]
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![warn(missing_docs)]
|
||||
|
@ -1,4 +1,3 @@
|
||||
use core::mem;
|
||||
use core::task::{RawWaker, RawWakerVTable, Waker};
|
||||
|
||||
use super::{wake_task, TaskHeader, TaskRef};
|
||||
@ -33,20 +32,35 @@ 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 {
|
||||
#[cfg(not(feature = "nightly"))]
|
||||
{
|
||||
struct WakerHack {
|
||||
data: *const (),
|
||||
vtable: &'static RawWakerVTable,
|
||||
}
|
||||
|
||||
// safety: OK because WakerHack has the same layout as Waker.
|
||||
// This is not really guaranteed because the structs are `repr(Rust)`, it is
|
||||
// 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 { mem::transmute(waker) };
|
||||
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) }
|
||||
}
|
||||
}
|
||||
|
||||
struct WakerHack {
|
||||
data: *const (),
|
||||
vtable: &'static RawWakerVTable,
|
||||
#[cfg(feature = "nightly")]
|
||||
{
|
||||
let raw_waker = waker.as_raw();
|
||||
|
||||
if raw_waker.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) }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user