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
8pub(crate) mod impl_binary;
9pub(crate) mod impl_unary;
10
11pub(crate) mod ast {
12 #[doc(inline)]
13 #[allow(unused_imports)]
14 pub use self::{ops::*, wrapper::*};
15
16 mod ops;
17 #[allow(dead_code)]
18 mod wrapper;
19}
20
21use crate::ast::WrapperOpsAst;
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 WrapperOpsAst);
66 let output = impl_binary::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 WrapperOpsAst);
105 let output = impl_unary::impl_wrapper_unary_ops(ast);
106 output.into()
107}
108
109
110#[deprecated(
111 since = "0.2.2",
112 note = "use `unary_wrapper` instead"
113)]
114#[proc_macro]
115pub fn impl_wrapper_unary(input: TokenStream) -> TokenStream {
116 let ast = parse_macro_input!(input as WrapperOpsAst);
117 let output = impl_unary::impl_wrapper_unary_ops(ast);
118 output.into()
119}