Skip to content

Commit

Permalink
Migrated to 2018 edition, and fixed a bunch of Clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Apr 18, 2020
1 parent 80e35cd commit c9d2e9d
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 78 deletions.
1 change: 1 addition & 0 deletions gc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ repository = "https://github.com/Manishearth/rust-gc"
readme = "../README.md"
license = "MPL-2.0"
keywords = ["garbage", "plugin", "memory"]
edition = "2018"

[features]
nightly = []
Expand Down
1 change: 0 additions & 1 deletion gc/benches/alloc_in_a_loop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(test)]

extern crate test;
extern crate gc;

const THING: u64 = 0;

Expand Down
32 changes: 16 additions & 16 deletions gc/src/gc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cell::{Cell, RefCell};
use std::mem;
use std::ptr::NonNull;
use trace::{Finalize, Trace};
use crate::trace::{Finalize, Trace};


const INITIAL_THRESHOLD: usize = 100;
Expand All @@ -15,7 +15,7 @@ const USED_SPACE_RATIO: f64 = 0.7;
struct GcState {
bytes_allocated: usize,
threshold: usize,
boxes_start: Option<NonNull<GcBox<Trace>>>,
boxes_start: Option<NonNull<GcBox<dyn Trace>>>,
}

impl Drop for GcState {
Expand All @@ -38,8 +38,8 @@ impl Drop for GcState {
}
}

/// Whether or not the thread is currently in the sweep phase of garbage collection.
/// During this phase, attempts to dereference a `Gc<T>` pointer will trigger a panic.
// Whether or not the thread is currently in the sweep phase of garbage collection.
// During this phase, attempts to dereference a `Gc<T>` pointer will trigger a panic.
thread_local!(pub static GC_DROPPING: Cell<bool> = Cell::new(false));
struct DropGuard;
impl DropGuard {
Expand All @@ -57,23 +57,23 @@ pub fn finalizer_safe() -> bool {
GC_DROPPING.with(|dropping| !dropping.get())
}

/// The garbage collector's internal state.
// The garbage collector's internal state.
thread_local!(static GC_STATE: RefCell<GcState> = RefCell::new(GcState {
bytes_allocated: 0,
threshold: INITIAL_THRESHOLD,
boxes_start: None,
}));

pub struct GcBoxHeader {
pub(crate) struct GcBoxHeader {
// XXX This is horribly space inefficient - not sure if we care
// We are using a word word bool - there is a full 63 bits of unused data :(
// XXX: Should be able to store marked in the high bit of roots?
roots: Cell<usize>,
next: Option<NonNull<GcBox<Trace>>>,
next: Option<NonNull<GcBox<dyn Trace>>>,
marked: Cell<bool>,
}

pub struct GcBox<T: Trace + ?Sized + 'static> {
pub(crate) struct GcBox<T: Trace + ?Sized + 'static> {
header: GcBoxHeader,
data: T,
}
Expand All @@ -83,7 +83,7 @@ impl<T: Trace> GcBox<T> {
/// and appends it to the thread-local `GcBox` chain.
///
/// A `GcBox` allocated this way starts its life rooted.
pub fn new(value: T) -> NonNull<Self> {
pub(crate) fn new(value: T) -> NonNull<Self> {
GC_STATE.with(|st| {
let mut st = st.borrow_mut();

Expand Down Expand Up @@ -121,7 +121,7 @@ impl<T: Trace> GcBox<T> {

impl<T: Trace + ?Sized> GcBox<T> {
/// Marks this `GcBox` and marks through its data.
pub unsafe fn trace_inner(&self) {
pub(crate) unsafe fn trace_inner(&self) {
let marked = self.header.marked.get();
if !marked {
self.header.marked.set(true);
Expand All @@ -131,31 +131,31 @@ impl<T: Trace + ?Sized> GcBox<T> {

/// Increases the root count on this `GcBox`.
/// Roots prevent the `GcBox` from being destroyed by the garbage collector.
pub unsafe fn root_inner(&self) {
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());
}

/// Decreases the root count on this `GcBox`.
/// Roots prevent the `GcBox` from being destroyed by the garbage collector.
pub unsafe fn unroot_inner(&self) {
pub(crate) unsafe fn unroot_inner(&self) {
self.header.roots.set(self.header.roots.get() - 1);
}

/// Returns a reference to the `GcBox`'s value.
pub fn value(&self) -> &T {
pub(crate) fn value(&self) -> &T {
&self.data
}
}

/// Collects garbage.
fn collect_garbage(st: &mut GcState) {
struct Unmarked {
incoming: *mut Option<NonNull<GcBox<Trace>>>,
this: NonNull<GcBox<Trace>>,
incoming: *mut Option<NonNull<GcBox<dyn Trace>>>,
this: NonNull<GcBox<dyn Trace>>,
}
unsafe fn mark(head: &mut Option<NonNull<GcBox<Trace>>>) -> Vec<Unmarked> {
unsafe fn mark(head: &mut Option<NonNull<GcBox<dyn Trace>>>) -> Vec<Unmarked> {
// Walk the tree, tracing and marking the nodes
let mut mark_head = *head;
while let Some(node) = mark_head {
Expand Down
32 changes: 11 additions & 21 deletions gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
feature(coerce_unsized, optin_builtin_traits, unsize, specialization)
)]

use gc::GcBox;
use crate::gc::GcBox;
use std::alloc::Layout;
use std::cell::{Cell, UnsafeCell};
use std::cmp::Ordering;
Expand All @@ -31,8 +31,8 @@ mod trace;

// We re-export the Trace method, as well as some useful internal methods for
// managing collections or configuring the garbage collector.
pub use gc::{finalizer_safe, force_collect};
pub use trace::{Finalize, Trace};
pub use crate::gc::{finalizer_safe, force_collect};
pub use crate::trace::{Finalize, Trace};

////////
// Gc //
Expand Down Expand Up @@ -283,11 +283,6 @@ impl<T: Trace + ?Sized + PartialEq> PartialEq for Gc<T> {
fn eq(&self, other: &Self) -> bool {
**self == **other
}

#[inline(always)]
fn ne(&self, other: &Self) -> bool {
**self != **other
}
}

impl<T: Trace + ?Sized + Eq> Eq for Gc<T> {}
Expand Down Expand Up @@ -333,19 +328,19 @@ impl<T: Trace + ?Sized + Hash> Hash for Gc<T> {
}

impl<T: Trace + ?Sized + Display> Display for Gc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&**self, f)
}
}

impl<T: Trace + ?Sized + Debug> Debug for Gc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&**self, f)
}
}

impl<T: Trace> fmt::Pointer for Gc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.inner(), f)
}
}
Expand Down Expand Up @@ -473,7 +468,7 @@ impl<T: Trace + ?Sized> GcCell<T> {
///
/// Panics if the value is currently mutably borrowed.
#[inline]
pub fn borrow(&self) -> GcCellRef<T> {
pub fn borrow(&self) -> GcCellRef<'_, T> {
if self.flags.get().borrowed() == BorrowState::Writing {
panic!("GcCell<T> already mutably borrowed");
}
Expand All @@ -500,7 +495,7 @@ impl<T: Trace + ?Sized> GcCell<T> {
///
/// Panics if the value is currently borrowed.
#[inline]
pub fn borrow_mut(&self) -> GcCellRefMut<T> {
pub fn borrow_mut(&self) -> GcCellRefMut<'_, T> {
if self.flags.get().borrowed() != BorrowState::Unused {
panic!("GcCell<T> already borrowed");
}
Expand Down Expand Up @@ -587,7 +582,7 @@ impl<'a, T: Trace + ?Sized> Drop for GcCellRef<'a, T> {
}

impl<'a, T: Trace + ?Sized + Debug> Debug for GcCellRef<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&**self, f)
}
}
Expand Down Expand Up @@ -630,7 +625,7 @@ impl<'a, T: Trace + ?Sized> Drop for GcCellRefMut<'a, T> {
}

impl<'a, T: Trace + ?Sized + Debug> Debug for GcCellRefMut<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&*(self.deref()), f)
}
}
Expand All @@ -656,11 +651,6 @@ impl<T: Trace + ?Sized + PartialEq> PartialEq for GcCell<T> {
fn eq(&self, other: &Self) -> bool {
*self.borrow() == *other.borrow()
}

#[inline(always)]
fn ne(&self, other: &Self) -> bool {
*self.borrow() != *other.borrow()
}
}

impl<T: Trace + ?Sized + Eq> Eq for GcCell<T> {}
Expand Down Expand Up @@ -700,7 +690,7 @@ impl<T: Trace + ?Sized + Ord> Ord for GcCell<T> {
}

impl<T: Trace + ?Sized + Debug> Debug for GcCell<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.flags.get().borrowed() {
BorrowState::Unused | BorrowState::Reading => f
.debug_struct("GcCell")
Expand Down
12 changes: 6 additions & 6 deletions gc/src/trace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::{BinaryHeap, BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
use std::hash::Hash;
use std::hash::{Hash, BuildHasher};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize};

Expand Down Expand Up @@ -248,7 +248,7 @@ unsafe impl<T: Trace, E: Trace> Trace for Result<T, E> {
impl<T: Ord + Trace> Finalize for BinaryHeap<T> {}
unsafe impl<T: Ord + Trace> Trace for BinaryHeap<T> {
custom_trace!(this, {
for v in this.into_iter() {
for v in this.iter() {
mark(v);
}
});
Expand All @@ -273,8 +273,8 @@ unsafe impl<T: Trace> Trace for BTreeSet<T> {
});
}

impl<K: Eq + Hash + Trace, V: Trace> Finalize for HashMap<K, V> {}
unsafe impl<K: Eq + Hash + Trace, V: Trace> Trace for HashMap<K, V> {
impl<K: Eq + Hash + Trace, V: Trace, S: BuildHasher> Finalize for HashMap<K, V, S> {}
unsafe impl<K: Eq + Hash + Trace, V: Trace, S: BuildHasher> Trace for HashMap<K, V, S> {
custom_trace!(this, {
for (k, v) in this.iter() {
mark(k);
Expand All @@ -283,8 +283,8 @@ unsafe impl<K: Eq + Hash + Trace, V: Trace> Trace for HashMap<K, V> {
});
}

impl<T: Eq + Hash + Trace> Finalize for HashSet<T> {}
unsafe impl<T: Eq + Hash + Trace> Trace for HashSet<T> {
impl<T: Eq + Hash + Trace, S: BuildHasher> Finalize for HashSet<T, S> {}
unsafe impl<T: Eq + Hash + Trace, S: BuildHasher> Trace for HashSet<T, S> {
custom_trace!(this, {
for v in this.iter() {
mark(v);
Expand Down
5 changes: 1 addition & 4 deletions gc/tests/finalize.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#![cfg_attr(feature = "nightly", feature(specialization))]

#[macro_use]
extern crate gc_derive;
extern crate gc;

use gc::{Finalize, Trace};
use gc_derive::{Trace, Finalize};
use std::cell::Cell;

#[derive(PartialEq, Eq, Debug, Clone, Copy)]
Expand Down
15 changes: 6 additions & 9 deletions gc/tests/gc_semantics.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#![cfg_attr(feature = "nightly", feature(specialization))]

#[macro_use]
extern crate gc_derive;
extern crate gc;

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)]
Expand All @@ -21,11 +18,11 @@ struct GcWatchFlags {
impl GcWatchFlags {
fn new(trace: i32, root: i32, unroot: i32, drop: i32, finalize: i32) -> GcWatchFlags {
GcWatchFlags {
trace: trace,
root: root,
unroot: unroot,
drop: drop,
finalize: finalize,
trace,
root,
unroot,
drop,
finalize,
}
}

Expand Down
5 changes: 1 addition & 4 deletions gc/tests/gymnastics_cycle.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#![cfg_attr(feature = "nightly", feature(specialization))]

#[macro_use]
extern crate gc_derive;
extern crate gc;

use std::cell::Cell;
use gc::{GcCell, Gc, force_collect};
use gc_derive::Trace;

thread_local!(static COUNTER: Cell<u8> = Cell::new(0u8));

Expand Down
4 changes: 0 additions & 4 deletions gc/tests/i128.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#![cfg_attr(feature = "nightly", feature(i128_type))]

extern crate gc;

use gc::Gc;

#[test]
Expand Down
6 changes: 2 additions & 4 deletions gc/tests/trace_impl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![cfg_attr(feature = "nightly", feature(specialization))]

#[macro_use]
extern crate gc_derive;
extern crate gc;
use gc_derive::{Trace, Finalize};
use std::cell::RefCell;

thread_local!(static X: RefCell<u8> = RefCell::new(0));
Expand All @@ -16,7 +14,7 @@ unsafe impl Trace for Foo {
unsafe fn trace(&self) {
X.with(|x| {
let mut m = x.borrow_mut();
*m = *m + 1;
*m += 1;
})
}
unsafe fn root(&self) {}
Expand Down
1 change: 1 addition & 0 deletions gc_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository = "https://github.com/Manishearth/rust-gc"
readme = "../README.md"
license = "MPL-2.0"
keywords = ["garbage", "macro", "memory"]
edition = "2018"

[lib]
name = "gc_derive"
Expand Down
13 changes: 4 additions & 9 deletions gc_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate synstructure;
#[macro_use]
extern crate quote;
extern crate proc_macro2;
use synstructure::{decl_derive, Structure};
use quote::quote;

decl_derive!([Trace, attributes(unsafe_ignore_trace)] => derive_trace);

fn derive_trace(mut s: synstructure::Structure) -> proc_macro2::TokenStream {
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")));
let trace_body = s.each(|bi| quote!(mark(#bi)));

Expand Down Expand Up @@ -67,6 +62,6 @@ fn derive_trace(mut s: synstructure::Structure) -> proc_macro2::TokenStream {

decl_derive!([Finalize] => derive_finalize);

fn derive_finalize(s: synstructure::Structure) -> proc_macro2::TokenStream {
fn derive_finalize(s: Structure<'_>) -> proc_macro2::TokenStream {
s.unbound_impl(quote!(::gc::Finalize), quote!())
}

0 comments on commit c9d2e9d

Please sign in to comment.