From: Ralf Jung Date: Mon, 3 Jul 2017 21:18:54 +0000 (-0700) Subject: Merge pull request #27 from TorstenScheck/fix-list-sigsegv-reworked X-Git-Url: https://git.ralfj.de/rust-101.git/commitdiff_plain/26ec22862fa81bba24e3f646cf482439999b0021?hp=d20f37032a05c84366b7287095a9aada44ff9884 Merge pull request #27 from TorstenScheck/fix-list-sigsegv-reworked Fix list sigsegv reworked --- diff --git a/solutions/src/list.rs b/solutions/src/list.rs index 3206982..1c794fc 100644 --- a/solutions/src/list.rs +++ b/solutions/src/list.rs @@ -53,6 +53,8 @@ impl LinkedList { if new_last.is_null() { // The list is now empty. self.first = new_last; + } else { + unsafe { (*new_last).next = ptr::null_mut() }; } let last = unsafe { raw_into_box(last) } ; Some(last.data) @@ -86,6 +88,8 @@ impl LinkedList { if new_first.is_null() { // The list is now empty. self.last = new_first; + } else { + unsafe { (*new_first).prev = ptr::null_mut() }; } let first = unsafe { raw_into_box(first) } ; Some(first.data) @@ -200,4 +204,20 @@ mod tests { } assert_eq!(count.count.get(), 20); } + + #[test] + fn test_iter_mut() { + let mut l = LinkedList::::new(); + for i in 0..5 { + l.push_back(i); + } + + assert_eq!(l.pop_front(), Some(0)); + assert_eq!(l.pop_back(), Some(4)); + + for (n, i) in l.iter_mut().enumerate() { + *i-=1; + assert_eq!(n as i32, *i); + } + } }