Skip to content

Commit c43d3bf

Browse files
committed
refactor(r3): Option::expect is now unstably const fn
<rust-lang/rust#90269> (merged on Dec 3, 2021)
1 parent 7d0e027 commit c43d3bf

File tree

7 files changed

+19
-74
lines changed

7 files changed

+19
-74
lines changed

src/r3/src/kernel/hook.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,7 @@ impl<System: raw::KernelBase> StartupHookDefiner<System> {
108108
let startup_hooks = &mut cfg.startup_hooks;
109109
let order = startup_hooks.len();
110110
startup_hooks.push(CfgStartupHook {
111-
// FIXME: Work-around for `Option::expect` being not `const fn`
112-
start: if let Some(x) = self.start {
113-
x
114-
} else {
115-
panic!("`start` is not specified")
116-
},
111+
start: self.start.expect("`start` is not specified"),
117112
param: self.param,
118113
priority: self.priority,
119114
order,

src/r3/src/kernel/interrupt.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,7 @@ impl<System: raw::KernelInterruptLine> InterruptLineDefiner<System> {
231231
self,
232232
cfg: &mut Cfg<C>,
233233
) -> InterruptLine<System> {
234-
// FIXME: Work-around for `Option::expect` being not `const fn`
235-
let line_num = if let Some(line) = self.line {
236-
line
237-
} else {
238-
panic!("`line` is not specified");
239-
};
234+
let line_num = self.line.expect("`line` is not specified");
240235

241236
// Create a `CfgBuilderInterruptLine` for `line_num` if it doesn't exist
242237
// yet
@@ -358,25 +353,15 @@ impl<System: raw::KernelInterruptLine> InterruptHandlerDefiner<System> {
358353
self,
359354
cfg: &mut Cfg<C>,
360355
) -> InterruptHandler<System> {
361-
// FIXME: Work-around for `Option::expect` being not `const fn`
362-
let line_num = if let Some(line) = self.line {
363-
line
364-
} else {
365-
panic!("`line` is not specified");
366-
};
356+
let line_num = self.line.expect("`line` is not specified");
367357

368358
// Add a `CfgInterruptLineInfo` at the same time
369359
InterruptLine::define().line(line_num).finish(cfg);
370360

371361
let order = cfg.interrupt_handlers.len();
372362
cfg.interrupt_handlers.push(CfgInterruptHandler {
373363
line: line_num,
374-
// FIXME: Work-around for `Option::expect` being not `const fn`
375-
start: if let Some(x) = self.start {
376-
x
377-
} else {
378-
panic!("`start` is not specified")
379-
},
364+
start: self.start.expect("`start` is not specified"),
380365
param: self.param,
381366
priority: self.priority,
382367
unmanaged: self.unmanaged,

src/r3/src/kernel/semaphore.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,8 @@ impl<System: raw::KernelSemaphore> SemaphoreDefiner<System> {
231231
self,
232232
c: &mut Cfg<C>,
233233
) -> Semaphore<System> {
234-
let initial_value = if let Some(x) = self.initial_value {
235-
x
236-
} else {
237-
panic!("`initial` is not specified")
238-
};
239-
let maximum_value = if let Some(x) = self.maximum_value {
240-
x
241-
} else {
242-
panic!("`maximum` is not specified")
243-
};
234+
let initial_value = self.initial_value.expect("`initial` is not specified");
235+
let maximum_value = self.maximum_value.expect("`maximum` is not specified");
244236

245237
let id = c.raw().semaphore_define(
246238
raw_cfg::SemaphoreDescriptor {

src/r3/src/kernel/task.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,14 @@ impl<System: raw::KernelBase> TaskDefiner<System> {
343343
let id = cfg.raw().task_define(
344344
raw_cfg::TaskDescriptor {
345345
phantom: Init::INIT,
346-
start: if let Some(x) = self.start {
347-
x
348-
} else {
349-
panic!("`start` (task entry point) is not specified")
350-
},
346+
start: self
347+
.start
348+
.expect("`start` (task entry point) is not specified"),
351349
param: self.param,
352350
active: self.active,
353-
priority: if let Some(x) = self.priority {
354-
x
355-
} else {
356-
panic!("`priority` (task entry point) is not specified")
357-
},
351+
priority: self
352+
.priority
353+
.expect("`priority` (task entry point) is not specified"),
358354
stack_size: self.stack_size,
359355
},
360356
(),

src/r3/src/kernel/timer.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,9 @@ impl<System: raw::KernelTimer> TimerDefiner<System> {
542542
let id = c.raw().timer_define(
543543
raw_cfg::TimerDescriptor {
544544
phantom: Init::INIT,
545-
// FIXME: Work-around for `Option::expect` being not `const fn`
546-
start: if let Some(x) = self.start {
547-
x
548-
} else {
549-
panic!("`start` (timer callback function) is not specified")
550-
},
545+
start: self
546+
.start
547+
.expect("`start` (timer callback function) is not specified"),
551548
param: self.param,
552549
delay: self.delay,
553550
period: self.period,

src/r3/src/time/duration.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,15 @@ impl Duration {
4747
/// Pancis if `millis` overflows the representable range of `Duration`.
4848
#[inline]
4949
pub const fn from_millis(millis: i32) -> Self {
50-
// FIXME: `Option::expect` is not `const fn` yet
51-
Self::from_micros(if let Some(x) = millis.checked_mul(1_000) {
52-
x
53-
} else {
54-
panic!("duration overflow");
55-
})
50+
Self::from_micros(millis.checked_mul(1_000).expect("duration overflow"))
5651
}
5752

5853
/// Construct a new `Duration` from the specified number of seconds.
5954
///
6055
/// Pancis if `secs` overflows the representable range of `Duration`.
6156
#[inline]
6257
pub const fn from_secs(secs: i32) -> Self {
63-
// FIXME: `Option::expect` is not `const fn` yet
64-
Self::from_micros(if let Some(x) = secs.checked_mul(1_000_000) {
65-
x
66-
} else {
67-
panic!("duration overflow");
68-
})
58+
Self::from_micros(secs.checked_mul(1_000_000).expect("duration overflow"))
6959
}
7060

7161
/// Get the total number of whole microseconds contained by this `Duration`.

src/r3/src/time/time.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,15 @@ impl Time {
5151
/// Pancis if `millis` overflows the representable range of `Time`.
5252
#[inline]
5353
pub const fn from_millis(millis: u64) -> Self {
54-
// FIXME: `Option::expect` is not `const fn` yet
55-
Self::from_micros(if let Some(x) = millis.checked_mul(1_000) {
56-
x
57-
} else {
58-
panic!("duration overflow");
59-
})
54+
Self::from_micros(millis.checked_mul(1_000).expect("duration overflow"))
6055
}
6156

6257
/// Construct a new `Time` from the specified number of seconds.
6358
///
6459
/// Pancis if `secs` overflows the representable range of `Time`.
6560
#[inline]
6661
pub const fn from_secs(secs: u64) -> Self {
67-
// FIXME: `Option::expect` is not `const fn` yet
68-
Self::from_micros(if let Some(x) = secs.checked_mul(1_000_000) {
69-
x
70-
} else {
71-
panic!("duration overflow");
72-
})
62+
Self::from_micros(secs.checked_mul(1_000_000).expect("duration overflow"))
7363
}
7464

7565
/// Get the total number of whole microseconds contained in the time span

0 commit comments

Comments
 (0)