Skip to content

Commit bbb8d0b

Browse files
committed
Add #[derive(NullTrace)] as sugar for #[zerogc(nop_trace)]
This is nice :)
1 parent aee5449 commit bbb8d0b

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

libs/derive/src/lib.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,38 @@ pub fn unsafe_gc_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream
449449
res.into()
450450
}
451451

452+
#[proc_macro_derive(NullTrace, attributes(zerogc))]
453+
pub fn derive_null_trace(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
454+
let input = parse_macro_input!(input as DeriveInput);
455+
let mut info = match GcTypeInfo::parse(&input) {
456+
Ok(info) => info,
457+
Err(e) => return e.to_compile_error().into()
458+
};
459+
if info.config.nop_trace {
460+
return quote_spanned! { input.ident.span() =>
461+
compile_error!("derive(NullTrace): Can't explicitly specify #[zerogc(nop_trace)] as it's already implied")
462+
}.into();
463+
}
464+
info.config.nop_trace = true;
465+
let res = From::from(impl_derive_trace(&input, &info)
466+
.unwrap_or_else(|e| e.to_compile_error()));
467+
debug_derive(
468+
"derive(NullTrace)",
469+
&input.ident.to_string(),
470+
&format_args!("#[derive(NullTrace) for {}", input.ident),
471+
&res
472+
);
473+
res
474+
}
452475

453476
#[proc_macro_derive(Trace, attributes(zerogc))]
454477
pub fn derive_trace(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
455478
let input = parse_macro_input!(input as DeriveInput);
456-
let res = From::from(impl_derive_trace(&input)
479+
let info = match GcTypeInfo::parse(&input) {
480+
Ok(info) => info,
481+
Err(e) => return e.to_compile_error().into()
482+
};
483+
let res = From::from(impl_derive_trace(&input, &info)
457484
.unwrap_or_else(|e| e.to_compile_error()));
458485
debug_derive(
459486
"derive(Trace)",
@@ -464,8 +491,7 @@ pub fn derive_trace(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
464491
res
465492
}
466493

467-
fn impl_derive_trace(input: &DeriveInput) -> Result<TokenStream, syn::Error> {
468-
let info = GcTypeInfo::parse(input)?;
494+
fn impl_derive_trace(input: &DeriveInput, info: &GcTypeInfo) -> Result<TokenStream, syn::Error> {
469495
let trace_impl = if info.config.nop_trace {
470496
impl_nop_trace(&input, &info)?
471497
} else {

libs/derive/tests/basic.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use zerogc::{Gc, CollectorId, Trace, GcSafe, NullTrace, dummy_impl::{self, DummyCollectorId}};
22

3-
use zerogc_derive::Trace;
3+
use zerogc_derive::{Trace, NullTrace};
44

55
#[derive(Trace)]
66
#[zerogc(collector_id(DummyCollectorId))]
@@ -75,8 +75,7 @@ fn check_id<'gc, Id: CollectorId>() {
7575
assert!(<Basic<'gc, Id> as GcSafe>::NEEDS_DROP);
7676
}
7777

78-
#[derive(Trace)]
79-
#[zerogc(nop_trace)]
78+
#[derive(NullTrace)]
8079
#[allow(unused)]
8180
struct NopTrace {
8281
s: String,

0 commit comments

Comments
 (0)