Skip to main content

derive_tools_meta/
lib.rs

1#![doc(html_logo_url = "https://raw.githubusercontent.com/Wandalen/wTools/master/asset/img/logo_v3_3_black.png")]
2#![doc(html_favicon_url = "https://raw.githubusercontent.com/Wandalen/wTools/master/asset/img/logo_v3_3_black.png")]
3#![doc(html_root_url = "https://docs.rs/derive_tools_meta/latest/derive_tools_meta/")]
4#![deny(rust_2018_idioms)]
5#![deny(future_incompatible)]
6#![deny(missing_debug_implementations)]
7#![deny(missing_docs)]
8#![deny(unsafe_code)]
9#![allow(clippy::upper_case_acronyms)]
10#![warn(clippy::unwrap_used)]
11#![warn(clippy::default_trait_access)]
12#![warn(clippy::wildcard_imports)]
13
14//!
15//! Collection of derive macros for `derive_tools`.
16//!
17
18mod derive;
19
20///
21/// Implement `AsMut` for a structure.
22///
23/// ### Sample.
24///
25/// ```text
26/// use derive_tools::AsMut;
27///
28/// #[ derive( AsMut ) ]
29/// struct MyStruct
30/// {
31///   #[ as_mut ]
32///   a: i32,
33///   b: i32,
34/// }
35///
36/// let mut my_struct = MyStruct { a: 1, b: 2 };
37/// *my_struct.as_mut() += 1;
38/// dbg!( my_struct.a );
39/// ```
40///
41/// To learn more about the feature, study the module [`derive_tools::AsMut`](https://docs.rs/derive_tools/latest/derive_tools/as_mut/index.html).
42///
43#[ proc_macro_derive(AsMut, attributes(as_mut)) ]
44pub fn as_mut(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
45{
46  derive::as_mut::as_mut(input)
47  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
48  .into()
49}
50
51///
52/// Implement `AsRef` for a structure.
53///
54/// ### Sample.
55///
56/// ```text
57/// use derive_tools::AsRef;
58///
59/// #[ derive( AsRef ) ]
60/// struct MyStruct
61/// {
62///   #[ as_ref ]
63///   a: i32,
64///   b: i32,
65/// }
66///
67/// let my_struct = MyStruct { a: 1, b: 2 };
68/// dbg!( my_struct.as_ref() );
69/// ```
70///
71/// To learn more about the feature, study the module [`derive_tools::AsRef`](https://docs.rs/derive_tools/latest/derive_tools/as_ref/index.html).
72///
73#[ proc_macro_derive(AsRef, attributes(as_ref)) ]
74pub fn as_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
75{
76  derive::as_ref::as_ref(input)
77  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
78  .into()
79}
80
81///
82/// Implement `Deref` for a structure.
83///
84/// ### Sample.
85///
86/// ```text
87/// use derive_tools::Deref;
88///
89/// #[ derive( Deref ) ]
90/// struct MyStruct
91/// {
92///   #[ deref ]
93///   a: i32,
94///   b: i32,
95/// }
96///
97/// let my_struct = MyStruct { a: 1, b: 2 };
98/// dbg!( *my_struct );
99/// ```
100///
101/// To learn more about the feature, study the module [`derive_tools::Deref`](https://docs.rs/derive_tools/latest/derive_tools/deref/index.html).
102///
103#[ proc_macro_derive(Deref, attributes(deref, debug)) ]
104pub fn deref(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
105{
106  derive::deref::deref(input)
107  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
108  .into()
109}
110
111///
112/// Implement `DerefMut` for a structure.
113///
114/// ### Sample.
115///
116/// ```text
117/// use derive_tools::DerefMut;
118///
119/// #[ derive( DerefMut ) ]
120/// struct MyStruct
121/// {
122///   #[ deref_mut ]
123///   a: i32,
124///   b: i32,
125/// }
126///
127/// let mut my_struct = MyStruct { a: 1, b: 2 };
128/// *my_struct += 1;
129/// dbg!( my_struct.a );
130/// ```
131///
132/// To learn more about the feature, study the module [`derive_tools::DerefMut`](https://docs.rs/derive_tools/latest/derive_tools/deref_mut/index.html).
133///
134#[ proc_macro_derive(DerefMut, attributes(deref_mut)) ]
135pub fn deref_mut(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
136{
137  derive::deref_mut::deref_mut(input)
138  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
139  .into()
140}
141
142///
143/// Implement `From` for a structure.
144///
145/// ### Sample.
146///
147/// ```text
148/// use derive_tools::From;
149///
150/// #[ derive( From ) ]
151/// struct MyStruct( i32 );
152///
153/// let my_struct = MyStruct::from( 13 );
154/// dbg!( my_struct.0 );
155/// ```
156///
157/// To learn more about the feature, study the module [`derive_tools::From`](https://docs.rs/derive_tools/latest/derive_tools/from/index.html).
158///
159#[ proc_macro_derive(From, attributes(from)) ]
160pub fn from(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
161{
162  derive::from::from(input)
163  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
164  .into()
165}
166
167///
168/// Implement `Index` for a structure.
169///
170/// ### Sample.
171///
172/// ```text
173/// use derive_tools::Index;
174///
175/// #[ derive( Index ) ]
176/// struct MyStruct( i32 );
177///
178/// let my_struct = MyStruct( 13 );
179/// dbg!( my_struct[ 0 ] );
180/// ```
181///
182/// To learn more about the feature, study the module [`derive_tools::Index`](https://docs.rs/derive_tools/latest/derive_tools/index/index.html).
183///
184#[ proc_macro_derive(Index, attributes(index)) ]
185pub fn index(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
186{
187  derive::index::index(input)
188  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
189  .into()
190}
191
192///
193/// Implement `IndexMut` for a structure.
194///
195/// ### Sample.
196///
197/// ```text
198/// use derive_tools::IndexMut;
199///
200/// #[ derive( IndexMut ) ]
201/// struct MyStruct( i32 );
202///
203/// let mut my_struct = MyStruct( 13 );
204/// my_struct[ 0 ] += 1;
205/// dbg!( my_struct.0 );
206/// ```
207///
208/// To learn more about the feature, study the module [`derive_tools::IndexMut`](https://docs.rs/derive_tools/latest/derive_tools/index_mut/index.html).
209///
210#[ proc_macro_derive(IndexMut, attributes(index_mut)) ]
211pub fn index_mut(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
212{
213  derive::index_mut::index_mut(input)
214  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
215  .into()
216}
217
218///
219/// Implement `InnerFrom` for a structure.
220///
221/// ### Sample.
222///
223/// ```text
224/// use derive_tools::InnerFrom;
225///
226/// #[ derive( InnerFrom ) ]
227/// struct MyStruct( i32 );
228///
229/// let my_struct = MyStruct::inner_from( 13 );
230/// dbg!( my_struct.0 );
231/// ```
232///
233/// To learn more about the feature, study the module [`derive_tools::InnerFrom`](https://docs.rs/derive_tools/latest/derive_tools/inner_from/index.html).
234///
235#[ proc_macro_derive(InnerFrom, attributes(inner_from)) ]
236pub fn inner_from(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
237{
238  derive::inner_from::inner_from(input)
239  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
240  .into()
241}
242
243///
244/// Implement `New` for a structure.
245///
246/// ### Sample.
247///
248/// ```text
249/// use derive_tools::New;
250///
251/// #[ derive( New ) ]
252/// struct MyStruct;
253///
254/// let my_struct = MyStruct::new();
255/// dbg!( my_struct );
256/// ```
257///
258/// To learn more about the feature, study the module [`derive_tools::New`](https://docs.rs/derive_tools/latest/derive_tools/new/index.html).
259///
260#[ proc_macro_derive(New, attributes(new)) ]
261pub fn new(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
262{
263  derive::new::new(input)
264  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
265  .into()
266}
267
268///
269/// Implement `Not` for a structure.
270///
271/// ### Sample.
272///
273/// ```text
274/// use derive_tools::Not;
275///
276/// #[ derive( Not ) ]
277/// struct MyStruct( bool );
278///
279/// let my_struct = MyStruct( true );
280/// dbg!( !my_struct );
281/// ```
282///
283/// To learn more about the feature, study the module [`derive_tools::Not`](https://docs.rs/derive_tools/latest/derive_tools/not/index.html).
284///
285#[ proc_macro_derive(Not, attributes(not)) ]
286pub fn not(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
287{
288  derive::not::not(input)
289  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
290  .into()
291}
292
293// ///\n// /// Implement `PhantomData` for a structure.\n// ///\n// /// ### Sample.\n// ///\n// /// ```text\n// /// use derive_tools ::PhantomData;\n// ///\n// /// #\[ derive\( PhantomData \) \]\n// /// struct MyStruct< T >\( core ::marker ::PhantomData< T > \);\n// ///\n// /// let my_struct = MyStruct :: \< i32 >\( core ::marker ::PhantomData \);\n// /// dbg!\( my_struct \);\n// /// ```\n// ///\n// /// To learn more about the feature, study the module \[`derive_tools ::PhantomData`\]\(https: //docs.rs/derive_tools/latest/derive_tools/phantom_data/index.html\)\.
294// qqq: This derive is currently generating invalid code by attempting to implement `core ::marker ::PhantomData` as a trait.
295// It needs to be re-designed to correctly handle `PhantomData` usage, likely by adding a field to the struct.
296// Temporarily disabling to allow other tests to pass.
297// #[ proc_macro_derive( PhantomData, attributes( phantom_data ) ]
298// pub fn phantom_data( input: proc_macro::TokenStream ) -> proc_macro::TokenStream
299// {
300//   derive::phantom::phantom( input ).unwrap_or_else( macro_tools::syn::Error::into_compile_error ).into()
301// }
302
303///
304/// Implement `VariadicFrom` for a structure.
305///
306/// ### Sample.
307///
308/// ```text
309/// use derive_tools::VariadicFrom;
310///
311/// #[ derive( VariadicFrom ) ]
312/// struct MyStruct( i32 );
313///
314/// let my_struct = MyStruct::variadic_from( 13 );
315/// dbg!( my_struct.0 );
316/// ```
317///
318/// To learn more about the feature, study the module [`derive_tools::VariadicFrom`](https://docs.rs/derive_tools/latest/derive_tools/variadic_from/index.html).
319///
320#[ proc_macro_derive(VariadicFrom, attributes(variadic_from)) ]
321pub fn variadic_from(input: proc_macro::TokenStream) -> proc_macro::TokenStream 
322{
323  derive::variadic_from::variadic_from(input)
324  .unwrap_or_else(macro_tools::syn::Error::into_compile_error)
325  .into()
326}