Declare and Implement Extension Trait Once

Currently, a common pattern in Rust is "extension traits" used to extend some external type with an internal trait. For example:

use some_lib::SomeType;

trait Extension {
    fn foo(self);
}

impl Extension for &SomeType {
    fn foo(self) { ... }
}

The main issue here is the function signature and definition are always duplicated, which violates DRY.

I propose a new simplified syntax to declare and implement extension traits in one place:

use some_lib::SomeType;

trait Extension for &SomeType {
    fn foo(self) {
        /* self: &SomeType */
    }
}

A syntax similar to this can already be achieved using ex_trait crate.

Here's a previous thread:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.