From 224cfd7d8e57ef11a41f92358ac672c3d0d62e3d Mon Sep 17 00:00:00 2001 From: yvt Date: Tue, 28 Jul 2020 18:56:30 +0900 Subject: [PATCH] refactor: replace pattern matching with `Option::{is_none, is_some}` These methods were added by [1] and are still unstable. [1]: --- src/constance/src/kernel/cfg/interrupt.rs | 29 ++++++++--------------- src/constance/src/kernel/cfg/task.rs | 6 ++--- src/constance/src/lib.rs | 1 + src/constance_port_arm_m/src/threading.rs | 3 +-- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/constance/src/kernel/cfg/interrupt.rs b/src/constance/src/kernel/cfg/interrupt.rs index f3679c3551..872d2ad8d6 100644 --- a/src/constance/src/kernel/cfg/interrupt.rs +++ b/src/constance/src/kernel/cfg/interrupt.rs @@ -42,8 +42,7 @@ impl CfgInterruptLineBuilder { /// [**Required**] Specify the interrupt line to confiigure. pub const fn line(self, line: interrupt::InterruptNum) -> Self { - // FIXME: `Option::is_some` is not `const fn` yet - if let Some(_) = self.line { + if self.line.is_some() { panic!("`line` is specified twice"); } Self { @@ -54,8 +53,7 @@ impl CfgInterruptLineBuilder { /// Specify the initial priority. pub const fn priority(self, priority: interrupt::InterruptPriority) -> Self { - // FIXME: `Option::is_some` is not `const fn` yet - if let Some(_) = self.priority { + if self.priority.is_some() { panic!("`priority` is specified twice"); } Self { @@ -98,8 +96,7 @@ impl CfgInterruptLineBuilder { let cfg_interrupt_line = inner.interrupt_lines.get_mut(i); if let Some(priority) = self.priority { - // FIXME: `Option::is_some` is not `const fn` yet - if let Some(_) = cfg_interrupt_line.priority { + if cfg_interrupt_line.priority.is_some() { panic!("`priority` is already specified for this interrupt line"); } cfg_interrupt_line.priority = Some(priority); @@ -140,8 +137,7 @@ impl CfgBuilderInterruptLine { priority: if let Some(i) = self.priority { i } else { 0 }, flags: { let mut f = 0; - // FIXME: `Option::is_some` is not `const fn` yet - if let Some(_) = self.priority { + if self.priority.is_some() { f |= interrupt::InterruptLineInitFlags::SET_PRIORITY.bits(); } if self.enabled { @@ -201,8 +197,7 @@ impl CfgInterruptHandlerBuilder { /// [**Required**] Specify the interrupt line to attach the interrupt /// handler to. pub const fn line(self, line: interrupt::InterruptNum) -> Self { - // FIXME: `Option::is_some` is not `const fn` yet - if let Some(_) = self.line { + if self.line.is_some() { panic!("`line` is specified twice"); } Self { @@ -302,14 +297,11 @@ pub(super) const fn panic_if_unmanaged_safety_is_violated( continue; } - // FIXME: Work-around for `Option::is_none` not being `const fn` - let line_unmanaged = matches!( - vec_position!(interrupt_lines, |line| line.num == handler.line - && line.is_initially_managed::()), - None - ); + let managed_line_i = vec_position!(interrupt_lines, |line| line.num == handler.line + && line.is_initially_managed::()); + let is_line_unmanaged = managed_line_i.is_none(); - if line_unmanaged { + if is_line_unmanaged { panic!( "An interrupt handler that is not marked with `unmanaged` \ is attached to an interrupt line whose priority value is \ @@ -578,10 +570,9 @@ pub const unsafe fn new_interrupt_handler_table< // Return the combined handler let handler = T::COMBINED_HANDLERS[i]; - // FIXME: Work-around for `Option::is_none` not being `const fn` // FIXME: Work-around for `Option::unwrap` not being `const fn` // FIXME: Work-around for `assert!` not being allowed in `const fn` - if let None = handler { + if handler.is_none() { panic!("assertion failed: T::COMBINED_HANDLERS[i] should be Some but got None"); } diff --git a/src/constance/src/kernel/cfg/task.rs b/src/constance/src/kernel/cfg/task.rs index 0385b72d32..1fc77462e3 100644 --- a/src/constance/src/kernel/cfg/task.rs +++ b/src/constance/src/kernel/cfg/task.rs @@ -60,8 +60,7 @@ impl CfgTaskBuilder { /// Specify the task's stack size. pub const fn stack_size(self, stack_size: usize) -> Self { - // FIXME: `Option::is_some` is not `const fn` yet - if let Some(_) = self.stack { + if self.stack.is_some() { panic!("the task's stack is already specified"); } @@ -73,8 +72,7 @@ impl CfgTaskBuilder { /// Specify the task's hunk. pub const fn stack_hunk(self, stack_hunk: task::StackHunk) -> Self { - // FIXME: `Option::is_some` is not `const fn` yet - if let Some(_) = self.stack { + if self.stack.is_some() { panic!("the task's stack is already specified"); } diff --git a/src/constance/src/lib.rs b/src/constance/src/lib.rs index 23f56828ef..43c95e2db0 100644 --- a/src/constance/src/lib.rs +++ b/src/constance/src/lib.rs @@ -8,6 +8,7 @@ #![feature(const_slice_from_raw_parts)] #![feature(const_raw_ptr_deref)] #![feature(const_checked_int_methods)] +#![feature(const_option)] #![feature(ptr_wrapping_offset_from)] #![feature(cfg_target_has_atomic)] // `#[cfg(target_has_atomic_load_store)]` #![feature(unsafe_block_in_unsafe_fn)] // `unsafe fn` doesn't imply `unsafe {}` diff --git a/src/constance_port_arm_m/src/threading.rs b/src/constance_port_arm_m/src/threading.rs index 0139d5c2d1..22b0f1dde3 100644 --- a/src/constance_port_arm_m/src/threading.rs +++ b/src/constance_port_arm_m/src/threading.rs @@ -566,10 +566,9 @@ where // FIXME: Work-around for `for` being unsupported in `const fn` while i < 16 { if i != INTERRUPT_SYSTICK { - // FIXME: `Option::is_some` is not `const fn` yet // TODO: This check trips even if no handler is registered at `i` #[cfg(any())] - if let Some(_) = System::INTERRUPT_HANDLERS.get(i) { + if System::INTERRUPT_HANDLERS.get(i).is_some() { panic!( "registering a handler for a non-internal exception is \ disallowed except for SysTick"