Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 /*
2 Copyright ⓒ 2015 rust-custom-derive contributors.
3
4 Licensed under the MIT license (see LICENSE or <http://opensource.org
5 /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
6 <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
7 files in the project carrying such notice may not be copied, modified,
8 or distributed except according to those terms.
9 */
10 /*!
11 This crate provides several macros for deriving implementations of various traits for "newtype" wrappers (*i.e.* tuple structs with a single element). That is, given a tuple struct with exactly one field (*e.g.* `struct Buckets(i32)`), these macros will derive "obvious" implementations of traits such as `Add`, `Neg`, `Index`, `Deref`, `From`, etc.
12
13 All of these macros are designed to be used with the [`custom_derive`](https://crates.io/crates/custom_derive) crate, though they can be used independent of it.
14
15 # Example
16
17 Create a simple integer wrapper with some arithmetic operators:
18
19 ```rust
20 #[macro_use] extern crate custom_derive;
21 #[macro_use] extern crate newtype_derive;
22
23 custom_derive! {
24 #[derive(NewtypeFrom, NewtypeAdd, NewtypeMul(i32))]
25 pub struct Happy(i32);
26 }
27
28 # fn main() {
29 // Let's add some happy little ints.
30 let a = Happy::from(6);
31 let b = Happy::from(7);
32 let c = (a + b) * 3;
33 let d: i32 = c.into();
34 assert_eq!(d, 39);
35 # }
36 ```
37
38 Create a "deref-transparent" wrapper around a type:
39
40 ```rust
41 #[macro_use] extern crate custom_derive;
42 #[macro_use] extern crate newtype_derive;
43
44 custom_derive! {
45 #[derive(NewtypeFrom,
46 NewtypeDeref, NewtypeDerefMut,
47 NewtypeIndex(usize), NewtypeIndexMut(usize)
48 )]
49 pub struct I32Array(Vec<i32>);
50 }
51
52 # fn main() {
53 let mut arr = I32Array::from(vec![1, 2, 3]);
54 arr.push(4);
55 arr[2] = 5;
56 assert_eq!(&**arr, &[1, 2, 5, 4]);
57 assert_eq!(arr.len(), 4);
58 # }
59 ```
60
61 # Overview
62
63 This crate provides macros to derive implementations of the following traits for newtype structs:
64
65 - Binary Arithmetic Operators: Add, BitAnd, BitOr, BitXor, Div, Mul, Rem, Sub, Shl, Shr, plus the corresponding *Assign traits.
66 - Unary Arithmetic Operators: Neg, Not.
67 - Other Operators: Deref, DerefMut, Index, IndexMut.
68 - Formatting: Binary, Debug, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex.
69 - Miscellaneous: From.
70 - Unstable: One, Product, Sum, Zero (requires the `std-unstable` feature).
71
72 All of these macros are named `Newtype$Trait`.
73
74 None of these macros currently support generic newtype structs.
75
76 ## Binary Arithmetic Operators
77
78 Each of the binary arithmetic operators accept several deriving forms. To use `Add` on a struct `T` as an example:
79
80 - `NewtypeAdd`: `impl Add<T, Output=T> for T`
81 - `NewtypeAdd(&self)`: `impl<'a> Add<&'a T, Output=T> for &'a T`
82 - `NewtypeAdd(U)`: `impl Add<U, Output=T> for T`
83 - `NewtypeAdd(&self, U)`: `impl<'a> Add<U, Output=T> for &'a T`
84 - `NewtypeAdd(*)`: All four combinations of `T` and `&T`
85
86 The `*Assign` variants accept zero or one argument only. For example:
87
88 - `NewtypeAddAssign`: `impl AddAssign<T> for T`
89 - `NewtypeAddAssign(&Self)`: `impl<'a> Add<&'a T> for &'a T`
90 - `NewtypeAddAssign(U)`: `impl Add<U> for T`
91 - `NewtypeAddAssign(*)`: Implements for `T` and `&T`.
92
93 In all cases, the implementation unwraps the newtype (where necessary), forwards to the wrapped value's implementation, then re-wraps the result in the newtype.
94
95 ## Unary Arithmetic Operators
96
97 Each of the binary arithmetic operators accept several deriving forms. To use `Neg` on a struct `T` as an example:
98
99 - `NewtypeNeg`: `impl Neg<Output=T> for T`
100 - `NewtypeNeg(&self)`: `impl<'a> Neg<Output=T> for &'a T`
101 - `NewtypeNeg(*)`: both of the above
102
103 In all cases, the implementation unwraps the newtype, forwards to the wrapped value's implementation, then re-wraps the result in the newtype.
104
105 ## Other Operators
106
107 `NewtypeDeref` and `NewtypeDerefMut` only support the argument-less form, and implements the corresponding trait such that the newtype structure derefs to a pointer to the wrapped value.
108
109 `NewtypeIndex` and `NewtypeIndexMut` must be used as `NewtypeIndex(usize)`, where the argument is the type to use for indexing. The call is forwarded to the wrapped value's implementation.
110
111 ## Formatting
112
113 The deriving macros for the formatting traits in [`std::fmt`][] forward to the wrapped value's implementation.
114
115 [`std::fmt`]: http://doc.rust-lang.org/std/fmt/index.html
116
117 ## Miscellaneous
118
119 `NewtypeFrom` implements `std::convert::From` twice: once for converting from the wrapped type to the newtype, and once for converting from the newtype to the wrapped type.
120
121 `NewtypeProduct` and `NewtypeSum` optionally support specifying `&Self` as an argument to generate an implementation that accepts an iterator of borrowed pointers (*e.g.* `NewtypeSum(&Self)`).
122
123 ## Using Without `custom_derive!`
124
125 Although designed to be used with `custom_derive!`, all of the macros in this crate can be used without it. The following:
126
127 ```rust
128 # #[macro_use] extern crate custom_derive;
129 # #[macro_use] extern crate newtype_derive;
130 custom_derive! {
131 #[derive(Copy, Clone, Debug, NewtypeFrom, NewtypeAdd, NewtypeAdd(f32))]
132 pub struct Meters(f32);
133 }
134 # fn main() {}
135 ```
136
137 Can also be written as:
138
139 ```rust
140 # #[macro_use] extern crate newtype_derive;
141 #[derive(Copy, Clone, Debug)]
142 pub struct Meters(f32);
143
144 NewtypeFrom! { () pub struct Meters(f32); }
145 NewtypeAdd! { () pub struct Meters(f32); }
146 NewtypeAdd! { (f32) pub struct Meters(f32); }
147 # fn main() {}
148 ```
149 */
150 /*
151 # `Newtype$binop` Template
152
153 Given `/\/\/\s*(ntbop\s+([A-Za-z0-9]+),\s*([a-z_]+))\n(^#\[.+?\]$\n)*^macro_rules!.*?\{$\n(^ +.*?$\n)*^\}$/`,
154
155 ```
156 // \1
157 #[macro_export]
158 macro_rules! Newtype\2 {
159 ((*) $($tts:tt)*) => {
160 Newtype\2! { () $($tts)* }
161 Newtype\2! { (&self) $($tts)* }
162 Newtype\2! { (&Self) $($tts)* }
163 Newtype\2! { (&self, Self) $($tts)* }
164 };
165 (() $($tts:tt)*) => {
166 newtype_wrap_bin_op! { trait: (::std::ops::\2)::\3, kind: simple, item: $($tts)* }
167 };
168 ((&self) $($tts:tt)*) => {
169 newtype_wrap_bin_op! { trait: (::std::ops::\2)::\3, kind: simple_ref, item: $($tts)* }
170 };
171 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
172 newtype_wrap_bin_op! { trait: (::std::ops::\2)::\3, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
173 };
174 (($($rhs:tt)*) $($tts:tt)*) => {
175 newtype_wrap_bin_op! { trait: (::std::ops::\2)::\3, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
176 };
177 }
178 ```
179
180 # `Newtype$binopass` Template
181
182 Given `/\/\/\s*(ntbopass\s+([A-Za-z0-9]+),\s*([a-z_]+))\n(^#\[.+?\]$\n)*^macro_rules!.*?\{$\n(^ +.*?$\n)*^\}$/`,
183
184 ```
185 // \1
186 #[macro_export]
187 #[cfg(op_assign)]
188 macro_rules! Newtype\2 {
189 ((*) $($tts:tt)*) => {
190 Newtype\2! { () $($tts)* }
191 Newtype\2! { (&self) $($tts)* }
192 };
193 (() $($tts:tt)*) => {
194 newtype_wrap_bin_op_assign! { trait: (::std::ops::\2)::\3, kind: simple, item: $($tts)* }
195 };
196 (($($rhs:tt)*) $($tts:tt)*) => {
197 newtype_wrap_bin_op_assign! { trait: (::std::ops::\2)::\3, kind: rhs($($rhs)*), item: $($tts)* }
198 };
199 }
200 ```
201
202 # `Newtype$unop` Template
203
204 Given `/\/\/\s*(ntuop\s+([A-Za-z0-9]+),\s*([a-z_]+))\n(^#\[.+?\]$\n)*^macro_rules!.*?\{$\n(^ +.*?$\n)*^\}$/`,
205
206 ```
207 // \1
208 #[macro_export]
209 macro_rules! Newtype\2 {
210 ((*) $($tts:tt)*) => {
211 Newtype\2! { () $($tts)* }
212 Newtype\2! { (&self) $($tts)* }
213 };
214 (() $($tts:tt)*) => {
215 newtype_wrap_un_op! { trait: (::std::ops::\2)::\3, kind: simple, item: $($tts)* }
216 };
217 ((&self) $($tts:tt)*) => {
218 newtype_wrap_un_op! { trait: (::std::ops::\2)::\3, kind: simple_ref, item: $($tts)* }
219 };
220 }
221 ```
222 */
223 #![cfg_attr(not(feature = "std"), no_std)]
224
225 mod std_unstable;
226
227 #[doc(hidden)]
228 #[macro_export]
229 macro_rules! newtype_as_item {
230 ($i:item) => {$i};
231 }
232
233 #[doc(hidden)]
234 #[macro_export]
235 macro_rules! newtype_wrap_bin_op {
236 (
237 trait: ($($tr:tt)*)::$meth:ident,
238 kind: simple,
239 item: $(pub)* struct $name:ident(pub $t:ty);
240 ) => {
241 newtype_as_item! {
242 impl $($tr)*<$name> for $name {
243 type Output = $name;
244 fn $meth(self, rhs: Self) -> $name {
245 $name((self.0).$meth(rhs.0))
246 }
247 }
248 }
249 };
250
251 (
252 trait: ($($tr:tt)*)::$meth:ident,
253 kind: simple,
254 item: $(pub)* struct $name:ident($t:ty);
255 ) => {
256 newtype_as_item! {
257 impl $($tr)*<$name> for $name {
258 type Output = $name;
259 fn $meth(self, rhs: Self) -> $name {
260 $name((self.0).$meth(rhs.0))
261 }
262 }
263 }
264 };
265
266 (
267 trait: ($($tr:tt)*)::$meth:ident,
268 kind: simple_ref,
269 item: $(pub)* struct $name:ident(pub $t:ty);
270 ) => {
271 newtype_as_item! {
272 impl<'a> $($tr)*<&'a $name> for &'a $name {
273 type Output = $name;
274 fn $meth(self, rhs: Self) -> $name {
275 $name((self.0).$meth(rhs.0))
276 }
277 }
278 }
279 };
280
281 (
282 trait: ($($tr:tt)*)::$meth:ident,
283 kind: simple_ref,
284 item: $(pub)* struct $name:ident($t:ty);
285 ) => {
286 newtype_as_item! {
287 impl<'a> $($tr)*<&'a $name> for &'a $name {
288 type Output = $name;
289 fn $meth(self, rhs: Self) -> $name {
290 $name((self.0).$meth(rhs.0))
291 }
292 }
293 }
294 };
295
296 (
297 trait: ($($tr:tt)*)::$meth:ident,
298 kind: rhs_rewrap(&Self),
299 item: $(pub)* struct $name:ident(pub $t:ty);
300 ) => {
301 newtype_as_item! {
302 impl<'a> $($tr)*<&'a $name> for $name {
303 type Output = $name;
304 fn $meth(self, rhs: &'a $name) -> $name {
305 $name((self.0).$meth(&rhs.0))
306 }
307 }
308 }
309 };
310
311 (
312 trait: ($($tr:tt)*)::$meth:ident,
313 kind: rhs_rewrap(&Self),
314 item: $(pub)* struct $name:ident($t:ty);
315 ) => {
316 newtype_as_item! {
317 impl<'a> $($tr)*<&'a $name> for $name {
318 type Output = $name;
319 fn $meth(self, rhs: &'a $name) -> $name {
320 $name((self.0).$meth(&rhs.0))
321 }
322 }
323 }
324 };
325
326 (
327 trait: ($($tr:tt)*)::$meth:ident,
328 kind: rhs_rewrap($rhs:ty),
329 item: $(pub)* struct $name:ident(pub $t:ty);
330 ) => {
331 newtype_as_item! {
332 impl $($tr)*<$rhs> for $name {
333 type Output = $name;
334 fn $meth(self, rhs: $rhs) -> $name {
335 $name((self.0).$meth(rhs))
336 }
337 }
338 }
339 };
340
341 (
342 trait: ($($tr:tt)*)::$meth:ident,
343 kind: rhs_rewrap($rhs:ty),
344 item: $(pub)* struct $name:ident($t:ty);
345 ) => {
346 newtype_as_item! {
347 impl $($tr)*<$rhs> for $name {
348 type Output = $name;
349 fn $meth(self, rhs: $rhs) -> $name {
350 $name((self.0).$meth(rhs))
351 }
352 }
353 }
354 };
355
356 (
357 trait: ($($tr:tt)*)::$meth:ident,
358 kind: ref_rhs_rewrap(Self),
359 item: $(pub)* struct $name:ident(pub $t:ty);
360 ) => {
361 newtype_as_item! {
362 impl<'a> $($tr)*<$name> for &'a $name {
363 type Output = $name;
364 fn $meth(self, rhs: $name) -> $name {
365 $name((self.0).$meth(rhs.0))
366 }
367 }
368 }
369 };
370
371 (
372 trait: ($($tr:tt)*)::$meth:ident,
373 kind: ref_rhs_rewrap(Self),
374 item: $(pub)* struct $name:ident($t:ty);
375 ) => {
376 newtype_as_item! {
377 impl<'a> $($tr)*<$name> for &'a $name {
378 type Output = $name;
379 fn $meth(self, rhs: $name) -> $name {
380 $name((self.0).$meth(rhs.0))
381 }
382 }
383 }
384 };
385
386 (
387 trait: ($($tr:tt)*)::$meth:ident,
388 kind: ref_rhs_rewrap($rhs:ty),
389 item: $(pub)* struct $name:ident(pub $t:ty);
390 ) => {
391 newtype_as_item! {
392 impl<'a> $($tr)*<$rhs> for &'a $name {
393 type Output = $name;
394 fn $meth(self, rhs: $rhs) -> $name {
395 $name((self.0).$meth(rhs))
396 }
397 }
398 }
399 };
400
401 (
402 trait: ($($tr:tt)*)::$meth:ident,
403 kind: ref_rhs_rewrap($rhs:ty),
404 item: $(pub)* struct $name:ident($t:ty);
405 ) => {
406 newtype_as_item! {
407 impl<'a> $($tr)*<$rhs> for &'a $name {
408 type Output = $name;
409 fn $meth(self, rhs: $rhs) -> $name {
410 $name((self.0).$meth(rhs))
411 }
412 }
413 }
414 };
415 }
416
417 #[doc(hidden)]
418 #[macro_export]
419 macro_rules! newtype_wrap_bin_op_assign {
420 (
421 trait: ($($tr:tt)*)::$meth:ident,
422 kind: simple,
423 item: $(pub)* struct $name:ident(pub $t:ty);
424 ) => {
425 newtype_as_item! {
426 impl $($tr)*<$name> for $name {
427 fn $meth(&mut self, rhs: Self) {
428 (self.0).$meth(rhs.0)
429 }
430 }
431 }
432 };
433
434 (
435 trait: ($($tr:tt)*)::$meth:ident,
436 kind: simple,
437 item: $(pub)* struct $name:ident($t:ty);
438 ) => {
439 newtype_as_item! {
440 impl $($tr)*<$name> for $name {
441 fn $meth(&mut self, rhs: Self) {
442 (self.0).$meth(rhs.0)
443 }
444 }
445 }
446 };
447
448 (
449 trait: ($($tr:tt)*)::$meth:ident,
450 kind: rhs(&Self),
451 item: $(pub)* struct $name:ident(pub $t:ty);
452 ) => {
453 newtype_as_item! {
454 impl<'a> $($tr)*<&'a $name> for $name {
455 fn $meth(&mut self, rhs: &'a $name) {
456 (self.0).$meth(rhs.0)
457 }
458 }
459 }
460 };
461
462 (
463 trait: ($($tr:tt)*)::$meth:ident,
464 kind: rhs(&Self),
465 item: $(pub)* struct $name:ident($t:ty);
466 ) => {
467 newtype_as_item! {
468 impl<'a> $($tr)*<&'a $name> for $name {
469 fn $meth(&mut self, rhs: &'a $name) {
470 (self.0).$meth(rhs.0)
471 }
472 }
473 }
474 };
475
476 (
477 trait: ($($tr:tt)*)::$meth:ident,
478 kind: rhs($rhs:ty),
479 item: $(pub)* struct $name:ident(pub $t:ty);
480 ) => {
481 newtype_as_item! {
482 impl $($tr)*<$rhs> for $name {
483 fn $meth(&mut self, rhs: $rhs) {
484 (self.0).$meth(rhs)
485 }
486 }
487 }
488 };
489
490 (
491 trait: ($($tr:tt)*)::$meth:ident,
492 kind: rhs($rhs:ty),
493 item: $(pub)* struct $name:ident($t:ty);
494 ) => {
495 newtype_as_item! {
496 impl $($tr)*<$rhs> for $name {
497 fn $meth(&mut self, rhs: $rhs) {
498 (self.0).$meth(rhs)
499 }
500 }
501 }
502 };
503 }
504
505 #[doc(hidden)]
506 #[macro_export]
507 macro_rules! newtype_wrap_un_op {
508 (
509 trait: ($($tr:tt)*)::$meth:ident,
510 kind: simple,
511 item: $(pub)* struct $name:ident(pub $t:ty);
512 ) => {
513 newtype_as_item! {
514 impl $($tr)* for $name {
515 type Output = $name;
516 fn $meth(self) -> $name {
517 $name((self.0).$meth())
518 }
519 }
520 }
521 };
522
523 (
524 trait: ($($tr:tt)*)::$meth:ident,
525 kind: simple,
526 item: $(pub)* struct $name:ident($t:ty);
527 ) => {
528 newtype_as_item! {
529 impl $($tr)* for $name {
530 type Output = $name;
531 fn $meth(self) -> $name {
532 $name((self.0).$meth())
533 }
534 }
535 }
536 };
537
538 (
539 trait: ($($tr:tt)*)::$meth:ident,
540 kind: simple_ref,
541 item: $(pub)* struct $name:ident(pub $t:ty);
542 ) => {
543 newtype_as_item! {
544 impl<'a> $($tr)* for &'a $name {
545 type Output = $name;
546 fn $meth(self) -> $name {
547 $name((self.0).$meth())
548 }
549 }
550 }
551 };
552
553 (
554 trait: ($($tr:tt)*)::$meth:ident,
555 kind: simple_ref,
556 item: $(pub)* struct $name:ident($t:ty);
557 ) => {
558 newtype_as_item! {
559 impl<'a> $($tr)* for &'a $name {
560 type Output = $name;
561 fn $meth(self) -> $name {
562 $name((self.0).$meth())
563 }
564 }
565 }
566 };
567 }
568
569 // ntbop Add, add
570 #[macro_export]
571 macro_rules! NewtypeAdd {
572 ((*) $($tts:tt)*) => {
573 NewtypeAdd! { () $($tts)* }
574 NewtypeAdd! { (&self) $($tts)* }
575 NewtypeAdd! { (&Self) $($tts)* }
576 NewtypeAdd! { (&self, Self) $($tts)* }
577 };
578 (() $($tts:tt)*) => {
579 newtype_wrap_bin_op! { trait: (::std::ops::Add)::add, kind: simple, item: $($tts)* }
580 };
581 ((&self) $($tts:tt)*) => {
582 newtype_wrap_bin_op! { trait: (::std::ops::Add)::add, kind: simple_ref, item: $($tts)* }
583 };
584 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
585 newtype_wrap_bin_op! { trait: (::std::ops::Add)::add, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
586 };
587 (($($rhs:tt)*) $($tts:tt)*) => {
588 newtype_wrap_bin_op! { trait: (::std::ops::Add)::add, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
589 };
590 }
591
592 // ntbopass AddAssign, add_assign
593 #[macro_export]
594 #[cfg(op_assign)]
595 macro_rules! NewtypeAddAssign {
596 ((*) $($tts:tt)*) => {
597 NewtypeAddAssign! { () $($tts)* }
598 NewtypeAddAssign! { (&self) $($tts)* }
599 };
600 (() $($tts:tt)*) => {
601 newtype_wrap_bin_op_assign! { trait: (::std::ops::AddAssign)::add_assign, kind: simple, item: $($tts)* }
602 };
603 (($($rhs:tt)*) $($tts:tt)*) => {
604 newtype_wrap_bin_op_assign! { trait: (::std::ops::AddAssign)::add_assign, kind: rhs($($rhs)*), item: $($tts)* }
605 };
606 }
607
608 // ntbop BitAnd, bitand
609 #[macro_export]
610 macro_rules! NewtypeBitAnd {
611 ((*) $($tts:tt)*) => {
612 NewtypeBitAnd! { () $($tts)* }
613 NewtypeBitAnd! { (&self) $($tts)* }
614 NewtypeBitAnd! { (&Self) $($tts)* }
615 NewtypeBitAnd! { (&self, Self) $($tts)* }
616 };
617 (() $($tts:tt)*) => {
618 newtype_wrap_bin_op! { trait: (::std::ops::BitAnd)::bitand, kind: simple, item: $($tts)* }
619 };
620 ((&self) $($tts:tt)*) => {
621 newtype_wrap_bin_op! { trait: (::std::ops::BitAnd)::bitand, kind: simple_ref, item: $($tts)* }
622 };
623 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
624 newtype_wrap_bin_op! { trait: (::std::ops::BitAnd)::bitand, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
625 };
626 (($($rhs:tt)*) $($tts:tt)*) => {
627 newtype_wrap_bin_op! { trait: (::std::ops::BitAnd)::bitand, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
628 };
629 }
630
631 // ntbopass BitAndAssign, bitand_assign
632 #[macro_export]
633 #[cfg(op_assign)]
634 macro_rules! NewtypeBitAndAssign {
635 ((*) $($tts:tt)*) => {
636 NewtypeBitAndAssign! { () $($tts)* }
637 NewtypeBitAndAssign! { (&self) $($tts)* }
638 };
639 (() $($tts:tt)*) => {
640 newtype_wrap_bin_op_assign! { trait: (::std::ops::BitAndAssign)::bitand_assign, kind: simple, item: $($tts)* }
641 };
642 (($($rhs:tt)*) $($tts:tt)*) => {
643 newtype_wrap_bin_op_assign! { trait: (::std::ops::BitAndAssign)::bitand_assign, kind: rhs($($rhs)*), item: $($tts)* }
644 };
645 }
646
647 // ntbop BitOr, bitor
648 #[macro_export]
649 macro_rules! NewtypeBitOr {
650 ((*) $($tts:tt)*) => {
651 NewtypeBitOr! { () $($tts)* }
652 NewtypeBitOr! { (&self) $($tts)* }
653 NewtypeBitOr! { (&Self) $($tts)* }
654 NewtypeBitOr! { (&self, Self) $($tts)* }
655 };
656 (() $($tts:tt)*) => {
657 newtype_wrap_bin_op! { trait: (::std::ops::BitOr)::bitor, kind: simple, item: $($tts)* }
658 };
659 ((&self) $($tts:tt)*) => {
660 newtype_wrap_bin_op! { trait: (::std::ops::BitOr)::bitor, kind: simple_ref, item: $($tts)* }
661 };
662 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
663 newtype_wrap_bin_op! { trait: (::std::ops::BitOr)::bitor, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
664 };
665 (($($rhs:tt)*) $($tts:tt)*) => {
666 newtype_wrap_bin_op! { trait: (::std::ops::BitOr)::bitor, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
667 };
668 }
669
670 // ntbopass BitOrAssign, bitor_assign
671 #[macro_export]
672 #[cfg(op_assign)]
673 macro_rules! NewtypeBitOrAssign {
674 ((*) $($tts:tt)*) => {
675 NewtypeBitOrAssign! { () $($tts)* }
676 NewtypeBitOrAssign! { (&self) $($tts)* }
677 };
678 (() $($tts:tt)*) => {
679 newtype_wrap_bin_op_assign! { trait: (::std::ops::BitOrAssign)::bitor_assign, kind: simple, item: $($tts)* }
680 };
681 (($($rhs:tt)*) $($tts:tt)*) => {
682 newtype_wrap_bin_op_assign! { trait: (::std::ops::BitOrAssign)::bitor_assign, kind: rhs($($rhs)*), item: $($tts)* }
683 };
684 }
685
686 // ntbop BitXor, bitxor
687 #[macro_export]
688 macro_rules! NewtypeBitXor {
689 ((*) $($tts:tt)*) => {
690 NewtypeBitXor! { () $($tts)* }
691 NewtypeBitXor! { (&self) $($tts)* }
692 NewtypeBitXor! { (&Self) $($tts)* }
693 NewtypeBitXor! { (&self, Self) $($tts)* }
694 };
695 (() $($tts:tt)*) => {
696 newtype_wrap_bin_op! { trait: (::std::ops::BitXor)::bitxor, kind: simple, item: $($tts)* }
697 };
698 ((&self) $($tts:tt)*) => {
699 newtype_wrap_bin_op! { trait: (::std::ops::BitXor)::bitxor, kind: simple_ref, item: $($tts)* }
700 };
701 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
702 newtype_wrap_bin_op! { trait: (::std::ops::BitXor)::bitxor, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
703 };
704 (($($rhs:tt)*) $($tts:tt)*) => {
705 newtype_wrap_bin_op! { trait: (::std::ops::BitXor)::bitxor, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
706 };
707 }
708
709 // ntbopass BitXorAssign, bitxor_assign
710 #[macro_export]
711 #[cfg(op_assign)]
712 macro_rules! NewtypeBitXorAssign {
713 ((*) $($tts:tt)*) => {
714 NewtypeBitXorAssign! { () $($tts)* }
715 NewtypeBitXorAssign! { (&self) $($tts)* }
716 };
717 (() $($tts:tt)*) => {
718 newtype_wrap_bin_op_assign! { trait: (::std::ops::BitXorAssign)::bitxor_assign, kind: simple, item: $($tts)* }
719 };
720 (($($rhs:tt)*) $($tts:tt)*) => {
721 newtype_wrap_bin_op_assign! { trait: (::std::ops::BitXorAssign)::bitxor_assign, kind: rhs($($rhs)*), item: $($tts)* }
722 };
723 }
724
725 // ntbop Div, div
726 #[macro_export]
727 macro_rules! NewtypeDiv {
728 ((*) $($tts:tt)*) => {
729 NewtypeDiv! { () $($tts)* }
730 NewtypeDiv! { (&self) $($tts)* }
731 NewtypeDiv! { (&Self) $($tts)* }
732 NewtypeDiv! { (&self, Self) $($tts)* }
733 };
734 (() $($tts:tt)*) => {
735 newtype_wrap_bin_op! { trait: (::std::ops::Div)::div, kind: simple, item: $($tts)* }
736 };
737 ((&self) $($tts:tt)*) => {
738 newtype_wrap_bin_op! { trait: (::std::ops::Div)::div, kind: simple_ref, item: $($tts)* }
739 };
740 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
741 newtype_wrap_bin_op! { trait: (::std::ops::Div)::div, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
742 };
743 (($($rhs:tt)*) $($tts:tt)*) => {
744 newtype_wrap_bin_op! { trait: (::std::ops::Div)::div, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
745 };
746 }
747
748 // ntbopass DivAssign, div_assign
749 #[macro_export]
750 #[cfg(op_assign)]
751 macro_rules! NewtypeDivAssign {
752 ((*) $($tts:tt)*) => {
753 NewtypeDivAssign! { () $($tts)* }
754 NewtypeDivAssign! { (&self) $($tts)* }
755 };
756 (() $($tts:tt)*) => {
757 newtype_wrap_bin_op_assign! { trait: (::std::ops::DivAssign)::div_assign, kind: simple, item: $($tts)* }
758 };
759 (($($rhs:tt)*) $($tts:tt)*) => {
760 newtype_wrap_bin_op_assign! { trait: (::std::ops::DivAssign)::div_assign, kind: rhs($($rhs)*), item: $($tts)* }
761 };
762 }
763
764 // ntbop Mul, mul
765 #[macro_export]
766 macro_rules! NewtypeMul {
767 ((*) $($tts:tt)*) => {
768 NewtypeMul! { () $($tts)* }
769 NewtypeMul! { (&self) $($tts)* }
770 NewtypeMul! { (&Self) $($tts)* }
771 NewtypeMul! { (&self, Self) $($tts)* }
772 };
773 (() $($tts:tt)*) => {
774 newtype_wrap_bin_op! { trait: (::std::ops::Mul)::mul, kind: simple, item: $($tts)* }
775 };
776 ((&self) $($tts:tt)*) => {
777 newtype_wrap_bin_op! { trait: (::std::ops::Mul)::mul, kind: simple_ref, item: $($tts)* }
778 };
779 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
780 newtype_wrap_bin_op! { trait: (::std::ops::Mul)::mul, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
781 };
782 (($($rhs:tt)*) $($tts:tt)*) => {
783 newtype_wrap_bin_op! { trait: (::std::ops::Mul)::mul, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
784 };
785 }
786
787 // ntbopass MulAssign, mul_assign
788 #[macro_export]
789 #[cfg(op_assign)]
790 macro_rules! NewtypeMulAssign {
791 ((*) $($tts:tt)*) => {
792 NewtypeMulAssign! { () $($tts)* }
793 NewtypeMulAssign! { (&self) $($tts)* }
794 };
795 (() $($tts:tt)*) => {
796 newtype_wrap_bin_op_assign! { trait: (::std::ops::MulAssign)::mul_assign, kind: simple, item: $($tts)* }
797 };
798 (($($rhs:tt)*) $($tts:tt)*) => {
799 newtype_wrap_bin_op_assign! { trait: (::std::ops::MulAssign)::mul_assign, kind: rhs($($rhs)*), item: $($tts)* }
800 };
801 }
802
803 // ntbop Rem, rem
804 #[macro_export]
805 macro_rules! NewtypeRem {
806 ((*) $($tts:tt)*) => {
807 NewtypeRem! { () $($tts)* }
808 NewtypeRem! { (&self) $($tts)* }
809 NewtypeRem! { (&Self) $($tts)* }
810 NewtypeRem! { (&self, Self) $($tts)* }
811 };
812 (() $($tts:tt)*) => {
813 newtype_wrap_bin_op! { trait: (::std::ops::Rem)::rem, kind: simple, item: $($tts)* }
814 };
815 ((&self) $($tts:tt)*) => {
816 newtype_wrap_bin_op! { trait: (::std::ops::Rem)::rem, kind: simple_ref, item: $($tts)* }
817 };
818 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
819 newtype_wrap_bin_op! { trait: (::std::ops::Rem)::rem, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
820 };
821 (($($rhs:tt)*) $($tts:tt)*) => {
822 newtype_wrap_bin_op! { trait: (::std::ops::Rem)::rem, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
823 };
824 }
825
826 // ntbopass RemAssign, rem_assign
827 #[macro_export]
828 #[cfg(op_assign)]
829 macro_rules! NewtypeRemAssign {
830 ((*) $($tts:tt)*) => {
831 NewtypeRemAssign! { () $($tts)* }
832 NewtypeRemAssign! { (&self) $($tts)* }
833 };
834 (() $($tts:tt)*) => {
835 newtype_wrap_bin_op_assign! { trait: (::std::ops::RemAssign)::rem_assign, kind: simple, item: $($tts)* }
836 };
837 (($($rhs:tt)*) $($tts:tt)*) => {
838 newtype_wrap_bin_op_assign! { trait: (::std::ops::RemAssign)::rem_assign, kind: rhs($($rhs)*), item: $($tts)* }
839 };
840 }
841
842 // ntbop Sub, sub
843 #[macro_export]
844 macro_rules! NewtypeSub {
845 ((*) $($tts:tt)*) => {
846 NewtypeSub! { () $($tts)* }
847 NewtypeSub! { (&self) $($tts)* }
848 NewtypeSub! { (&Self) $($tts)* }
849 NewtypeSub! { (&self, Self) $($tts)* }
850 };
851 (() $($tts:tt)*) => {
852 newtype_wrap_bin_op! { trait: (::std::ops::Sub)::sub, kind: simple, item: $($tts)* }
853 };
854 ((&self) $($tts:tt)*) => {
855 newtype_wrap_bin_op! { trait: (::std::ops::Sub)::sub, kind: simple_ref, item: $($tts)* }
856 };
857 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
858 newtype_wrap_bin_op! { trait: (::std::ops::Sub)::sub, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
859 };
860 (($($rhs:tt)*) $($tts:tt)*) => {
861 newtype_wrap_bin_op! { trait: (::std::ops::Sub)::sub, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
862 };
863 }
864
865 // ntbopass SubAssign, sub_assign
866 #[macro_export]
867 #[cfg(op_assign)]
868 macro_rules! NewtypeSubAssign {
869 ((*) $($tts:tt)*) => {
870 NewtypeSubAssign! { () $($tts)* }
871 NewtypeSubAssign! { (&self) $($tts)* }
872 };
873 (() $($tts:tt)*) => {
874 newtype_wrap_bin_op_assign! { trait: (::std::ops::SubAssign)::sub_assign, kind: simple, item: $($tts)* }
875 };
876 (($($rhs:tt)*) $($tts:tt)*) => {
877 newtype_wrap_bin_op_assign! { trait: (::std::ops::SubAssign)::sub_assign, kind: rhs($($rhs)*), item: $($tts)* }
878 };
879 }
880
881 // ntbop Shl, shl
882 #[macro_export]
883 macro_rules! NewtypeShl {
884 ((*) $($tts:tt)*) => {
885 NewtypeShl! { () $($tts)* }
886 NewtypeShl! { (&self) $($tts)* }
887 NewtypeShl! { (&Self) $($tts)* }
888 NewtypeShl! { (&self, Self) $($tts)* }
889 };
890 (() $($tts:tt)*) => {
891 newtype_wrap_bin_op! { trait: (::std::ops::Shl)::shl, kind: simple, item: $($tts)* }
892 };
893 ((&self) $($tts:tt)*) => {
894 newtype_wrap_bin_op! { trait: (::std::ops::Shl)::shl, kind: simple_ref, item: $($tts)* }
895 };
896 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
897 newtype_wrap_bin_op! { trait: (::std::ops::Shl)::shl, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
898 };
899 (($($rhs:tt)*) $($tts:tt)*) => {
900 newtype_wrap_bin_op! { trait: (::std::ops::Shl)::shl, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
901 };
902 }
903
904 // ntbopass ShlAssign, shl_assign
905 #[macro_export]
906 #[cfg(op_assign)]
907 macro_rules! NewtypeShlAssign {
908 ((*) $($tts:tt)*) => {
909 NewtypeShlAssign! { () $($tts)* }
910 NewtypeShlAssign! { (&self) $($tts)* }
911 };
912 (() $($tts:tt)*) => {
913 newtype_wrap_bin_op_assign! { trait: (::std::ops::ShlAssign)::shl_assign, kind: simple, item: $($tts)* }
914 };
915 (($($rhs:tt)*) $($tts:tt)*) => {
916 newtype_wrap_bin_op_assign! { trait: (::std::ops::ShlAssign)::shl_assign, kind: rhs($($rhs)*), item: $($tts)* }
917 };
918 }
919
920 // ntbop Shr, shr
921 #[macro_export]
922 macro_rules! NewtypeShr {
923 ((*) $($tts:tt)*) => {
924 NewtypeShr! { () $($tts)* }
925 NewtypeShr! { (&self) $($tts)* }
926 NewtypeShr! { (&Self) $($tts)* }
927 NewtypeShr! { (&self, Self) $($tts)* }
928 };
929 (() $($tts:tt)*) => {
930 newtype_wrap_bin_op! { trait: (::std::ops::Shr)::shr, kind: simple, item: $($tts)* }
931 };
932 ((&self) $($tts:tt)*) => {
933 newtype_wrap_bin_op! { trait: (::std::ops::Shr)::shr, kind: simple_ref, item: $($tts)* }
934 };
935 ((&self, $($rhs:tt)*) $($tts:tt)*) => {
936 newtype_wrap_bin_op! { trait: (::std::ops::Shr)::shr, kind: ref_rhs_rewrap($($rhs)*), item: $($tts)* }
937 };
938 (($($rhs:tt)*) $($tts:tt)*) => {
939 newtype_wrap_bin_op! { trait: (::std::ops::Shr)::shr, kind: rhs_rewrap($($rhs)*), item: $($tts)* }
940 };
941 }
942
943 // ntbopass ShrAssign, shr_assign
944 #[macro_export]
945 #[cfg(op_assign)]
946 macro_rules! NewtypeShrAssign {
947 ((*) $($tts:tt)*) => {
948 NewtypeShrAssign! { () $($tts)* }
949 NewtypeShrAssign! { (&self) $($tts)* }
950 };
951 (() $($tts:tt)*) => {
952 newtype_wrap_bin_op_assign! { trait: (::std::ops::ShrAssign)::shr_assign, kind: simple, item: $($tts)* }
953 };
954 (($($rhs:tt)*) $($tts:tt)*) => {
955 newtype_wrap_bin_op_assign! { trait: (::std::ops::ShrAssign)::shr_assign, kind: rhs($($rhs)*), item: $($tts)* }
956 };
957 }
958
959 // ntuop Neg, neg
960 #[macro_export]
961 macro_rules! NewtypeNeg {
962 ((*) $($tts:tt)*) => {
963 NewtypeNeg! { () $($tts)* }
964 NewtypeNeg! { (&self) $($tts)* }
965 };
966 (() $($tts:tt)*) => {
967 newtype_wrap_un_op! { trait: (::std::ops::Neg)::neg, kind: simple, item: $($tts)* }
968 };
969 ((&self) $($tts:tt)*) => {
970 newtype_wrap_un_op! { trait: (::std::ops::Neg)::neg, kind: simple_ref, item: $($tts)* }
971 };
972 }
973
974 // ntuop Not, not
975 #[macro_export]
976 macro_rules! NewtypeNot {
977 ((*) $($tts:tt)*) => {
978 NewtypeNot! { () $($tts)* }
979 NewtypeNot! { (&self) $($tts)* }
980 };
981 (() $($tts:tt)*) => {
982 newtype_wrap_un_op! { trait: (::std::ops::Not)::not, kind: simple, item: $($tts)* }
983 };
984 ((&self) $($tts:tt)*) => {
985 newtype_wrap_un_op! { trait: (::std::ops::Not)::not, kind: simple_ref, item: $($tts)* }
986 };
987 }
988
989 #[macro_export]
990 macro_rules! NewtypeDeref {
991 (() $(pub)* struct $name:ident(pub $t0:ty);) => {
992 impl ::std::ops::Deref for $name {
993 type Target = $t0;
994 fn deref(&self) -> &Self::Target {
995 &self.0
996 }
997 }
998 };
999
1000 (() $(pub)* struct $name:ident($t0:ty);) => {
1001 impl ::std::ops::Deref for $name {
1002 type Target = $t0;
1003 fn deref(&self) -> &Self::Target {
1004 &self.0
1005 }
1006 }
1007 };
1008 }
1009
1010 #[macro_export]
1011 macro_rules! NewtypeDerefMut {
1012 (() $(pub)* struct $name:ident(pub $t0:ty);) => {
1013 impl ::std::ops::DerefMut for $name {
1014 fn deref_mut(&mut self) -> &mut Self::Target {
1015 &mut self.0
1016 }
1017 }
1018 };
1019
1020 (() $(pub)* struct $name:ident($t0:ty);) => {
1021 impl ::std::ops::DerefMut for $name {
1022 fn deref_mut(&mut self) -> &mut Self::Target {
1023 &mut self.0
1024 }
1025 }
1026 };
1027 }
1028
1029 #[macro_export]
1030 macro_rules! NewtypeIndex {
1031 (($index_ty:ty) $(pub)* struct $name:ident(pub $t0:ty);) => {
1032 impl ::std::ops::Index<$index_ty> for $name {
1033 type Output = <$t0 as ::std::ops::Index<$index_ty>>::Output;
1034 fn index(&self, index: $index_ty) -> &Self::Output {
1035 (&self.0).index(index)
1036 }
1037 }
1038 };
1039
1040 (($index_ty:ty) $(pub)* struct $name:ident($t0:ty);) => {
1041 impl ::std::ops::Index<$index_ty> for $name {
1042 type Output = <$t0 as ::std::ops::Index<$index_ty>>::Output;
1043 fn index(&self, index: $index_ty) -> &Self::Output {
1044 (&self.0).index(index)
1045 }
1046 }
1047 };
1048 }
1049
1050 #[macro_export]
1051 macro_rules! NewtypeIndexMut {
1052 (($index_ty:ty) $(pub)* struct $name:ident(pub $t0:ty);) => {
1053 impl ::std::ops::IndexMut<$index_ty> for $name {
1054 fn index_mut(&mut self, index: $index_ty) -> &mut Self::Output {
1055 (&mut self.0).index_mut(index)
1056 }
1057 }
1058 };
1059
1060 (($index_ty:ty) $(pub)* struct $name:ident($t0:ty);) => {
1061 impl ::std::ops::IndexMut<$index_ty> for $name {
1062 fn index_mut(&mut self, index: $index_ty) -> &mut Self::Output {
1063 (&mut self.0).index_mut(index)
1064 }
1065 }
1066 };
1067 }
1068
1069 #[macro_export]
1070 macro_rules! NewtypeFrom {
1071 (() $(pub)* struct $name:ident(pub $t0:ty);) => {
1072 impl ::std::convert::From<$t0> for $name {
1073 fn from(v: $t0) -> Self {
1074 $name(v)
1075 }
1076 }
1077 impl ::std::convert::From<$name> for $t0 {
1078 fn from(v: $name) -> Self {
1079 v.0
1080 }
1081 }
1082 };
1083
1084 (() $(pub)* struct $name:ident($t0:ty);) => {
1085 impl ::std::convert::From<$t0> for $name {
1086 fn from(v: $t0) -> Self {
1087 $name(v)
1088 }
1089 }
1090 impl ::std::convert::From<$name> for $t0 {
1091 fn from(v: $name) -> Self {
1092 v.0
1093 }
1094 }
1095 };
1096 }
1097
1098 #[doc(hidden)]
1099 #[macro_export]
1100 macro_rules! newtype_fmt {
1101 ($fmt_trait:ident, $name:ident) => {
1102 impl ::std::fmt::$fmt_trait for $name {
1103 fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
1104 ::std::fmt::$fmt_trait::fmt(&self.0, fmt)
1105 }
1106 }
1107 };
1108 }
1109
1110 #[macro_export]
1111 macro_rules! NewtypeBinary {
1112 (() $(pub)* struct $name:ident $_field:tt;) => {
1113 newtype_fmt! { Binary, $name }
1114 };
1115 }
1116
1117 #[macro_export]
1118 macro_rules! NewtypeDebug {
1119 (() $(pub)* struct $name:ident $_field:tt;) => {
1120 newtype_fmt! { Debug, $name }
1121 };
1122 }
1123
1124 #[macro_export]
1125 macro_rules! NewtypeDisplay {
1126 (() $(pub)* struct $name:ident $_field:tt;) => {
1127 newtype_fmt! { Display, $name }
1128 };
1129 }
1130
1131 #[macro_export]
1132 macro_rules! NewtypeLowerExp {
1133 (() $(pub)* struct $name:ident $_field:tt;) => {
1134 newtype_fmt! { LowerExp, $name }
1135 };
1136 }
1137
1138 #[macro_export]
1139 macro_rules! NewtypeLowerHex {
1140 (() $(pub)* struct $name:ident $_field:tt;) => {
1141 newtype_fmt! { LowerHex, $name }
1142 };
1143 }
1144
1145 #[macro_export]
1146 macro_rules! NewtypeOctal {
1147 (() $(pub)* struct $name:ident $_field:tt;) => {
1148 newtype_fmt! { Octal, $name }
1149 };
1150 }
1151
1152 #[macro_export]
1153 macro_rules! NewtypePointer {
1154 (() $(pub)* struct $name:ident $_field:tt;) => {
1155 newtype_fmt! { Pointer, $name }
1156 };
1157 }
1158
1159 #[macro_export]
1160 macro_rules! NewtypeUpperExp {
1161 (() $(pub)* struct $name:ident $_field:tt;) => {
1162 newtype_fmt! { UpperExp, $name }
1163 };
1164 }
1165
1166 #[macro_export]
1167 macro_rules! NewtypeUpperHex {
1168 (() $(pub)* struct $name:ident $_field:tt;) => {
1169 newtype_fmt! { UpperHex, $name }
1170 };
1171 }