-
Notifications
You must be signed in to change notification settings - Fork 296
Open
Description
I think there is a bug in the final code. By copying the 7.12 final code to the Rust playground and running the following you can observe it:
let mut m: LinkedList<u32> = LinkedList::new();
m.extend([1, 2, 3, 4, 5, 6]);
let mut cursor = m.cursor_mut();
assert_eq!(cursor.current(), None);
cursor.move_next();
assert_eq!(cursor.current(), Some(&mut 1));
let before = cursor.split_before();
//prints false then the program panics
println!("{:#?}", before.front.is_none());The issue is that in the new list before we create, the front still points to the node with element 1 in m.
This bug is from the split_before function in 7.10:
pub fn split_before(&mut self) -> LinkedList<T> {
if let Some(cur) = self.cur {
// We are pointing at a real element, so the list is non-empty.
unsafe {
//...
// What the output will become
let output_front = self.list.front; // <-- incorrect if we are at index 0 (the first non-ghost node)
//.....
LinkedList {
front: output_front, <-- this is still self.list.front.
back: output_back,
len: output_len,
_boo: PhantomData,
}
//.....
}There is also a mirror of this bug in split_after (where output_back = self.list.back even if the cursor is on the last non-ghost node in the list).
You can observe it by running:
let mut m: LinkedList<u32> = LinkedList::new();
m.extend([1, 2, 3, 4, 5, 6]);
let mut cursor = m.cursor_mut();
assert_eq!(cursor.current(), None);
cursor.move_prev();
assert_eq!(cursor.current(), Some(&mut 6));
let after = cursor.split_after();
//prints false
println!("{:#?}", after.back.is_none());escape0707
Metadata
Metadata
Assignees
Labels
No labels