feldera-macros 0.372.0

Continuous streaming analytics engine
Documentation
//! Procedural macros for Feldera tuple types and utility traits.
//!
//! The `declare_tuple!` macro decides which layout to use based on tuple size
//! and the active storage format rules.

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

mod engine_test;
mod ord_repr;
mod tuples;

/// Registers a connector suite case of `dbsp_connectors`, a function of
/// `&dyn ConnectorTestEngine` or `&dyn PipelineTestEngine`, so that the test harness of
/// every engine runs it as one test.
///
/// The arguments, all optional: `serial` runs the case alone among the serial cases;
/// `ignore` or `ignore = "reason"` skips it unless ignored tests are asked for;
/// `ignore_if(predicate)` or `ignore_if(predicate, "reason")` skips it where the `cfg`
/// predicate holds; `should_panic = "text"` expects a panic whose message contains the
/// text; `requires(Capability, ...)` ignores it on an engine that lacks one of the
/// capabilities; `services(Service, ...)` names the services outside the test binary that it
/// uses, and ignores it when one does not answer, on an engine that asks for that; and
/// `cfg(predicate)` registers the case only where the predicate holds, while the function
/// still compiles everywhere.
#[proc_macro_attribute]
pub fn engine_test(args: TokenStream, item: TokenStream) -> TokenStream {
    engine_test::engine_test(args.into(), item.into())
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

/// Parses input of the form: `declare_tuple!(Tup1<T0>);`
/// and generates dbsp tuple structs.
#[proc_macro]
pub fn declare_tuple(input: TokenStream) -> TokenStream {
    let tuple = parse_macro_input!(input as tuples::TupleDef);
    let expanded = tuples::declare_tuple_impl(tuple);
    if std::env::var_os("FELDERA_DEV_MACROS_DUMP").is_some() {
        let parsed_file: syn::File = syn::parse2(expanded.clone()).expect("Failed to parse output");
        let formatted = prettyplease::unparse(&parsed_file);
        eprintln!("{}", formatted);
    }

    expanded.into()
}

/// Derives `OrdRepr` for the rkyv archived form of a struct or enum, so
/// that `ArchivedFoo` can be ordered against a `Foo` without deserializing.
/// It applies to types whose archived form comes from `#[derive(Archive)]`,
/// and therefore is named `ArchivedFoo` and mirrors `Foo`'s fields.
///
/// The generated comparison follows `#[derive(Ord)]`: struct fields compare
/// in declaration order, and enum variants compare by declaration position
/// before their payloads. It therefore agrees with `Ord` on the original
/// whenever `Ord` is derived. A type with a hand-written `Ord` must
/// hand-write `OrdRepr` to match it, and so must an enum with an explicit
/// discriminant, which `#[derive(Ord)]` orders by discriminant value rather
/// than by position; the derive rejects such an enum.
///
/// Every field type `T` gets the bound `<T as Archive>::Archived: OrdRepr<T>`,
/// in the same way that rkyv's derive bounds field types. A field marked
/// `#[omit_bounds]` (rkyv's attribute for recursive fields) contributes no
/// bound, which is what keeps a recursive type from requiring itself.
#[proc_macro_derive(OrdRepr)]
pub fn derive_ord_repr(item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as DeriveInput);
    match ord_repr::derive_ord_repr_impl(input) {
        Ok(expanded) => expanded.into(),
        Err(error) => error.to_compile_error().into(),
    }
}

#[proc_macro_derive(IsNone)]
pub fn derive_not_none(item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as DeriveInput);

    let ident = input.ident;
    let generics = input.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let expanded = quote! {
        impl #impl_generics ::dbsp::utils::IsNone for #ident #ty_generics #where_clause {
            type Inner = Self;

            #[inline]
            fn is_none(&self) -> bool {
                false
            }

            #[inline]
            fn unwrap_or_self(&self) -> &Self::Inner {
                self
            }

            #[inline]
            fn from_inner(inner: Self::Inner) -> Self {
                inner
            }
        }

        impl #impl_generics ::dbsp::utils::SupportsRoaring for #ident #ty_generics #where_clause {}
    };

    TokenStream::from(expanded)
}