Macro enumx::impl_trait[][src]

macro_rules! impl_trait {
    ($(_impl!($($gen:tt),*))* AsRef<$t:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* AsMut<$t:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* Clone _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* DoubleEndedIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* ExactSizeIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* Extend<$a:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* Fn<$args:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* FnMut<$args:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* FnOnce<$args:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* Iterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::error::Error _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::fmt::Debug _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::fmt::Display _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::iter::FusedIterator _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::iter::TrustedLen _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::io::BufRead _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::io::Read _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::io::Seek _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::io::Write _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::ops::Deref _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::ops::DerefMut _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::ops::Generator<$r:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::ops::Index<$idx:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::ops::IndexMut<$idx:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
    ($(_impl!($($gen:tt),*))* std::ops::RangeBounds<$t:ident> _for!($($ty:tt)+) $(_where!($($pred:tt)*))*) => { ... };
}

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

impl_trait! {
    _impl!( Generics ) Path::Of::Trait _for!( Type ) _where!( Clause )
}

Generics and Clause are optional:

impl_trait!{ _impl!() Path::Of::Trait _for!( Type ) _where!() }

and the wrapped macros can be omitted:

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:

impl_trait!{ Path::Of::Trait _for!( Enum![1..=16] )}

The other is the enum type definition copied in _def!() macro:

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:

impl_trait!{ Iterator _for!( Type )}

The example of implementing Fn:

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:

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!().

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)+ }
    };
}