From fd79836016ab157c9dd2628c97e3c282abebf852 Mon Sep 17 00:00:00 2001 From: Lewin Bormann Date: Mon, 7 Mar 2022 21:52:56 +0100 Subject: [PATCH] #13: fix crash after last change --- src/cache.rs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index c776081..bc38f4e 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -18,15 +18,14 @@ struct LRUList { /// This is likely unstable; more investigation is needed into correct behavior! impl LRUList { fn new() -> LRUList { - let l = LRUList { + LRUList { head: LRUNode { data: None, next: None, prev: None, }, count: 0, - }; - l + } } /// Inserts new element at front (least recently used element) @@ -71,10 +70,7 @@ impl LRUList { return None; } let mut lasto = unsafe { - replace( - &mut (*((*self.head.prev.unwrap()).prev.unwrap())).next, - None, - ) + (*((*self.head.prev.unwrap()).prev.unwrap())).next.take() }; assert!(lasto.is_some()); @@ -83,7 +79,7 @@ impl LRUList { assert!(self.head.prev.is_some()); self.head.prev = last.prev; self.count -= 1; - replace(&mut (*last).data, None) + (*last).data.take() } else { None } @@ -91,17 +87,16 @@ impl LRUList { fn remove(&mut self, node_handle: LRUHandle) -> T { unsafe { - let d = replace(&mut (*node_handle).data, None).unwrap(); - // If has next - if let Some(ref mut nextp) = (*node_handle).next { - swap(&mut (**nextp).prev, &mut (*node_handle).prev); - } - // If has prev - if let Some(ref mut prevp) = (*node_handle).prev { - // swap prev.next with sink. sink will be dropped. - let mut sink = (*node_handle).next.take(); - swap(&mut (**prevp).next, &mut sink); + let d = (*node_handle).data.take().unwrap(); + // Take ownership of node to be removed. + let mut current = (*(*node_handle).prev.unwrap()).next.take().unwrap(); + let prev = current.prev.unwrap(); + // Update previous node's successor. + if current.next.is_some() { + // Update next node's predecessor. + current.next.as_mut().unwrap().prev = current.prev.take(); } + (*prev).next = current.next.take(); self.count -= 1;