diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-06-13 00:17:26 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-06-13 00:17:26 +0000 |
| commit | 8d79679bb27ce6eca8904e167a8762ad5fc90604 (patch) | |
| tree | e64ba9fcfc6028b68659960071d5685c95958b6f | |
| parent | e80ca5fc67406cd0f001a50d895e58d0a229f098 (diff) | |
| parent | dd6a873447c35224b405d6bf94512b7be4dbb3b4 (diff) | |
Merge pull request #3069 from zjp-CN/waker-getters
minimize cfg code in task_from_waker
| -rw-r--r-- | embassy-executor/src/raw/waker.rs | 49 |
1 files changed, 23 insertions, 26 deletions
diff --git a/embassy-executor/src/raw/waker.rs b/embassy-executor/src/raw/waker.rs index 75641c257..8d3910a25 100644 --- a/embassy-executor/src/raw/waker.rs +++ b/embassy-executor/src/raw/waker.rs | |||
| @@ -32,35 +32,32 @@ pub(crate) unsafe fn from_task(p: TaskRef) -> Waker { | |||
| 32 | /// | 32 | /// |
| 33 | /// Panics if the waker is not created by the Embassy executor. | 33 | /// Panics if the waker is not created by the Embassy executor. |
| 34 | pub fn task_from_waker(waker: &Waker) -> TaskRef { | 34 | pub fn task_from_waker(waker: &Waker) -> TaskRef { |
| 35 | #[cfg(not(feature = "nightly"))] | 35 | let (vtable, data) = { |
| 36 | { | 36 | #[cfg(not(feature = "nightly"))] |
| 37 | struct WakerHack { | 37 | { |
| 38 | data: *const (), | 38 | struct WakerHack { |
| 39 | vtable: &'static RawWakerVTable, | 39 | data: *const (), |
| 40 | vtable: &'static RawWakerVTable, | ||
| 41 | } | ||
| 42 | |||
| 43 | // safety: OK because WakerHack has the same layout as Waker. | ||
| 44 | // This is not really guaranteed because the structs are `repr(Rust)`, it is | ||
| 45 | // indeed the case in the current implementation. | ||
| 46 | // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992 | ||
| 47 | let hack: &WakerHack = unsafe { core::mem::transmute(waker) }; | ||
| 48 | (hack.vtable, hack.data) | ||
| 40 | } | 49 | } |
| 41 | 50 | ||
| 42 | // safety: OK because WakerHack has the same layout as Waker. | 51 | #[cfg(feature = "nightly")] |
| 43 | // This is not really guaranteed because the structs are `repr(Rust)`, it is | 52 | { |
| 44 | // indeed the case in the current implementation. | 53 | let raw_waker = waker.as_raw(); |
| 45 | // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992 | 54 | (raw_waker.vtable(), raw_waker.data()) |
| 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 | } | 55 | } |
| 56 | }; | ||
| 50 | 57 | ||
| 51 | // safety: our wakers are always created with `TaskRef::as_ptr` | 58 | if vtable != &VTABLE { |
| 52 | unsafe { TaskRef::from_ptr(hack.data as *const TaskHeader) } | 59 | panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.") |
| 53 | } | ||
| 54 | |||
| 55 | #[cfg(feature = "nightly")] | ||
| 56 | { | ||
| 57 | let raw_waker = waker.as_raw(); | ||
| 58 | |||
| 59 | if raw_waker.vtable() != &VTABLE { | ||
| 60 | panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.") | ||
| 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 | } | 60 | } |
| 61 | // safety: our wakers are always created with `TaskRef::as_ptr` | ||
| 62 | unsafe { TaskRef::from_ptr(data as *const TaskHeader) } | ||
| 66 | } | 63 | } |
