1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! # The predfined macros for implementing traits for enums

/// For frequently used traits in std, this library provides macros such as
/// `impl_trait!()` to implement these traits without the need of writing trait
/// methods.
///
/// ## Syntax of `impl_trait!{}`
///
/// The full form is
///
/// ```text
/// impl_trait! {
///     _impl!( Generics ) Path::Of::Trait _for!( Type ) _where!( Clause )
/// }
/// ```
///
/// `Generics` and `Clause` are optional:
///
/// ```text
/// impl_trait!{ _impl!() Path::Of::Trait _for!( Type ) _where!() }
/// ```
/// and the wrapped macros can be omitted:
///
/// ```text
/// impl_trait!{ Path::Of::Trait _for!( Type )}
/// ```
///
/// ## Supported forms of types in `_for!()`
///
/// The `_for!()` macro supports two forms of types.
///
/// One is ad-hoc enums:
///
///
/// ```text
/// impl_trait!{ Path::Of::Trait _for!( Enum![1..=16] )}
/// ```
///
/// The other is the enum type definition copied in `_def!()` macro:
///
///
/// ```text
/// impl_trait!{ Path::Of::Trait _for!( _def!(
///     enum Value {
///         Bin( Vec<u8> ),
///         Text( String ),
///     }
/// ))}
/// ```
///
/// Note: `_def!()` does not define any enum, so `Value` should have been defined elsewhere.
///
/// ## The `_where!()` macro
///
/// You can write any where clause in this macro.
///
/// Note: you do not need write `_where!( _Variants!(): Path::Of::Trait )` which the
/// `impl_trait!{}` macro will generate it silently.
///
/// ## Traits in std prelude
///
/// `AsRef`
///
/// `AsMut`
///
/// `DoubleEndedIterator`
///
/// `ExactSizeIterator`
///
/// `Extend`
///
/// `Fn`
///
/// `Iterator`
///
/// The example of implementing `Iterator`:
///
/// ```text
/// impl_trait!{ Iterator _for!( Type )}
/// ```
///
/// The example of implementing `Fn`:
///
/// ```text
/// impl_trait!{ _impl!(Args) Fn<Args> _for!( Type )}
/// ```
///
/// ## Traits with full path
///
/// `std::error::Error`
///
/// `std::fmt::Debug`
///
/// `std::fmt::Display`
///
/// `std::iter::FusedIterator`
///
/// `std::iter::TrustedLen`
///
/// `std::io::BufRead`
///
/// `std::io::Read`
///
/// `std::io::Seek`
///
/// `std::io::Write`
///
/// `std::ops::Deref`
///
/// `std::ops::DerefMut`
///
/// `std::ops::Generator`
///
/// `std::ops::Index`
///
/// `std::ops::IndexMut`
///
/// `std::ops::RangeBounds`
///
/// The example of implementing `std::ops::Generator`:
///
/// ```text
/// impl_trait!{ _impl!(R) std::ops::Generator<R> _for!( Type )}
/// ```
///
/// ## Unstable traits
///
/// To implement these traits, the crate feature "unstable" should be opted in.
///
/// `Fn`
///
/// `std::iter::TrustedLen`
///
/// `std::ops::Generator`
///
/// ## macro inheritance
///
/// If the library users want to support extra traits, they can write the extra
/// implementations in their macro, and delegate other traits to
/// `enumx::impl_trait!()`.
///
/// ```rust
/// use enumx::export::{def_impls, impl_all_traits};
///
/// macro_rules! impl_trait {
///     ($(_impl!($($gen:tt),*))* ExtraTrait<$t:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
///         // omitted
///     };
///     ($($tt:tt)+) => {
///         enumx::impl_trait!{ $($tt)+ }
///     };
/// }
///
///
/// macro_rules! impl_super_traits {
///     ($(_impl!($($gen:tt),*))* ExtraTrait<$t:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
///         // omitted
///     };
///     ($($tt:tt)+) => {
///         enumx::impl_super_traits!{ $($tt)+ }
///     };
/// }
/// ```
#[macro_export]
macro_rules! impl_trait {
    ($(_impl!($($gen:tt),*))* AsRef<$t:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> AsRef<$t> for $($ty)+
                where _Variants!(): AsRef<$t>
                      $($(, $pred)*)*
            {
                fn as_ref( &self ) -> &$t { _match!( _variant!().as_ref() )}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* AsMut<$t:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> AsMut<$t> for $($ty)+
                where _Variants!(): AsMut<$t>
                      $($(, $pred)*)*
            {
                fn as_mut( &mut self ) -> &mut $t { _match!( _variant!().as_mut() )}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* Clone _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> Clone for $($ty)+
                where _Variants!(): Clone
                      $($(, $pred)*)*
            {
                fn clone( &self ) -> Self { _match!( _enum!( _variant!().clone() ))}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* DoubleEndedIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Item> DoubleEndedIterator for $($ty)+
                where _Variants!() : DoubleEndedIterator<Item=_Item>
                    , Self         : Iterator<Item=_Item>
                      $($(, $pred)*)*
            {
                fn next_back( &mut self ) -> Option<_Item> {
                    _match!( _variant!().next_back() )
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* ExactSizeIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Item> ExactSizeIterator for $($ty)+
                where _Variants!() : ExactSizeIterator<Item=_Item>
                    , Self         : Iterator<Item=_Item>
                      $($(, $pred)*)*
            {
            }
        }
    };
    ($(_impl!($($gen:tt),*))* Extend<$a:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> Extend<$a> for $($ty)+
                where _Variants!(): Extend<$a>
                      $($(, $pred)*)*
            {
                fn extend<T>( &mut self, iter: T ) where T: IntoIterator<Item=$a> {
                    _match!( _variant!().extend( iter ))
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* Fn<$args:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Output> Fn<$args> for $($ty)+
                where _Variants!() : Fn<$args, Output=_Output>
                    ,         Self : FnMut<$args, Output=_Output>
                      $($(, $pred)*)*
            {
                extern "rust-call" fn call( &self, args: $args ) -> Self::Output {
                    _match!( _variant!().call( args ))
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* FnMut<$args:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Output> FnMut<$args> for $($ty)+
                where _Variants!() : FnMut<$args, Output=_Output>
                    ,         Self : FnOnce<$args, Output=_Output>
                      $($(, $pred)*)*
            {
                extern "rust-call" fn call_mut( &mut self, args: $args ) -> Self::Output {
                    _match!( _variant!().call_mut( args ))
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* FnOnce<$args:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Output> FnOnce<$args> for $($ty)+
                where _Variants!() : FnOnce<$args, Output=_Output>
                      $($(, $pred)*)*
            {
                type Output = _Output;
                extern "rust-call" fn call_once( self, args: $args ) -> Self::Output {
                    _match!( _variant!().call_once( args ))
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* Iterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Item> Iterator for $($ty)+
                where _Variants!(): Iterator<Item=_Item>
                      $($(, $pred)*)*
            {
                type Item = _Item;
                fn next( &mut self ) -> Option<_Item> { _match!( _variant!().next() )}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::error::Error _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> std::error::Error for $($ty)+
                where _Variants!() : std::error::Error
                    ,         Self : std::fmt::Debug
                                   + std::fmt::Display
                      $($(, $pred)*)*
            {
               fn source( &self ) -> Option<&(dyn std::error::Error + 'static)> { _match!( _variant!().source() )}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::fmt::Debug _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> std::fmt::Debug for $($ty)+
                where _Variants!() : std::fmt::Debug
                      $($(, $pred)*)*
            {
                fn fmt( &self, f: &mut std::fmt::Formatter ) -> std::fmt::Result {
                    _match!( _variant!().fmt(f) )
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::fmt::Display _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> std::fmt::Display for $($ty)+
                where _Variants!(): std::fmt::Display
                      $($(, $pred)*)*
            {
                fn fmt( &self, f: &mut std::fmt::Formatter ) -> std::fmt::Result {
                    _match!( _variant!().fmt(f) )
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::iter::FusedIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Item> std::iter::FusedIterator for $($ty)+
                where _Variants!() : std::iter::FusedIterator<Item=_Item>
                    , Self         : Iterator<Item=_Item>
                      $($(, $pred)*)*
            {
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::iter::TrustedLen _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            unsafe impl<$($($gen,)*)* _Item> std::iter::TrustedLen for $($ty)+
                where _Variants!() : std::iter::TrustedLen<Item=_Item>
                    , Self         : Iterator<Item=_Item>
                      $($(, $pred)*)*
            {
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::io::BufRead _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> std::io::BufRead for $($ty)+
                where _Variants!() : std::io::BufRead
                    ,         Self : std::io::Read
                      $($(, $pred)*)*
            {
                fn fill_buf( &mut self ) -> std::io::Result<&[u8]> {
                    _match!( _variant!().fill_buf() )
                }

                fn consume( &mut self, amt: usize ) {
                    _match!( _variant!().consume( amt ))
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::io::Read _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> std::io::Read for $($ty)+
                where _Variants!(): std::io::Read
                      $($(, $pred)*)*
            {
                fn read( &mut self, buf: &mut [u8] ) -> std::io::Result<usize> {
                    _match!( _variant!().read( buf ))
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::io::Seek _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*>  std::io::Seek for $($ty)+
                where _Variants!(): std::io::Seek
                      $($(, $pred)*)*
            {
                fn seek( &mut self, pos: std::io::SeekFrom ) -> std::io::Result<u64> {
                    _match!( _variant!().seek( pos ))
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::io::Write _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> std::io::Write for $($ty)+
                where _Variants!(): std::io::Write
                      $($(, $pred)*)*
            {
                fn write( &mut self, buf: &[u8] ) -> std::io::Result<usize> {
                    _match!( _variant!().write( buf ))
                }

                fn flush( &mut self ) -> std::io::Result<()> {
                    _match!( _variant!().flush() )
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::ops::Deref _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Target> std::ops::Deref for $($ty)+
                where _Variants!(): std::ops::Deref<Target=_Target>
                      $($(, $pred)*)*
            {
                type Target = _Target;
                fn deref( &self ) -> &_Target { _match!( _variant!().deref() )}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::ops::DerefMut _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Target> std::ops::DerefMut for $($ty)+
                where _Variants!() : std::ops::DerefMut<Target=_Target>
                    ,         Self : std::ops::Deref<Target=_Target>
                      $($(, $pred)*)*
            {
                fn deref_mut( &mut self ) -> &mut _Target { _match!( _variant!().deref_mut() )}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::ops::Generator<$r:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Yield, _Return> std::ops::Generator<$r> for $($ty)+
                where _Variants!(): std::ops::Generator<$r,Yield=_Yield,Return=_Return>
                      $($(, $pred)*)*
            {
                type Yield = _Yield;
                type Return = _Return;
                fn resume( self: std::pin::Pin<&mut Self>, arg: $r ) -> std::ops::GeneratorState<Self::Yield, Self::Return> {
                    _match!( unsafe{ self.get_unchecked_mut() } =>
                         unsafe{ std::pin::Pin::new_unchecked( _variant!() )}.resume( arg )
                    )
                }
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::ops::Index<$idx:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Output> std::ops::Index<$idx> for $($ty)+
                where _Variants!(): std::ops::Index<$idx,Output=_Output>
                      $($(, $pred)*)*
            {
                type Output = _Output;
                fn index( &self, index: $idx ) -> &_Output { _match!( _variant!().index( index ))}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::ops::IndexMut<$idx:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)* _Output> std::ops::IndexMut<$idx> for $($ty)+
                where _Variants!() : std::ops::IndexMut<$idx,Output=_Output>
                    ,         Self : std::ops::Index<$idx,Output=_Output>
                      $($(, $pred)*)*
            {
                fn index_mut( &mut self, index: $idx ) -> &mut _Output { _match!( _variant!().index_mut( index ))}
            }
        }
    };
    ($(_impl!($($gen:tt),*))* std::ops::RangeBounds<$t:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        def_impls! {
            impl<$($($gen,)*)*> std::ops::RangeBounds<$t> for $($ty)+
                where _Variants!(): std::ops::RangeBounds<$t>
                      $($(, $pred)*)*
            {
                fn start_bound( &self ) -> std::ops::Bound<&T> { _match!( _variant!().start_bound() )}
                fn end_bound( &self ) -> std::ops::Bound<&T> { _match!( _variant!().end_bound() )}
            }
        }
    };
}

/// The `impl_super_traits!{}` macro helps to implement the super trait(s) of the
/// mentioned trait, e.g. `impl_super_traits!{ _impl!(Args) Fn<Args> _for!( Type )}`
///  will implement `FnMut` and `FnOnce` for `Type`, but **NOT** `Fn`.
#[macro_export]
macro_rules! impl_super_traits {
    ($(_impl!($($gen:tt),*))* DoubleEndedIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* Iterator _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* ExactSizeIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* Iterator _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* Fn<$args:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* FnMut<$args> _for!($($ty)+) $(_where!($($pred)*))*);
        impl_trait!($(_impl!($($gen),*))* FnOnce<$args> _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* FnMut<$args:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* FnOnce<$args> _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* std::error::Error _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* std::fmt::Debug _for!($($ty)+) $(_where!($($pred)*))*);
        impl_trait!($(_impl!($($gen),*))* std::fmt::Display _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* std::iter::FusedIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* Iterator _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* std::iter::TrustedLen _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* Iterator _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* std::io::BufRead _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* std::io::Read _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* std::ops::DerefMut _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* std::ops::Deref _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($(_impl!($($gen:tt),*))* std::ops::IndexMut _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => {
        impl_trait!($(_impl!($($gen),*))* std::ops::Index _for!($($ty)+) $(_where!($($pred)*))*);
    };
    ($($_tt:tt)+) => {};
}

/// The `impl_all_traits!{}` macro does what `impl_trait!{}` and
/// `impl_super_traits!{}` does, e.g.
/// `impl_all_traits!{ _impl!(Args) Fn<Args> _for!( Type )}` will implement `Fn`,
/// `FnMut` and `FnOnce` for `Type`.
#[macro_export]
macro_rules! impl_all_traits {
    ($($tt:tt)+) => {
        impl_trait!{ $($tt)+ }
        impl_super_traits!{ $($tt)+ }
    };
}