#macro-derive #macro #derive #syntax

yanked desugar-impl

Sugar for less explicit generics in structs, enums, and union declarations

0.1.1 Jul 29, 2021
0.1.0 Jul 29, 2021

#181 in #macros

ISC OR MIT OR Apache-2.0

8KB
90 lines

impl Trait not allowed outside of function and method return types

… but it is now!

This library gives you one macro, and one macro only: #[desugar_impl].

Annotate any struct, enum, or union with #[desugar_impl] to allow the use of field_name: impl SomeTrait in their declaration. E.g.

#[desugar_impl]
struct Test {
    a: impl Clone + PartialOrd,
    b: impl Clone + PartialOrd,
    c: impl Hash,
}

desugars to

struct Test<Ty1, Ty2, Ty3>
where
    Ty1: Clone + PartialOrd,
    Ty2: Clone + PartialOrd,
    Ty3: Hash,
{
    a: Ty1,
    b: Ty2,
    c: Ty3,
}

You can still place any #[derive()] macros just below #[desugar_impl] any they see work with the desugared code.


lib.rs:

impl Trait not allowed outside of function and method return types

… but it is now!

This library gives you one macro, and one macro only: [#[desugar_impl]][macro@desugar_impl].

Annotate any struct, enum, or union with [#[desugar_impl]][macro@desugar_impl] to allow the use of field_name: impl SomeTrait in their declaration. E.g.

#[desugar_impl::desugar_impl]
struct Test {
    a: impl Clone + PartialOrd,
    b: impl Clone + PartialOrd,
    c: impl Copy,
}

desugars to

struct Test<Ty1, Ty2, Ty3>
where
    Ty1: Clone + PartialOrd,
    Ty2: Clone + PartialOrd,
    Ty3: Copy,
{
    a: Ty1,
    b: Ty2,
    c: Ty3,
}

You can still place any #[derive()] macros just below #[desugar_impl] any they see work with the desugared code.

Dependencies

~1.5MB
~36K SLoC