GitXplorerGitXplorer
t

easy-ext

public
48 stars
2 forks
3 issues

Commits

List of commits on branch main.
Unverified
8a8c4e7805e69286c5e9a2c07e3a09b4728c792c

Reduce allocations

ttaiki-e committed 3 months ago
Unverified
75b5c950cc823349b3cf4b98832913a4c8244993

Address clippy::ref_option warning

ttaiki-e committed 3 months ago
Unverified
8d2b68ba0cee1d8299e606ead1aceea0847d4ed6

Remove punct helper function

ttaiki-e committed 3 months ago
Unverified
91745f61a9ba4fc954b9e1b81f4c36674d67ea71

tools: Update tidy.sh

ttaiki-e committed 4 months ago
Unverified
b9c6669a96112926d715a482f685cd8e408d7e06

ci: Use taiki-e/github-actions/install-rust action

ttaiki-e committed 4 months ago
Unverified
04ef1ad55dbaf76f6a9e863f91b03a27b6b3f3a0

Fix clippy::used_underscore_items warning

ttaiki-e committed 4 months ago

README

The README file for this repository.

easy-ext

crates.io docs.rs license msrv github actions

A lightweight attribute macro for easily writing extension trait pattern.

[dependencies]
easy-ext = "1"

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.

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]
impl<T> T {
    fn method(&self) {}
}
use easy_ext::ext;

#[ext]
impl<T> T {
    const MSG: &'static str = "Hello!";
}
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.