Skip to main content

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 {
9    pub use self::wrapper_impl_ast::*;
10
11    mod wrapper_impl_ast;
12}
13
14mod impls {
15    pub use self::{binary::impl_wrapper_binary_ops, unary::impl_wrapper_unary_ops};
16
17    pub mod binary;
18    pub mod unary;
19}
20
21use crate::ast::WrapperImpls;
22use proc_macro::TokenStream;
23use syn::parse_macro_input;
24
25/// The [`binary_wrapper!`] macro generates implementations for the core binary operations
26/// onto a generic wrapper type. It supports both tuple structs and structs with named fields.
27///
28/// ```rust
29/// extern crate contained_macros as macros;
30///
31/// pub struct Wrapper<T>(pub T);
32///
33/// macros::binary_wrapper! {
34///     impl Wrapper {
35///         Add.add,
36///         Sub.sub,
37///         Mul.mul,
38///         Div.div,
39///         Rem.rem,
40///     }
41/// }
42/// ```
43///
44/// or, for transparent structs with a named field:
45///
46/// ```rust
47/// extern crate contained_macros as macros;
48///
49/// pub struct Wrapper<T> {
50///     pub field: T,
51/// }
52///
53/// macros::binary_wrapper! {
54///     impl Wrapper.field {
55///         Add.add,
56///         Sub.sub,
57///         Mul.mul,
58///         Div.div,
59///         Rem.rem,
60///     }
61/// }
62/// ```
63#[proc_macro]
64pub fn binary_wrapper(input: TokenStream) -> TokenStream {
65    let ast = parse_macro_input!(input as WrapperImpls);
66    let output = impls::impl_wrapper_binary_ops(ast);
67    output.into()
68}
69
70/// The [`unary_wrapper!`] macro generates implementations for the core unary operations
71/// onto a generic wrapper type. It supports both tuple structs and structs with named fields.
72///
73/// ```rust
74/// extern crate contained_macros as macros;
75///
76/// pub struct Wrapper<T>(pub T);
77///
78/// macros::unary_wrapper! {
79///     impl Wrapper {
80///         Neg.neg,
81///         Not.not,
82///     }
83/// }
84/// ```
85///
86/// or, for transparent structs with a named field:
87///
88/// ```rust
89/// extern crate contained_macros as macros;
90///
91/// pub struct Wrapper<T> {
92///     pub field: T,
93/// }
94///
95/// macros::unary_wrapper! {
96///     impl Wrapper.field {
97///         Neg.neg,
98///         Not.not,
99///     }
100/// }
101/// ```
102#[proc_macro]
103pub fn unary_wrapper(input: TokenStream) -> TokenStream {
104    let ast = parse_macro_input!(input as WrapperImpls);
105    let output = impls::impl_wrapper_unary_ops(ast);
106    output.into()
107}