rspace_macros/
lib.rs

1/*
2    Appellation: rspace-macros <library>
3    Contributors: FL03 <jo3mccain@icloud.com>
4*/
5//! procedural macros for interacting with various wrappers
6extern crate proc_macro;
7
8mod ast;
9mod impls;
10
11use crate::ast::WrapperOpsAst;
12use proc_macro::TokenStream;
13use syn::parse_macro_input;
14
15/// The [`binary_wrapper!`] macro generates implementations for the core binary operations
16/// onto a generic wrapper type. It supports both tuple structs and structs with named fields.
17///
18/// ```rust
19/// extern crate rspace_macros as macros;
20///
21/// pub struct Wrapper<T>(pub T);
22///
23/// macros::binary_wrapper! {
24///     impl Wrapper {
25///         Add.add,
26///         Sub.sub,
27///         Mul.mul,
28///         Div.div,
29///         Rem.rem,
30///     }
31/// }
32/// ```
33///
34/// or, for transparent structs with a named field:
35///
36/// ```rust
37/// extern crate rspace_macros as macros;
38///
39/// pub struct Wrapper<T> {
40///     pub field: T,
41/// }
42///
43/// macros::binary_wrapper! {
44///     impl Wrapper.field {
45///         Add.add,
46///         Sub.sub,
47///         Mul.mul,
48///         Div.div,
49///         Rem.rem,
50///     }
51/// }
52/// ```
53#[proc_macro]
54pub fn binary_wrapper(input: TokenStream) -> TokenStream {
55    let ast = parse_macro_input!(input as WrapperOpsAst);
56    let output = impls::impl_wrapper_binary_ops(ast);
57    output.into()
58}