Skip to content

Commit

Permalink
refactor: replace pattern matching with Option::{is_none, is_some}
Browse files Browse the repository at this point in the history
These methods were added by [1] and are still unstable.

[1]: <rust-lang/rust#73930>
  • Loading branch information
yvt committed Jul 28, 2020
1 parent 06fa07a commit 224cfd7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
29 changes: 10 additions & 19 deletions src/constance/src/kernel/cfg/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ impl<System: Port> CfgInterruptLineBuilder<System> {

/// [**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 {
Expand All @@ -54,8 +53,7 @@ impl<System: Port> CfgInterruptLineBuilder<System> {

/// 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 {
Expand Down Expand Up @@ -98,8 +96,7 @@ impl<System: Port> CfgInterruptLineBuilder<System> {
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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -201,8 +197,7 @@ impl<System: Port> CfgInterruptHandlerBuilder<System> {
/// [**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 {
Expand Down Expand Up @@ -302,14 +297,11 @@ pub(super) const fn panic_if_unmanaged_safety_is_violated<System: Port>(
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::<System>()),
None
);
let managed_line_i = vec_position!(interrupt_lines, |line| line.num == handler.line
&& line.is_initially_managed::<System>());
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 \
Expand Down Expand Up @@ -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");
}

Expand Down
6 changes: 2 additions & 4 deletions src/constance/src/kernel/cfg/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ impl<System: Port> CfgTaskBuilder<System> {

/// 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");
}

Expand All @@ -73,8 +72,7 @@ impl<System: Port> CfgTaskBuilder<System> {

/// Specify the task's hunk.
pub const fn stack_hunk(self, stack_hunk: task::StackHunk<System>) -> 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");
}

Expand Down
1 change: 1 addition & 0 deletions src/constance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}`
Expand Down
3 changes: 1 addition & 2 deletions src/constance_port_arm_m/src/threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 224cfd7

Please sign in to comment.