put cfg code inside task_from_waker function
This commit is contained in:
parent
6b9470be2c
commit
b780df5f76
1 changed files with 26 additions and 36 deletions
|
@ -31,46 +31,36 @@ pub(crate) unsafe fn from_task(p: TaskRef) -> Waker {
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if the waker is not created by the Embassy executor.
|
/// Panics if the waker is not created by the Embassy executor.
|
||||||
#[cfg(not(feature = "nightly"))]
|
|
||||||
pub fn task_from_waker(waker: &Waker) -> TaskRef {
|
pub fn task_from_waker(waker: &Waker) -> TaskRef {
|
||||||
// safety: OK because WakerHack has the same layout as Waker.
|
#[cfg(not(feature = "nightly"))]
|
||||||
// This is not really guaranteed because the structs are `repr(Rust)`, it is
|
{
|
||||||
// indeed the case in the current implementation.
|
struct WakerHack {
|
||||||
// TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992
|
data: *const (),
|
||||||
let hack: &WakerHack = unsafe { core::mem::transmute(waker) };
|
vtable: &'static RawWakerVTable,
|
||||||
if hack.vtable != &VTABLE {
|
}
|
||||||
panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.")
|
|
||||||
|
// 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 { 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) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// safety: our wakers are always created with `TaskRef::as_ptr`
|
#[cfg(feature = "nightly")]
|
||||||
unsafe { TaskRef::from_ptr(hack.data as *const TaskHeader) }
|
{
|
||||||
}
|
let raw_waker = waker.as_raw();
|
||||||
|
|
||||||
#[cfg(not(feature = "nightly"))]
|
if raw_waker.vtable() != &VTABLE {
|
||||||
struct WakerHack {
|
panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.")
|
||||||
data: *const (),
|
}
|
||||||
vtable: &'static RawWakerVTable,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a task pointer from a waker.
|
// safety: our wakers are always created with `TaskRef::as_ptr`
|
||||||
///
|
unsafe { TaskRef::from_ptr(raw_waker.data() as *const TaskHeader) }
|
||||||
/// This can be used as an optimization in wait queues to store task pointers
|
|
||||||
/// (1 word) instead of full Wakers (2 words). This saves a bit of RAM and helps
|
|
||||||
/// avoid dynamic dispatch.
|
|
||||||
///
|
|
||||||
/// You can use the returned task pointer to wake the task with [`wake_task`](super::wake_task).
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Panics if the waker is not created by the Embassy executor.
|
|
||||||
#[cfg(feature = "nightly")]
|
|
||||||
pub fn task_from_waker(waker: &Waker) -> TaskRef {
|
|
||||||
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…
Add table
Add a link
Reference in a new issue