lib.rs

cdk-rsx/crates/cdk-rsx-engine-nyxus-derive/src/lib.rs

1//! A recursive data-only whitelist derive. Physical layout certification is a2//! separate build tool: this macro does not infer ABI stability from field types.3use proc_macro::TokenStream;4use quote::quote;5use syn::{Data, DeriveInput, GenericParam, parse_macro_input, parse_quote};67/// Fingerprint the actual derive package selected by Cargo.8#[doc(hidden)]9#[proc_macro]10pub fn __package_policy(input: TokenStream) -> TokenStream {11    assert!(input.is_empty(), "package policy takes no arguments");12    cdk_rsx_compat::emit_package_recipe(13        std::path::Path::new(env!("CARGO_MANIFEST_DIR")),14        env!("CARGO_PKG_NAME"),15        include_str!("../Cargo.toml"),16        include_bytes!("lib.rs"),17    )18}1920#[proc_macro_derive(NyxusData)]21pub fn nyxus_data(input: TokenStream) -> TokenStream {22    let input = parse_macro_input!(input as DeriveInput);23    match derive(input) {24        Ok(tokens) => tokens.into(),25        Err(error) => error.to_compile_error().into(),26    }27}28fn derive(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream> {29    let declaration = quote!(#input);30    let name = input.ident;31    let fields = match input.data {32        Data::Struct(data) => data.fields.into_iter().map(|f| f.ty).collect::<Vec<_>>(),33        Data::Enum(data) => data34            .variants35            .into_iter()36            .flat_map(|v| v.fields)37            .map(|f| f.ty)38            .collect(),39        Data::Union(data) => {40            return Err(syn::Error::new_spanned(41                data.union_token,42                "NyxusData does not admit unions",43            ));44        }45    };46    let mut generics = input.generics;47    for parameter in &mut generics.params {48        match parameter {49            GenericParam::Type(parameter) => parameter50                .bounds51                .push(parse_quote!(::cdk_rsx_engine_nyxus::NyxusData)),52            GenericParam::Lifetime(parameter) => {53                return Err(syn::Error::new_spanned(54                    parameter,55                    "NyxusData requires owned data; lifetime parameters are unsupported",56                ));57            }58            GenericParam::Const(_) => {}59        }60    }61    let (implementation, types, predicates) = generics.split_for_impl();62    // The const body is type-checked even if never evaluated. Keep checks out63    // of impl predicates to support recursive Rc<Self> without cyclic bounds.64    Ok(quote! {65        // SAFETY: every field obligation is checked in the const body. The66        // derive introduces no hidden allocations, aliases or callbacks.67        unsafe impl #implementation ::cdk_rsx_engine_nyxus::NyxusData for #name #types #predicates {68            const __NYXUS_POLICY: [u8; 32] = ::cdk_rsx_engine_nyxus::declaration_policy!(#declaration);69            const __NYXUS_DATA: () = {70                fn admitted<T: ::cdk_rsx_engine_nyxus::NyxusData + ?Sized>() {}71                #(let _ = admitted::<#fields>;)*72            };73        }74    })75}