#extension #trait #macros #attribute

macro no-std easy-ext

An attribute macro for easily writing extension trait pattern

17 releases (1 stable)

1.0.0 Aug 24, 2021
0.2.9 Jul 3, 2021
0.2.8 Jun 23, 2021
0.2.7 Mar 25, 2021
0.1.4 Mar 10, 2019

#927 in Rust patterns

Download history 3518/week @ 2022-03-10 4080/week @ 2022-03-17 3784/week @ 2022-03-24 2204/week @ 2022-03-31 3766/week @ 2022-04-07 3574/week @ 2022-04-14 3710/week @ 2022-04-21 3104/week @ 2022-04-28 3928/week @ 2022-05-05 3734/week @ 2022-05-12 4713/week @ 2022-05-19 4597/week @ 2022-05-26 4736/week @ 2022-06-02 4034/week @ 2022-06-09 4251/week @ 2022-06-16 3828/week @ 2022-06-23

17,781 downloads per month
Used in 43 crates (12 directly)

Apache-2.0 OR MIT

86KB
2K SLoC

easy-ext

crates.io docs.rs license rustc build status

An attribute macro for easily writing extension trait pattern.

[dependencies]
easy-ext = "1"

Compiler support: requires rustc 1.31+

Examples

use easy_ext::ext;

#[ext(ResultExt)]
pub impl<T, E> Result<T, E> {
    fn err_into<U>(self) -> Result<T, U>
    where
        E: Into<U>,
    {
        self.map_err(Into::into)
    }
}

Code like this will be generated:

pub trait ResultExt<T, E> {
    fn err_into<U>(self) -> Result<T, U>
    where
        E: Into<U>;
}

impl<T, E> ResultExt<T, E> for Result<T, E> {
    fn err_into<U>(self) -> Result<T, U>
    where
        E: Into<U>,
    {
        self.map_err(Into::into)
    }
}

You can elide the trait name.

use easy_ext::ext;

#[ext]
impl<T, E> Result<T, E> {
    fn err_into<U>(self) -> Result<T, U>
    where
        E: Into<U>,
    {
        self.map_err(Into::into)
    }
}

Note that in this case, #[ext] assigns a random name, so you cannot import/export the generated trait.

Visibility

There are two ways to specify visibility.

Impl-level visibility

The first way is to specify visibility at the impl level. For example:

use easy_ext::ext;

// unnamed
#[ext]
pub impl str {
    fn foo(&self) {}
}

// named
#[ext(StrExt)]
pub impl str {
    fn bar(&self) {}
}

Associated-item-level visibility

Another way is to specify visibility at the associated item level.

For example, if the method is pub then the trait will also be pub:

use easy_ext::ext;

#[ext(ResultExt)] // generate `pub trait ResultExt`
impl<T, E> Result<T, E> {
    pub fn err_into<U>(self) -> Result<T, U>
    where
        E: Into<U>,
    {
        self.map_err(Into::into)
    }
}

This is useful when migrate from an inherent impl to an extension trait.

Note that the visibility of all the associated items in the impl must be identical.

Note that you cannot specify impl-level visibility and associated-item-level visibility at the same time.

Supertraits

If you want the extension trait to be a subtrait of another trait, add Self: SubTrait bound to the where clause.

use easy_ext::ext;

#[ext(Ext)]
impl<T> T
where
    Self: Default,
{
    fn method(&self) {}
}

Supported items

Associated functions (methods)

use easy_ext::ext;

#[ext]
impl<T> T {
    fn method(&self) {}
}

Associated constants

use easy_ext::ext;

#[ext]
impl<T> T {
    const MSG: &'static str = "Hello!";
}

Associated types

use easy_ext::ext;

#[ext]
impl str {
    type Owned = String;

    fn method(&self) -> Self::Owned {
        self.to_owned()
    }
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps