From 31967e1f901454014d0c452dd0328f98a80c8a9c Mon Sep 17 00:00:00 2001 From: Dániel Buga Date: Sat, 16 Aug 2025 11:50:14 +0200 Subject: Print more informative panic messages --- embassy-executor-timer-queue/src/lib.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'embassy-executor-timer-queue') 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 @@ use core::task::Waker; -const ITEM_SIZE: usize = if cfg!(feature = "timer-item-size-8-words") { +const ITEM_WORDS: usize = if cfg!(feature = "timer-item-size-8-words") { 8 } else if cfg!(feature = "timer-item-size-6-words") { 6 @@ -39,13 +39,13 @@ const ITEM_SIZE: usize = if cfg!(feature = "timer-item-size-8-words") { /// [`TimerQueueItem::as_mut`]. #[repr(align(8))] pub struct TimerQueueItem { - data: [usize; ITEM_SIZE], + data: [usize; ITEM_WORDS], } impl TimerQueueItem { /// Creates a new, zero-initialized `TimerQueueItem`. pub const fn new() -> Self { - Self { data: [0; ITEM_SIZE] } + Self { data: [0; ITEM_WORDS] } } /// Retrieves the `TimerQueueItem` reference that belongs to the task of the waker. @@ -74,10 +74,7 @@ impl TimerQueueItem { /// - The type must be valid when zero-initialized. /// - The timer queue should only be interpreted as a single type `T` during its lifetime. pub unsafe fn as_ref(&self) -> &T { - const { - assert!(core::mem::size_of::() >= core::mem::size_of::()); - assert!(core::mem::align_of::() >= core::mem::align_of::()); - } + const { validate::() } unsafe { &*(self.data.as_ptr() as *const T) } } @@ -88,10 +85,20 @@ impl TimerQueueItem { /// - The type must be valid when zero-initialized. /// - The timer queue should only be interpreted as a single type `T` during its lifetime. pub unsafe fn as_mut(&self) -> &mut T { - const { - assert!(core::mem::size_of::() >= core::mem::size_of::()); - assert!(core::mem::align_of::() >= core::mem::align_of::()); - } + const { validate::() } unsafe { &mut *(self.data.as_ptr() as *mut T) } } } + +const fn validate() { + const { + assert!( + core::mem::size_of::() >= core::mem::size_of::(), + "embassy-executor-timer-queue item size is smaller than the requested type. Select a larger timer-item-size-N-words feature." + ); + assert!( + core::mem::align_of::() >= core::mem::align_of::(), + "the alignment of the requested type is greater than 8" + ); + } +} -- cgit