diff --git a/gc/benches/alloc_in_a_loop.rs b/gc/benches/alloc_in_a_loop.rs index af43004..3f255fd 100644 --- a/gc/benches/alloc_in_a_loop.rs +++ b/gc/benches/alloc_in_a_loop.rs @@ -15,9 +15,7 @@ fn discard(b: &mut test::Bencher, n: usize) { fn keep(b: &mut test::Bencher, n: usize) { b.iter(|| { gc::force_collect(); - (0..n) - .map(|_| gc::Gc::new(THING)) - .collect::>() + (0..n).map(|_| gc::Gc::new(THING)).collect::>() }) } diff --git a/gc/src/gc.rs b/gc/src/gc.rs index b97234e..d2a728d 100644 --- a/gc/src/gc.rs +++ b/gc/src/gc.rs @@ -1,8 +1,7 @@ +use crate::trace::{Finalize, Trace}; use std::cell::{Cell, RefCell}; use std::mem; use std::ptr::NonNull; -use crate::trace::{Finalize, Trace}; - const INITIAL_THRESHOLD: usize = 100; @@ -134,7 +133,9 @@ impl GcBox { pub(crate) unsafe fn root_inner(&self) { // abort if the count overflows to prevent `mem::forget` loops that could otherwise lead to // erroneous drops - self.header.roots.set(self.header.roots.get().checked_add(1).unwrap()); + self.header + .roots + .set(self.header.roots.get().checked_add(1).unwrap()); } /// Decreases the root count on this `GcBox`. diff --git a/gc/src/trace.rs b/gc/src/trace.rs index 8fe9444..c7f6641 100644 --- a/gc/src/trace.rs +++ b/gc/src/trace.rs @@ -1,5 +1,5 @@ -use std::collections::{BinaryHeap, BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque}; -use std::hash::{Hash, BuildHasher}; +use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; +use std::hash::{BuildHasher, Hash}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize}; @@ -114,9 +114,29 @@ macro_rules! simple_empty_finalize_trace { } } -simple_empty_finalize_trace![(), isize, usize, bool, i8, u8, i16, u16, i32, - u32, i64, u64, f32, f64, char, String, Path, PathBuf, AtomicBool, - AtomicIsize, AtomicUsize]; +simple_empty_finalize_trace![ + (), + isize, + usize, + bool, + i8, + u8, + i16, + u16, + i32, + u32, + i64, + u64, + f32, + f64, + char, + String, + Path, + PathBuf, + AtomicBool, + AtomicIsize, + AtomicUsize +]; simple_empty_finalize_trace![i128, u128]; @@ -130,7 +150,7 @@ macro_rules! array_finalize_trace { } }); } - } + }; } macro_rules! fn_finalize_trace_one { @@ -189,10 +209,8 @@ macro_rules! type_arg_tuple_based_finalized_trace_impls { } array_finalize_trace_impls![ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27, 28, 29, 30, 31 ]; type_arg_tuple_based_finalized_trace_impls![ (); diff --git a/gc/tests/finalize.rs b/gc/tests/finalize.rs index b56a19c..524aa02 100644 --- a/gc/tests/finalize.rs +++ b/gc/tests/finalize.rs @@ -1,7 +1,7 @@ #![cfg_attr(feature = "nightly", feature(specialization))] use gc::{Finalize, Trace}; -use gc_derive::{Trace, Finalize}; +use gc_derive::{Finalize, Trace}; use std::cell::Cell; #[derive(PartialEq, Eq, Debug, Clone, Copy)] diff --git a/gc/tests/gc_semantics.rs b/gc/tests/gc_semantics.rs index 40582a7..c5245df 100644 --- a/gc/tests/gc_semantics.rs +++ b/gc/tests/gc_semantics.rs @@ -1,9 +1,9 @@ #![cfg_attr(feature = "nightly", feature(specialization))] +use gc::{force_collect, Finalize, Gc, GcCell, Trace}; +use gc_derive::{Finalize, Trace}; use std::cell::Cell; use std::thread::LocalKey; -use gc::{Trace, Finalize, GcCell, Gc, force_collect}; -use gc_derive::{Trace, Finalize}; // Utility methods for the tests #[derive(PartialEq, Eq, Debug, Clone, Copy)] diff --git a/gc/tests/gymnastics_cycle.rs b/gc/tests/gymnastics_cycle.rs index 34ad8a5..c6f2046 100644 --- a/gc/tests/gymnastics_cycle.rs +++ b/gc/tests/gymnastics_cycle.rs @@ -1,8 +1,8 @@ #![cfg_attr(feature = "nightly", feature(specialization))] -use std::cell::Cell; -use gc::{GcCell, Gc, force_collect}; +use gc::{force_collect, Gc, GcCell}; use gc_derive::Trace; +use std::cell::Cell; thread_local!(static COUNTER: Cell = Cell::new(0u8)); @@ -23,9 +23,9 @@ impl gc::Finalize for Cyclic { fn test_cycle() { { let mut gcs = vec![Gc::new(Cyclic { - prev: GcCell::new(None), - name: 0, - })]; + prev: GcCell::new(None), + name: 0, + })]; for i in 1..4 { let prev = gcs[i - 1].clone(); diff --git a/gc/tests/trace_impl.rs b/gc/tests/trace_impl.rs index 6737ba0..a3109ab 100644 --- a/gc/tests/trace_impl.rs +++ b/gc/tests/trace_impl.rs @@ -1,6 +1,6 @@ #![cfg_attr(feature = "nightly", feature(specialization))] -use gc_derive::{Trace, Finalize}; +use gc_derive::{Finalize, Trace}; use std::cell::RefCell; thread_local!(static X: RefCell = RefCell::new(0)); diff --git a/gc_derive/src/lib.rs b/gc_derive/src/lib.rs index df69991..8a35ae3 100644 --- a/gc_derive/src/lib.rs +++ b/gc_derive/src/lib.rs @@ -1,58 +1,69 @@ -use synstructure::{decl_derive, Structure}; use quote::quote; +use synstructure::{decl_derive, Structure}; decl_derive!([Trace, attributes(unsafe_ignore_trace)] => derive_trace); fn derive_trace(mut s: Structure<'_>) -> proc_macro2::TokenStream { - s.filter(|bi| !bi.ast().attrs.iter().any(|attr| attr.path.is_ident("unsafe_ignore_trace"))); + s.filter(|bi| { + !bi.ast() + .attrs + .iter() + .any(|attr| attr.path.is_ident("unsafe_ignore_trace")) + }); let trace_body = s.each(|bi| quote!(mark(#bi))); - let trace_impl = s.unsafe_bound_impl(quote!(::gc::Trace), quote! { - #[inline] unsafe fn trace(&self) { - #[allow(dead_code)] - #[inline] - unsafe fn mark(it: &T) { - ::gc::Trace::trace(it); + let trace_impl = s.unsafe_bound_impl( + quote!(::gc::Trace), + quote! { + #[inline] unsafe fn trace(&self) { + #[allow(dead_code)] + #[inline] + unsafe fn mark(it: &T) { + ::gc::Trace::trace(it); + } + match *self { #trace_body } } - match *self { #trace_body } - } - #[inline] unsafe fn root(&self) { - #[allow(dead_code)] - #[inline] - unsafe fn mark(it: &T) { - ::gc::Trace::root(it); + #[inline] unsafe fn root(&self) { + #[allow(dead_code)] + #[inline] + unsafe fn mark(it: &T) { + ::gc::Trace::root(it); + } + match *self { #trace_body } } - match *self { #trace_body } - } - #[inline] unsafe fn unroot(&self) { - #[allow(dead_code)] - #[inline] - unsafe fn mark(it: &T) { - ::gc::Trace::unroot(it); + #[inline] unsafe fn unroot(&self) { + #[allow(dead_code)] + #[inline] + unsafe fn mark(it: &T) { + ::gc::Trace::unroot(it); + } + match *self { #trace_body } } - match *self { #trace_body } - } - #[inline] fn finalize_glue(&self) { - #[allow(dead_code)] - #[inline] - fn mark(it: &T) { - ::gc::Trace::finalize_glue(it); + #[inline] fn finalize_glue(&self) { + #[allow(dead_code)] + #[inline] + fn mark(it: &T) { + ::gc::Trace::finalize_glue(it); + } + match *self { #trace_body } + ::gc::Finalize::finalize(self); } - match *self { #trace_body } - ::gc::Finalize::finalize(self); - } - }); + }, + ); // We also implement drop to prevent unsafe drop implementations on this // type and encourage people to use Finalize. This implementation will // call `Finalize::finalize` if it is safe to do so. - let drop_impl = s.unbound_impl(quote!(::std::ops::Drop), quote! { - fn drop(&mut self) { - if ::gc::finalizer_safe() { - ::gc::Finalize::finalize(self); + let drop_impl = s.unbound_impl( + quote!(::std::ops::Drop), + quote! { + fn drop(&mut self) { + if ::gc::finalizer_safe() { + ::gc::Finalize::finalize(self); + } } - } - }); + }, + ); quote! { #trace_impl