Skip to content

Commit

Permalink
fix(cordyceps): remove let ... else syntax (#373)
Browse files Browse the repository at this point in the history
I changed all the crates to use this syntax in #335 when updating, but
I forgot that `cordyceps` has a MSRV. Whoops!
  • Loading branch information
hawkw committed Nov 16, 2022
1 parent 2215dcf commit 36986e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
47 changes: 30 additions & 17 deletions cordyceps/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,16 @@ 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 Some(tail) = self.tail else {
// if this list is empty, simply replace it with `other`
debug_assert!(self.is_empty());
mem::swap(self, other);
return;
// TODO(eliza): this could be rewritten to use `let ... else` when
// that's supported on `cordyceps`' MSRV.
let tail = match self.tail {
Some(tail) => tail,
None => {
// if this list is empty, simply replace it with `other`
debug_assert!(self.is_empty());
mem::swap(self, other);
return;
}
};

// if `other` is empty, do nothing.
Expand Down Expand Up @@ -447,16 +452,21 @@ 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 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;
// TODO(eliza): this could be rewritten to use `let ... else` when
// that's supported on `cordyceps`' MSRV.
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;
}
};

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

#[inline]
unsafe fn split_after_node(&mut self, split_node: Link<T>, idx: usize) -> Self {
let Some(split_node) = split_node else {
return mem::replace(self, Self::new());
// TODO(eliza): this could be rewritten to use `let ... else` when
// that's supported on `cordyceps`' MSRV.
let split_node = match split_node {
Some(node) => node,
None => return mem::replace(self, Self::new()),
};

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

let Some(split_node) = self.core.curr else {
// TODO(eliza): this could be rewritten to use `let ... else` when
// that's supported on `cordyceps`' MSRV.
let split_node = match self.core.curr {
Some(node) => node,
// the split portion is the entire list. just return it.
return mem::replace(self.core.list, List::new())
None => 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 @@ -373,9 +376,12 @@ 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 Some((splice_head, splice_tail, splice_len)) = spliced.take_all() else {
// TODO(eliza): this could be rewritten to use `let ... else` when
// that's supported on `cordyceps`' MSRV.
let (splice_head, splice_tail, splice_len) = match spliced.take_all() {
Some(splice) => splice,
// the spliced list is empty, do nothing.
return;
None => return,
};

let next = self.core.next_link();
Expand All @@ -402,9 +408,12 @@ 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 Some((splice_head, splice_tail, splice_len)) = spliced.take_all() else {
// TODO(eliza): this could be rewritten to use `let ... else` when
// that's supported on `cordyceps`' MSRV.
let (splice_head, splice_tail, splice_len) = match spliced.take_all() {
Some(splice) => splice,
// the spliced list is empty, do nothing.
return;
None => return,
};

let prev = self.core.prev_link();
Expand Down

0 comments on commit 36986e7

Please sign in to comment.