Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code using 'cargo fmt' #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
86 changes: 44 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down Expand Up @@ -132,10 +130,7 @@ impl<T: Trace<T>> Heap<T> {
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
Expand All @@ -145,7 +140,8 @@ impl<T: Trace<T>> Heap<T> {
debug_assert!(self.contains(handle));

Rooted {
rc: self.rooted
rc: self
.rooted
.entry(*handle)
.or_insert_with(|| Rc::new(()))
.clone(),
Expand Down Expand Up @@ -212,7 +208,7 @@ impl<T: Trace<T>> Heap<T> {
/// 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<Item=Handle<T>>) {
pub fn clean_excluding(&mut self, excluding: impl IntoIterator<Item = Handle<T>>) {
let new_sweep = self.last_sweep + 1;
let mut tracer = Tracer {
new_sweep,
Expand All @@ -221,41 +217,43 @@ impl<T: Trace<T>> Heap<T> {
};

// 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;
}
Expand Down Expand Up @@ -332,7 +330,10 @@ impl<T> Handle<T> {
impl<T> Copy for Handle<T> {}
impl<T> Clone for Handle<T> {
fn clone(&self) -> Self {
Self { gen: self.gen, ptr: self.ptr }
Self {
gen: self.gen,
ptr: self.ptr,
}
}
}

Expand Down Expand Up @@ -413,20 +414,21 @@ mod tests {
impl<'a> Trace<Self> for Value<'a> {
fn trace(&self, tracer: &mut Tracer<Self>) {
match self {
Value::Base(_) => {},
Value::Base(_) => {}
Value::Refs(_, a, b) => {
a.trace(tracer);
b.trace(tracer);
},
}
}
}
}

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)
}
};
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ pub struct Tracer<'a, T: Trace<T>> {

impl<'a, T: Trace<T>> Tracer<'a, T> {
pub(crate) fn mark(&mut self, handle: Handle<T>) {
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);
}
}
}
}
Expand All @@ -70,11 +73,7 @@ impl<O: Trace<O>> Trace<O> for Rooted<O> {
}

// Impl on standard things
use std::collections::{
HashMap as StdHashMap,
VecDeque,
LinkedList,
};
use std::collections::{HashMap as StdHashMap, LinkedList, VecDeque};

impl<O: Trace<O>, T: Trace<O>> Trace<O> for [T] {
fn trace(&self, tracer: &mut Tracer<O>) {
Expand Down