diff options
| author | Dániel Buga <[email protected]> | 2025-08-16 11:50:14 +0200 |
|---|---|---|
| committer | Dániel Buga <[email protected]> | 2025-08-18 12:50:52 +0200 |
| commit | 31967e1f901454014d0c452dd0328f98a80c8a9c (patch) | |
| tree | a6ab1f433b60ea4b52e125b663bfe836435d40f1 | |
| parent | 74037f04933f4ec9a678e0b47fd6819e7c0489a9 (diff) | |
Print more informative panic messages
| -rw-r--r-- | embassy-executor-timer-queue/src/lib.rs | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/embassy-executor-timer-queue/src/lib.rs b/embassy-executor-timer-queue/src/lib.rs index 456ccaec3..de94e3faf 100644 --- a/embassy-executor-timer-queue/src/lib.rs +++ b/embassy-executor-timer-queue/src/lib.rs | |||
| @@ -22,7 +22,7 @@ | |||
| 22 | 22 | ||
| 23 | use core::task::Waker; | 23 | use core::task::Waker; |
| 24 | 24 | ||
| 25 | const ITEM_SIZE: usize = if cfg!(feature = "timer-item-size-8-words") { | 25 | const ITEM_WORDS: usize = if cfg!(feature = "timer-item-size-8-words") { |
| 26 | 8 | 26 | 8 |
| 27 | } else if cfg!(feature = "timer-item-size-6-words") { | 27 | } else if cfg!(feature = "timer-item-size-6-words") { |
| 28 | 6 | 28 | 6 |
| @@ -39,13 +39,13 @@ const ITEM_SIZE: usize = if cfg!(feature = "timer-item-size-8-words") { | |||
| 39 | /// [`TimerQueueItem::as_mut`]. | 39 | /// [`TimerQueueItem::as_mut`]. |
| 40 | #[repr(align(8))] | 40 | #[repr(align(8))] |
| 41 | pub struct TimerQueueItem { | 41 | pub struct TimerQueueItem { |
| 42 | data: [usize; ITEM_SIZE], | 42 | data: [usize; ITEM_WORDS], |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | impl TimerQueueItem { | 45 | impl TimerQueueItem { |
| 46 | /// Creates a new, zero-initialized `TimerQueueItem`. | 46 | /// Creates a new, zero-initialized `TimerQueueItem`. |
| 47 | pub const fn new() -> Self { | 47 | pub const fn new() -> Self { |
| 48 | Self { data: [0; ITEM_SIZE] } | 48 | Self { data: [0; ITEM_WORDS] } |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | /// Retrieves the `TimerQueueItem` reference that belongs to the task of the waker. | 51 | /// Retrieves the `TimerQueueItem` reference that belongs to the task of the waker. |
| @@ -74,10 +74,7 @@ impl TimerQueueItem { | |||
| 74 | /// - The type must be valid when zero-initialized. | 74 | /// - The type must be valid when zero-initialized. |
| 75 | /// - The timer queue should only be interpreted as a single type `T` during its lifetime. | 75 | /// - The timer queue should only be interpreted as a single type `T` during its lifetime. |
| 76 | pub unsafe fn as_ref<T>(&self) -> &T { | 76 | pub unsafe fn as_ref<T>(&self) -> &T { |
| 77 | const { | 77 | const { validate::<T>() } |
| 78 | assert!(core::mem::size_of::<Self>() >= core::mem::size_of::<T>()); | ||
| 79 | assert!(core::mem::align_of::<Self>() >= core::mem::align_of::<T>()); | ||
| 80 | } | ||
| 81 | unsafe { &*(self.data.as_ptr() as *const T) } | 78 | unsafe { &*(self.data.as_ptr() as *const T) } |
| 82 | } | 79 | } |
| 83 | 80 | ||
| @@ -88,10 +85,20 @@ impl TimerQueueItem { | |||
| 88 | /// - The type must be valid when zero-initialized. | 85 | /// - The type must be valid when zero-initialized. |
| 89 | /// - The timer queue should only be interpreted as a single type `T` during its lifetime. | 86 | /// - The timer queue should only be interpreted as a single type `T` during its lifetime. |
| 90 | pub unsafe fn as_mut<T>(&self) -> &mut T { | 87 | pub unsafe fn as_mut<T>(&self) -> &mut T { |
| 91 | const { | 88 | const { validate::<T>() } |
| 92 | assert!(core::mem::size_of::<Self>() >= core::mem::size_of::<T>()); | ||
| 93 | assert!(core::mem::align_of::<Self>() >= core::mem::align_of::<T>()); | ||
| 94 | } | ||
| 95 | unsafe { &mut *(self.data.as_ptr() as *mut T) } | 89 | unsafe { &mut *(self.data.as_ptr() as *mut T) } |
| 96 | } | 90 | } |
| 97 | } | 91 | } |
| 92 | |||
| 93 | const fn validate<T>() { | ||
| 94 | const { | ||
| 95 | assert!( | ||
| 96 | core::mem::size_of::<TimerQueueItem>() >= core::mem::size_of::<T>(), | ||
| 97 | "embassy-executor-timer-queue item size is smaller than the requested type. Select a larger timer-item-size-N-words feature." | ||
| 98 | ); | ||
| 99 | assert!( | ||
| 100 | core::mem::align_of::<TimerQueueItem>() >= core::mem::align_of::<T>(), | ||
| 101 | "the alignment of the requested type is greater than 8" | ||
| 102 | ); | ||
| 103 | } | ||
| 104 | } | ||
