GitPedia

Easy ext

A lightweight attribute macro for easily writing extension trait pattern.

From taiki-e·Updated June 18, 2026·View on GitHub·

A lightweight attribute macro for easily writing [extension trait pattern][rfc0445]. The project is written primarily in Rust, distributed under the Apache License 2.0 license, first published in 2019. Key topics include: no-std, proc-macro, rust.

Latest release: v1.0.31.0.3
March 3, 2026View Changelog →

easy-ext

crates.io
docs.rs
license
msrv
github actions

<!-- tidy:sync-markdown-to-rustdoc:start:src/lib.rs -->

A lightweight attribute macro for easily writing extension trait pattern.

toml
[dependencies] easy-ext = "1"

Examples

rust
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:

rust
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.

rust
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:

rust
use easy_ext::ext; #[ext(StrExt)] // generate `pub trait StrExt` pub impl str { fn foo(&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:

rust
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.

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

Supported items

Associated functions (methods)

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

Associated constants

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

Associated types

rust
use easy_ext::ext; #[ext] impl str { type Owned = String; fn method(&self) -> Self::Owned { self.to_owned() } }
<!-- tidy:sync-markdown-to-rustdoc:end -->

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.

Contributors

Showing top 3 contributors by commit count.

View all contributors on GitHub →

This article is auto-generated from taiki-e/easy-ext via the GitHub API.Last fetched: 6/26/2026