[][src]Attribute Macro easy_ext::ext

#[ext]

An attribute macro for easily writing extension trait pattern.

Examples

use easy_ext::ext;

#[ext(ResultExt)]
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:

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. Note that in this case, #[ext] assigns a random name, so you cannot import/export the generated trait.

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

Visibility

  • The generated extension trait inherits the visibility of the item in the original impl.

  • The visibility of all the items in the original impl must be identical.

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

use easy_ext::ext;

#[ext(Ext)]
impl<T> T {
    fn method(&self) {}
}
use easy_ext::ext;

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