Skip to content

Commit

Permalink
style: use let-else in a few places (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Nov 16, 2022
1 parent 5e5ee6b commit d7d07cb
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 53 deletions.
7 changes: 3 additions & 4 deletions alloc/src/buddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,14 @@ impl<const FREE_LISTS: usize> Alloc<FREE_LISTS> {
tracing::trace!(?min_order);
let min_order = min_order.ok_or_else(AllocErr::oom)?;

let size = match self.size_for(layout) {
Some(size) => size,
let Some(size) = self.size_for(layout) else {
// XXX(eliza): is it better to just leak it?
None => panic!(
panic!(
"couldn't determine the correct layout for an allocation \
we previously allocated successfully, what the actual fuck!\n \
addr={:?}; layout={:?}; min_order={}",
paddr, layout, min_order,
),
)
};

// Construct a new free block.
Expand Down
39 changes: 16 additions & 23 deletions cordyceps/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,11 @@ impl<T: Linked<Links<T>> + ?Sized> List<T> {
///
/// This operation should compute in *O*(1) time and *O*(1) memory.
pub fn append(&mut self, other: &mut Self) {
let tail = match self.tail {
let Some(tail) = self.tail else {
// if this list is empty, simply replace it with `other`
None => {
debug_assert!(self.is_empty());
mem::swap(self, other);
return;
}
Some(tail) => tail,
debug_assert!(self.is_empty());
mem::swap(self, other);
return;
};

// if `other` is empty, do nothing.
Expand Down Expand Up @@ -450,19 +447,16 @@ impl<T: Linked<Links<T>> + ?Sized> List<T> {
/// Asserts as many of the linked list's invariants as possible.
#[track_caller]
pub(crate) fn assert_valid_named(&self, name: &str) {
let head = match self.head {
Some(head) => head,
None => {
assert!(
self.tail.is_none(),
"{name}if the linked list's head is null, the tail must also be null"
);
assert_eq!(
self.len, 0,
"{name}if a linked list's head is null, its length must be 0"
);
return;
}
let Some(head) = self.head else {
assert!(
self.tail.is_none(),
"{name}if the linked list's head is null, the tail must also be null"
);
assert_eq!(
self.len, 0,
"{name}if a linked list's head is null, its length must be 0"
);
return;
};

assert_ne!(
Expand Down Expand Up @@ -928,9 +922,8 @@ impl<T: Linked<Links<T>> + ?Sized> List<T> {

#[inline]
unsafe fn split_after_node(&mut self, split_node: Link<T>, idx: usize) -> Self {
let split_node = match split_node {
Some(node) => node,
None => return mem::replace(self, Self::new()),
let Some(split_node) = split_node else {
return mem::replace(self, Self::new());
};

// the head of the new list is the split node's `next` node (which is
Expand Down
15 changes: 6 additions & 9 deletions cordyceps/src/list/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,9 @@ impl<'list, T: Linked<Links<T>> + ?Sized> CursorMut<'list, T> {
let split_at = self.core.index;
self.core.index = 0;

let split_node = match self.core.curr {
Some(node) => node,
let Some(split_node) = self.core.curr else {
// the split portion is the entire list. just return it.
None => return mem::replace(self.core.list, List::new()),
return mem::replace(self.core.list, List::new())
};

// the tail of the new list is the split node's `prev` node (which is
Expand Down Expand Up @@ -374,10 +373,9 @@ impl<'list, T: Linked<Links<T>> + ?Sized> CursorMut<'list, T> {
/// If the cursor is pointing at the null element, then the contents of
/// `spliced` are inserted at the beginning of the `List` the cursor points to.
pub fn splice_after(&mut self, mut spliced: List<T>) {
let (splice_head, splice_tail, splice_len) = match spliced.take_all() {
Some(spliced) => spliced,
let Some((splice_head, splice_tail, splice_len)) = spliced.take_all() else {
// the spliced list is empty, do nothing.
None => return,
return;
};

let next = self.core.next_link();
Expand All @@ -404,10 +402,9 @@ impl<'list, T: Linked<Links<T>> + ?Sized> CursorMut<'list, T> {
/// If the cursor is pointing at the null element, then the contents of
/// `spliced` are inserted at the end of the `List` the cursor points to.
pub fn splice_before(&mut self, mut spliced: List<T>) {
let (splice_head, splice_tail, splice_len) = match spliced.take_all() {
Some(spliced) => spliced,
let Some((splice_head, splice_tail, splice_len)) = spliced.take_all() else {
// the spliced list is empty, do nothing.
None => return,
return;
};

let prev = self.core.prev_link();
Expand Down
5 changes: 1 addition & 4 deletions maitake/src/scheduler/steal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ impl<'worker, S: Schedule> Stealer<'worker, S> {
/// - `true` if a task was successfully stolen.
/// - `false` if the targeted queue is empty.
pub fn spawn_one(&self, scheduler: &S) -> bool {
let task = match self.queue.dequeue() {
Some(task) => task,
None => return false,
};
let Some(task) = self.queue.dequeue() else { return false };
test_trace!(?task, "stole");

// decrement the target queue's task count
Expand Down
5 changes: 1 addition & 4 deletions maitake/src/sync/wait_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,7 @@ pub(crate) mod test_util {
let this = Arc::downgrade(&self);
drop(self);
futures_util::future::poll_fn(move |cx| {
let this = match this.upgrade() {
Some(this) => this,
None => return Poll::Ready(()),
};
let Some(this) = this.upgrade() else {return Poll::Ready(()) };

let res = this.task.wait();
futures_util::pin_mut!(res);
Expand Down
9 changes: 3 additions & 6 deletions mycotest/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ impl<'a> TestName<'a> {

#[tracing::instrument(level = "trace")]
pub fn parse_outcome(line: &'a str) -> Result<Option<(Self, Outcome)>, ParseError> {
let line = match line.strip_prefix("MYCELIUM_TEST_") {
None => {
tracing::trace!("not a test outcome");
return Ok(None);
}
Some(line) => line,
let Some(line) = line.strip_prefix("MYCELIUM_TEST_") else {
tracing::trace!("not a test outcome");
return Ok(None);
};
tracing::trace!(?line);
let (line, result) = if let Some(line) = line.strip_prefix("PASS:") {
Expand Down
5 changes: 2 additions & 3 deletions src/arch/x86_64/framebuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ pub(super) fn init(bootinfo: &mut BootInfo) -> bool {
}

// Okay, try to initialize the framebuffer
let framebuffer = match mem::replace(&mut bootinfo.framebuffer, Optional::None) {
Optional::Some(framebuffer) => framebuffer,
let Optional::Some(framebuffer) = mem::replace(&mut bootinfo.framebuffer, Optional::None) else {
// The boot info does not contain a framebuffer configuration. Nothing
// for us to do!
Optional::None => return false,
return false;
};

let info = framebuffer.info();
Expand Down

0 comments on commit d7d07cb

Please sign in to comment.