Open morlinbrot opened 1 year ago
+1. I just stumbled upon this too and filed #300. I have a small change-set to fix this, although I did it in a slightly different way. I put an explicit case for if len == 0, set front / back to None. I thought it fit nicely in the article as it isolates the specific case that was originally missed. Here are some tests that also show the bug:
#[test]
fn test_empty_slice() {
let mut list = LinkedList::new();
list.push_back(7);
list.push_back(8);
list.push_back(9);
let mut cur = list.cursor_mut();
cur.move_next(); // Point at the first node
let result = cur.split_before(); // Everything before the first node
assert_eq!(result.len, 0);
assert_eq!(result.front(), None);
assert_eq!(result.back(), None);
assert_eq!(list.len, 3);
assert_eq!(list.front(), Some(7).as_ref());
let mut cur = list.cursor_mut();
cur.move_prev(); // Point at the last node
let result = cur.split_after(); // Everything after the last node
assert_eq!(result.len, 0);
assert_eq!(result.front(), None);
assert_eq!(result.back(), None);
}
But since you already have a PR and since it seems none of the PRs here are being merged, I don't think I'll bother. This was a great exercise in learning about the lists.
Fixes #238
Both methods returned illegal lists when the cursor was at the first or last node respectively.
Note that I PR'd the same fix over on contain-rs/linked-list but the implementation is slightly different. Here, we are still setting the head/tail optimistically as before but remove it again later (when encountering the edge case):
It seemed to me that this fit better into the flow of the article. Difference to the implementation I did before:
My OCD prefers the latter version as we only ever set the head when it is actually required instead of setting and then maybe unsetting again. Let me know if you prefer any version and I will update any one of the PRs to unify the implementations, if desired.