diff --git a/benches/basic.rs b/benches/basic.rs index 9793db7..872c14d 100644 --- a/benches/basic.rs +++ b/benches/basic.rs @@ -2,9 +2,9 @@ extern crate test; -use test::{Bencher, black_box}; -use rand::prelude::*; use broom::prelude::*; +use rand::prelude::*; +use test::{black_box, Bencher}; pub enum Value { Root, diff --git a/src/lib.rs b/src/lib.rs index 361f5af..1ddac32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,21 +54,19 @@ pub mod trace; +use crate::trace::*; +use hashbrown::{HashMap, HashSet}; use std::{ - cmp::{PartialEq, Eq}, - rc::Rc, + cmp::{Eq, PartialEq}, hash::{Hash, Hasher}, + rc::Rc, }; -use hashbrown::{HashMap, HashSet}; -use crate::trace::*; /// Common items that you'll probably need often. pub mod prelude { pub use super::{ - Heap, - Handle, - Rooted, trace::{Trace, Tracer}, + Handle, Heap, Rooted, }; } @@ -132,10 +130,7 @@ impl> Heap { let rc = Rc::new(()); self.rooted.insert(handle, rc.clone()); - Rooted { - rc, - handle, - } + Rooted { rc, handle } } /// Upgrade a handle (that will be cleared by the garbage collector) into a rooted handle (that @@ -145,7 +140,8 @@ impl> Heap { debug_assert!(self.contains(handle)); Rooted { - rc: self.rooted + rc: self + .rooted .entry(*handle) .or_insert_with(|| Rc::new(())) .clone(), @@ -212,7 +208,7 @@ impl> Heap { /// This function is useful in circumstances in which you wish to keep certain items alive over /// a garbage collection without the addition cost of a [`Rooted`] handle. An example of this /// might be stack items in a garbage-collected language - pub fn clean_excluding(&mut self, excluding: impl IntoIterator>) { + pub fn clean_excluding(&mut self, excluding: impl IntoIterator>) { let new_sweep = self.last_sweep + 1; let mut tracer = Tracer { new_sweep, @@ -221,41 +217,43 @@ impl> Heap { }; // Mark - self.rooted - .retain(|handle, rc| { - if Rc::strong_count(rc) > 1 { - tracer.mark(*handle); - unsafe { (&*handle.ptr).trace(&mut tracer); } - true - } else { - false + self.rooted.retain(|handle, rc| { + if Rc::strong_count(rc) > 1 { + tracer.mark(*handle); + unsafe { + (&*handle.ptr).trace(&mut tracer); } - }); + true + } else { + false + } + }); let objects = &self.objects; excluding .into_iter() .filter(|handle| objects.contains(&handle)) .for_each(|handle| { tracer.mark(handle); - unsafe { (&*handle.ptr).trace(&mut tracer); } + unsafe { + (&*handle.ptr).trace(&mut tracer); + } }); // Sweep let object_sweeps = &mut self.object_sweeps; - self.objects - .retain(|handle| { - if object_sweeps - .get(handle) - .map(|sweep| *sweep == new_sweep) - .unwrap_or(false) - { - true - } else { - object_sweeps.remove(handle); - drop(unsafe { Box::from_raw(handle.ptr) }); - false - } - }); + self.objects.retain(|handle| { + if object_sweeps + .get(handle) + .map(|sweep| *sweep == new_sweep) + .unwrap_or(false) + { + true + } else { + object_sweeps.remove(handle); + drop(unsafe { Box::from_raw(handle.ptr) }); + false + } + }); self.last_sweep = new_sweep; } @@ -332,7 +330,10 @@ impl Handle { impl Copy for Handle {} impl Clone for Handle { fn clone(&self) -> Self { - Self { gen: self.gen, ptr: self.ptr } + Self { + gen: self.gen, + ptr: self.ptr, + } } } @@ -413,11 +414,11 @@ mod tests { impl<'a> Trace for Value<'a> { fn trace(&self, tracer: &mut Tracer) { match self { - Value::Base(_) => {}, + Value::Base(_) => {} Value::Refs(_, a, b) => { a.trace(tracer); b.trace(tracer); - }, + } } } } @@ -425,8 +426,9 @@ mod tests { impl<'a> Drop for Value<'a> { fn drop(&mut self) { match self { - Value::Base(count) | Value::Refs(count, _, _) => - count.fetch_sub(1, Ordering::Relaxed), + Value::Base(count) | Value::Refs(count, _, _) => { + count.fetch_sub(1, Ordering::Relaxed) + } }; } } diff --git a/src/trace.rs b/src/trace.rs index e34583b..8ee8acc 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -47,12 +47,15 @@ pub struct Tracer<'a, T: Trace> { impl<'a, T: Trace> Tracer<'a, T> { pub(crate) fn mark(&mut self, handle: Handle) { - let sweep = self.object_sweeps + let sweep = self + .object_sweeps .entry(handle) .or_insert(self.new_sweep - 1); if *sweep != self.new_sweep && self.objects.contains(&handle) { *sweep = self.new_sweep; - unsafe { (&*handle.ptr).trace(self); } + unsafe { + (&*handle.ptr).trace(self); + } } } } @@ -70,11 +73,7 @@ impl> Trace for Rooted { } // Impl on standard things -use std::collections::{ - HashMap as StdHashMap, - VecDeque, - LinkedList, -}; +use std::collections::{HashMap as StdHashMap, LinkedList, VecDeque}; impl, T: Trace> Trace for [T] { fn trace(&self, tracer: &mut Tracer) {