contained_macros/
lib.rs

1/*
2    Appellation: contained-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::WrapperImpls;
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 contained_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 contained_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 WrapperImpls);
56    let output = impls::impl_wrapper_binary_ops(ast);
57    output.into()
58}
59
60/// The [`unary_wrapper!`] macro generates implementations for the core unary operations
61/// onto a generic wrapper type. It supports both tuple structs and structs with named fields.
62///
63/// ```rust
64/// extern crate contained_macros as macros;
65///
66/// pub struct Wrapper<T>(pub T);
67///
68/// macros::unary_wrapper! {
69///     impl Wrapper {
70///         Neg.neg,
71///         Not.not,
72///     }
73/// }
74/// ```
75///
76/// or, for transparent structs with a named field:
77///
78/// ```rust
79/// extern crate contained_macros as macros;
80///
81/// pub struct Wrapper<T> {
82///     pub field: T,
83/// }
84///
85/// macros::unary_wrapper! {
86///     impl Wrapper.field {
87///         Neg.neg,
88///         Not.not,
89///     }
90/// }
91/// ```
92#[proc_macro]
93pub fn unary_wrapper(input: TokenStream) -> TokenStream {
94    let ast = parse_macro_input!(input as WrapperImpls);
95    let output = impls::impl_wrapper_unary_ops(ast);
96    output.into()
97}