impl_scope!() { /* proc-macro */ }
Expand description

Scope supporting impl Self and advanced attribute macros

This macro has three raisons d’être:

  • To support impl Self syntax
  • To allow field initializers, as used by impl_default
  • To allow user-defined attribute macros to read/write other impls within the impl_scope

Caveat: rustfmt can not yet format contents (see rustfmt#5254).


ImplScope :
   impl_scope! { ScopeItem ItemImpl * }

ScopeItem :
   ItemEnum | ItemStruct | ItemType | ItemUnion

That is, one type definition followed by a set of implementations. Impls must take one of two forms:

  • impl Self { ... } — generic parameters and bounds of the type are used
  • impl MyType { ... } where MyType matches the name of the defined type

Generic parameters from the type are included implicitly with the first form. Additional generic parameters and where clauses are supported (parameters and bounds are merged).


impl_tools::impl_scope! {
    struct Pair<T>(T, T);

    impl Self {
        pub fn new(a: T, b: T) -> Self {
            Pair(a, b)

    impl Self where T: Clone {
        pub fn splat(a: T) -> Self {
            let b = a.clone();
            Pair(a, b)