feldera_macros/lib.rs
1//! Procedural macros for Feldera tuple types and utility traits.
2//!
3//! The `declare_tuple!` macro decides which layout to use based on tuple size
4//! and the active storage format rules.
5
6use proc_macro::TokenStream;
7use quote::quote;
8use syn::{parse_macro_input, DeriveInput};
9
10mod ord_repr;
11mod tuples;
12
13/// Parses input of the form: `declare_tuple!(Tup1<T0>);`
14/// and generates dbsp tuple structs.
15#[proc_macro]
16pub fn declare_tuple(input: TokenStream) -> TokenStream {
17 let tuple = parse_macro_input!(input as tuples::TupleDef);
18 let expanded = tuples::declare_tuple_impl(tuple);
19 if std::env::var_os("FELDERA_DEV_MACROS_DUMP").is_some() {
20 let parsed_file: syn::File = syn::parse2(expanded.clone()).expect("Failed to parse output");
21 let formatted = prettyplease::unparse(&parsed_file);
22 eprintln!("{}", formatted);
23 }
24
25 expanded.into()
26}
27
28/// Derives `OrdRepr` for the rkyv archived form of a struct or enum, so
29/// that `ArchivedFoo` can be ordered against a `Foo` without deserializing.
30/// It applies to types whose archived form comes from `#[derive(Archive)]`,
31/// and therefore is named `ArchivedFoo` and mirrors `Foo`'s fields.
32///
33/// The generated comparison follows `#[derive(Ord)]`: struct fields compare
34/// in declaration order, and enum variants compare by declaration position
35/// before their payloads. It therefore agrees with `Ord` on the original
36/// whenever `Ord` is derived. A type with a hand-written `Ord` must
37/// hand-write `OrdRepr` to match it, and so must an enum with an explicit
38/// discriminant, which `#[derive(Ord)]` orders by discriminant value rather
39/// than by position; the derive rejects such an enum.
40///
41/// Every field type `T` gets the bound `<T as Archive>::Archived: OrdRepr<T>`,
42/// in the same way that rkyv's derive bounds field types. A field marked
43/// `#[omit_bounds]` (rkyv's attribute for recursive fields) contributes no
44/// bound, which is what keeps a recursive type from requiring itself.
45#[proc_macro_derive(OrdRepr)]
46pub fn derive_ord_repr(item: TokenStream) -> TokenStream {
47 let input = parse_macro_input!(item as DeriveInput);
48 match ord_repr::derive_ord_repr_impl(input) {
49 Ok(expanded) => expanded.into(),
50 Err(error) => error.to_compile_error().into(),
51 }
52}
53
54#[proc_macro_derive(IsNone)]
55pub fn derive_not_none(item: TokenStream) -> TokenStream {
56 let input = parse_macro_input!(item as DeriveInput);
57
58 let ident = input.ident;
59 let generics = input.generics;
60 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
61
62 let expanded = quote! {
63 impl #impl_generics ::dbsp::utils::IsNone for #ident #ty_generics #where_clause {
64 type Inner = Self;
65
66 #[inline]
67 fn is_none(&self) -> bool {
68 false
69 }
70
71 #[inline]
72 fn unwrap_or_self(&self) -> &Self::Inner {
73 self
74 }
75
76 #[inline]
77 fn from_inner(inner: Self::Inner) -> Self {
78 inner
79 }
80 }
81
82 impl #impl_generics ::dbsp::utils::SupportsRoaring for #ident #ty_generics #where_clause {}
83 };
84
85 TokenStream::from(expanded)
86}