Skip to content
File

Blob: src/rust/jsg-macros/trace.rs

rust30 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5//! Trace code generation for `#[jsg_resource]` structs.
6//!
7//! We intentionally do not inspect field types here. Every field is assumed to
8//! implement `jsg::Traced`, and the generated body simply delegates to
9//! `jsg::Traced::trace(&self.field, visitor)` for each named field.
10 
11/// Generates one trace delegation statement per named field.
12/// All fields are traced for safety by default, based on enforcing they implement
13/// the `Traced` trait, which all `GarbageCollected` types implement, and all non-GC
14/// types implement as a no-op.
15pub fn generate_trace_statements(
16 fields: &syn::punctuated::Punctuated<syn::Field, syn::token::Comma>,
17) -> Vec<quote::__private::TokenStream> {
18 use quote::quote;
19 
20 fields
21 .iter()
22 .filter_map(|field| {
23 let field_name = field.ident.as_ref()?;
24 Some(quote! {
25 jsg::Traced::trace(&self.#field_name, visitor);
26 })
27 })
28 .collect()
29}