rama icon indicating copy to clipboard operation
rama copied to clipboard

Track potential use of eval macro

Open soundofspace opened this issue 9 months ago • 1 comments

Eval macro is a really cool tool that is situated somewhere between macro_rules and procedural macros. It's very easy to read and quite powerful and could help us a lot with boilerplate plate code / repetions.

But replacing this with eval macros brings in a new dependency in the core and this also makes it a liability. For now I only played with using it to generate the Impl blocks for tuple layers. I'm creating this issue so we can list potential other use cases and up/downs to use this. Once we know more we can decide on using this tool or not.

Example of how it could drastically simplify rama-core/src/layer/mod.rs from 800 lines to 150 lines, while still being very readable and also much easier to extend/update if needed.

use eval_macro::eval;

eval! {
    for i in 1..=25{
        let generic_layers = (1..=i)
            .map(|x| format!("L{}", x))
            .collect::<Vec<_>>()
            .join(",");

        let bounds = (1..=i)
            .map(|x| {
                if x==i {
                    format!("L{}: Layer<S>", x)
                } else {
                    format!("L{}: Layer<L{}::Service>", x, x+1)
                }
            })
            .collect::<Vec<_>>()
            .join(",");

        let layers_except_first = (2..=i)
            .map(|x| format!("l{}", x))
            .collect::<Vec<_>>()
            .join(",");

        output! {
            impl<S, {{generic_layers}}> Layer<S> for ({{generic_layers}},)
            where
                {{bounds}},
            {
                type Service = L1::Service;

                fn layer(&self, service: S) -> Self::Service {
                    let (l1, {{layers_except_first}}) = self;
                    l1.layer(({{layers_except_first}}).layer(service))
                }
            }
        }
    }
}

soundofspace avatar Mar 17 '25 07:03 soundofspace

Love it. Well summarized. As we continue to collect use cases we can decide when, if ever, it might be the right time to take the dive.

Note also that in contrast to proc macros and macro rules, these third party eval macros not are re-usable. So it will only ever be something that benefits us as the maintainers.

GlenDC avatar Mar 17 '25 08:03 GlenDC