Skip to main content

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 engine_test;
11mod ord_repr;
12mod tuples;
13
14/// Registers a connector suite case of `dbsp_connectors`, a function of
15/// `&dyn ConnectorTestEngine` or `&dyn PipelineTestEngine`, so that the test harness of
16/// every engine runs it as one test.
17///
18/// The arguments, all optional: `serial` runs the case alone among the serial cases;
19/// `ignore` or `ignore = "reason"` skips it unless ignored tests are asked for;
20/// `ignore_if(predicate)` or `ignore_if(predicate, "reason")` skips it where the `cfg`
21/// predicate holds; `should_panic = "text"` expects a panic whose message contains the
22/// text; `requires(Capability, ...)` ignores it on an engine that lacks one of the
23/// capabilities; `services(Service, ...)` names the services outside the test binary that it
24/// uses, and ignores it when one does not answer, on an engine that asks for that; and
25/// `cfg(predicate)` registers the case only where the predicate holds, while the function
26/// still compiles everywhere.
27#[proc_macro_attribute]
28pub fn engine_test(args: TokenStream, item: TokenStream) -> TokenStream {
29    engine_test::engine_test(args.into(), item.into())
30        .unwrap_or_else(syn::Error::into_compile_error)
31        .into()
32}
33
34/// Parses input of the form: `declare_tuple!(Tup1<T0>);`
35/// and generates dbsp tuple structs.
36#[proc_macro]
37pub fn declare_tuple(input: TokenStream) -> TokenStream {
38    let tuple = parse_macro_input!(input as tuples::TupleDef);
39    let expanded = tuples::declare_tuple_impl(tuple);
40    if std::env::var_os("FELDERA_DEV_MACROS_DUMP").is_some() {
41        let parsed_file: syn::File = syn::parse2(expanded.clone()).expect("Failed to parse output");
42        let formatted = prettyplease::unparse(&parsed_file);
43        eprintln!("{}", formatted);
44    }
45
46    expanded.into()
47}
48
49/// Derives `OrdRepr` for the rkyv archived form of a struct or enum, so
50/// that `ArchivedFoo` can be ordered against a `Foo` without deserializing.
51/// It applies to types whose archived form comes from `#[derive(Archive)]`,
52/// and therefore is named `ArchivedFoo` and mirrors `Foo`'s fields.
53///
54/// The generated comparison follows `#[derive(Ord)]`: struct fields compare
55/// in declaration order, and enum variants compare by declaration position
56/// before their payloads. It therefore agrees with `Ord` on the original
57/// whenever `Ord` is derived. A type with a hand-written `Ord` must
58/// hand-write `OrdRepr` to match it, and so must an enum with an explicit
59/// discriminant, which `#[derive(Ord)]` orders by discriminant value rather
60/// than by position; the derive rejects such an enum.
61///
62/// Every field type `T` gets the bound `<T as Archive>::Archived: OrdRepr<T>`,
63/// in the same way that rkyv's derive bounds field types. A field marked
64/// `#[omit_bounds]` (rkyv's attribute for recursive fields) contributes no
65/// bound, which is what keeps a recursive type from requiring itself.
66#[proc_macro_derive(OrdRepr)]
67pub fn derive_ord_repr(item: TokenStream) -> TokenStream {
68    let input = parse_macro_input!(item as DeriveInput);
69    match ord_repr::derive_ord_repr_impl(input) {
70        Ok(expanded) => expanded.into(),
71        Err(error) => error.to_compile_error().into(),
72    }
73}
74
75#[proc_macro_derive(IsNone)]
76pub fn derive_not_none(item: TokenStream) -> TokenStream {
77    let input = parse_macro_input!(item as DeriveInput);
78
79    let ident = input.ident;
80    let generics = input.generics;
81    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
82
83    let expanded = quote! {
84        impl #impl_generics ::dbsp::utils::IsNone for #ident #ty_generics #where_clause {
85            type Inner = Self;
86
87            #[inline]
88            fn is_none(&self) -> bool {
89                false
90            }
91
92            #[inline]
93            fn unwrap_or_self(&self) -> &Self::Inner {
94                self
95            }
96
97            #[inline]
98            fn from_inner(inner: Self::Inner) -> Self {
99                inner
100            }
101        }
102
103        impl #impl_generics ::dbsp::utils::SupportsRoaring for #ident #ty_generics #where_clause {}
104    };
105
106    TokenStream::from(expanded)
107}