Skip to content

Commit

Permalink
#13: fix crash after last change
Browse files Browse the repository at this point in the history
  • Loading branch information
dermesser committed Mar 7, 2022
1 parent 0de33d9 commit fd79836
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ struct LRUList<T> {
/// This is likely unstable; more investigation is needed into correct behavior!
impl<T> LRUList<T> {
fn new() -> LRUList<T> {
let l = LRUList {
LRUList {
head: LRUNode {
data: None,
next: None,
prev: None,
},
count: 0,
};
l
}
}

/// Inserts new element at front (least recently used element)
Expand Down Expand Up @@ -71,10 +70,7 @@ impl<T> LRUList<T> {
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());
Expand All @@ -83,25 +79,24 @@ impl<T> LRUList<T> {
assert!(self.head.prev.is_some());
self.head.prev = last.prev;
self.count -= 1;
replace(&mut (*last).data, None)
(*last).data.take()
} else {
None
}
}

fn remove(&mut self, node_handle: LRUHandle<T>) -> 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;

Expand Down

0 comments on commit fd79836

Please sign in to comment.