diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-06-12 23:40:53 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-06-12 23:40:53 +0000 |
| commit | e80ca5fc67406cd0f001a50d895e58d0a229f098 (patch) | |
| tree | 04e0e2318adfc07b9a1d312e9ed7371ea1b5c9d9 | |
| parent | 3d8c028864a8fcefa832bfaac624ebb74f3905f7 (diff) | |
| parent | b780df5f76adaf68d041d13820c455d4bf7eed96 (diff) | |
Merge pull request #3059 from zjp-CN/waker-getters
use nightly waker_getters APIs
| -rw-r--r-- | embassy-executor/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-executor/src/raw/waker.rs | 42 |
2 files changed, 29 insertions, 14 deletions
diff --git a/embassy-executor/src/lib.rs b/embassy-executor/src/lib.rs index 6a2e493a2..553ed76d3 100644 --- a/embassy-executor/src/lib.rs +++ b/embassy-executor/src/lib.rs | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | #![cfg_attr(not(any(feature = "arch-std", feature = "arch-wasm")), no_std)] | 1 | #![cfg_attr(not(any(feature = "arch-std", feature = "arch-wasm")), no_std)] |
| 2 | #![cfg_attr(feature = "nightly", feature(waker_getters))] | ||
| 2 | #![allow(clippy::new_without_default)] | 3 | #![allow(clippy::new_without_default)] |
| 3 | #![doc = include_str!("../README.md")] | 4 | #![doc = include_str!("../README.md")] |
| 4 | #![warn(missing_docs)] | 5 | #![warn(missing_docs)] |
diff --git a/embassy-executor/src/raw/waker.rs b/embassy-executor/src/raw/waker.rs index 522853e34..75641c257 100644 --- a/embassy-executor/src/raw/waker.rs +++ b/embassy-executor/src/raw/waker.rs | |||
| @@ -1,4 +1,3 @@ | |||
| 1 | use core::mem; | ||
| 2 | use core::task::{RawWaker, RawWakerVTable, Waker}; | 1 | use core::task::{RawWaker, RawWakerVTable, Waker}; |
| 3 | 2 | ||
| 4 | use super::{wake_task, TaskHeader, TaskRef}; | 3 | use super::{wake_task, TaskHeader, TaskRef}; |
| @@ -33,20 +32,35 @@ pub(crate) unsafe fn from_task(p: TaskRef) -> Waker { | |||
| 33 | /// | 32 | /// |
| 34 | /// Panics if the waker is not created by the Embassy executor. | 33 | /// Panics if the waker is not created by the Embassy executor. |
| 35 | pub fn task_from_waker(waker: &Waker) -> TaskRef { | 34 | pub fn task_from_waker(waker: &Waker) -> TaskRef { |
| 36 | // safety: OK because WakerHack has the same layout as Waker. | 35 | #[cfg(not(feature = "nightly"))] |
| 37 | // This is not really guaranteed because the structs are `repr(Rust)`, it is | 36 | { |
| 38 | // indeed the case in the current implementation. | 37 | struct WakerHack { |
| 39 | // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992 | 38 | data: *const (), |
| 40 | let hack: &WakerHack = unsafe { mem::transmute(waker) }; | 39 | vtable: &'static RawWakerVTable, |
| 41 | if hack.vtable != &VTABLE { | 40 | } |
| 42 | panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.") | 41 | |
| 42 | // safety: OK because WakerHack has the same layout as Waker. | ||
| 43 | // This is not really guaranteed because the structs are `repr(Rust)`, it is | ||
| 44 | // indeed the case in the current implementation. | ||
| 45 | // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992 | ||
| 46 | let hack: &WakerHack = unsafe { core::mem::transmute(waker) }; | ||
| 47 | if hack.vtable != &VTABLE { | ||
| 48 | panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.") | ||
| 49 | } | ||
| 50 | |||
| 51 | // safety: our wakers are always created with `TaskRef::as_ptr` | ||
| 52 | unsafe { TaskRef::from_ptr(hack.data as *const TaskHeader) } | ||
| 43 | } | 53 | } |
| 44 | 54 | ||
| 45 | // safety: our wakers are always created with `TaskRef::as_ptr` | 55 | #[cfg(feature = "nightly")] |
| 46 | unsafe { TaskRef::from_ptr(hack.data as *const TaskHeader) } | 56 | { |
| 47 | } | 57 | let raw_waker = waker.as_raw(); |
| 48 | 58 | ||
| 49 | struct WakerHack { | 59 | if raw_waker.vtable() != &VTABLE { |
| 50 | data: *const (), | 60 | panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.") |
| 51 | vtable: &'static RawWakerVTable, | 61 | } |
| 62 | |||
| 63 | // safety: our wakers are always created with `TaskRef::as_ptr` | ||
| 64 | unsafe { TaskRef::from_ptr(raw_waker.data() as *const TaskHeader) } | ||
| 65 | } | ||
| 52 | } | 66 | } |
