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

generic arenas #26

Closed
wants to merge 7 commits into from
Closed
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
8 changes: 2 additions & 6 deletions src/gc-arena/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,14 @@ impl ArenaParameters {
/// pointers and manually ensuring that invariants are held.
#[macro_export]
macro_rules! make_arena {
($arena:ident, $root:ident) => {
make_arena!(pub(self) $arena, $root);
};

($vis:vis $arena:ident, $root:ident) => {
($vis:vis $arena:ident $(( $($arena_gen:tt)* ))?, $root:ident $(( $($root_gen:tt)* ))?) => {
// Instead of generating an impl of `RootProvider`, we use a trait object.
// The projection `<R as RootProvider<'gc>>::Root` is used to obtain the root
// type with the lifetime `'gc` applied
// By using a trait object, we avoid the need to generate a new type for each
// invocation of this macro, which would lead to name conflicts if the macro was
// used multiple times in the same scope.
$vis type $arena = $crate::Arena<dyn for<'a> $crate::RootProvider<'a, Root = $root<'a>>>;
$vis type $arena $( <$($arena_gen)*> )? = $crate::Arena<dyn for<'gc> $crate::RootProvider<'gc, Root = $root<'gc, $( $($root_gen)* )?>>>;
};
}

Expand Down
21 changes: 21 additions & 0 deletions src/gc-arena/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,27 @@ fn derive_collect() {
assert_eq!(Test8::needs_trace(), false);
}

#[test]
fn generic_make_arena() {
#[derive(Collect)]
#[collect(no_drop)]
struct Test<'gc, T: 'gc + Collect, const N: usize, U: 'gc + Collect>(Gc<'gc, T>, [(); N], U);
make_arena!(TestArena(T, const N: usize, U), Test((T, T), N, Gc<'gc, U>));

fn test<const N: usize, T: 'static + Collect + Copy, U: 'static + Collect + Copy>(
v: T,
x: U,
) -> ((T, T), usize, U) {
let arena = TestArena::<T, N, U>::new(Default::default(), |mc| {
Test(Gc::allocate(mc, (v, v)), [(); N], Gc::allocate(mc, x))
});
arena.mutate(|_, arena| (*arena.0, arena.1.len(), *arena.2))
}

assert_eq!(test::<12, _, _>(34, 12i8), ((34, 34), 12, 12i8));
assert_eq!(test::<7, _, _>(false, -6i64), ((false, false), 7, -6i64));
}

#[test]
fn ui() {
let t = trybuild::TestCases::new();
Expand Down