| 1 |
\#\[inherent\] |
| 2 |
============== |
| 3 |
|
| 4 |
[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/inherent-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/inherent) |
| 5 |
[<img alt="crates.io" src="https://img.shields.io/crates/v/inherent.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/inherent) |
| 6 |
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-inherent-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/inherent) |
| 7 |
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/dtolnay/inherent/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/dtolnay/inherent/actions?query=branch%3Amaster) |
| 8 |
|
| 9 |
This crate provides an attribute macro to make trait methods callable without |
| 10 |
the trait in scope. |
| 11 |
|
| 12 |
```toml |
| 13 |
[dependencies] |
| 14 |
inherent = "1.0" |
| 15 |
``` |
| 16 |
|
| 17 |
## Example |
| 18 |
|
| 19 |
```rust |
| 20 |
mod types { |
| 21 |
use inherent::inherent; |
| 22 |
|
| 23 |
trait Trait { |
| 24 |
fn f(self); |
| 25 |
} |
| 26 |
|
| 27 |
pub struct Struct; |
| 28 |
|
| 29 |
#[inherent] |
| 30 |
impl Trait for Struct { |
| 31 |
pub fn f(self) {} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
fn main() { |
| 36 |
// types::Trait is not in scope, but method can be called. |
| 37 |
types::Struct.f(); |
| 38 |
} |
| 39 |
``` |
| 40 |
|
| 41 |
Without the `inherent` macro on the trait impl, this would have failed with the |
| 42 |
following error: |
| 43 |
|
| 44 |
```console |
| 45 |
error[E0599]: no method named `f` found for type `types::Struct` in the current scope |
| 46 |
--> src/main.rs:18:19 |
| 47 |
| |
| 48 |
8 | pub struct Struct; |
| 49 |
| ------------------ method `f` not found for this |
| 50 |
... |
| 51 |
18 | types::Struct.f(); |
| 52 |
| ^ |
| 53 |
| |
| 54 |
= help: items from traits can only be used if the trait is implemented and in scope |
| 55 |
= note: the following trait defines an item `f`, perhaps you need to implement it: |
| 56 |
candidate #1: `types::Trait` |
| 57 |
``` |
| 58 |
|
| 59 |
The `inherent` macro expands to inherent methods on the `Self` type of the trait |
| 60 |
impl that forward to the trait methods. In the case above, the generated code |
| 61 |
would be: |
| 62 |
|
| 63 |
```rust |
| 64 |
impl Struct { |
| 65 |
pub fn f(self) { |
| 66 |
<Self as Trait>::f(self) |
| 67 |
} |
| 68 |
} |
| 69 |
``` |
| 70 |
|
| 71 |
<br> |
| 72 |
|
| 73 |
#### License |
| 74 |
|
| 75 |
<sup> |
| 76 |
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version |
| 77 |
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. |
| 78 |
</sup> |
| 79 |
|
| 80 |
<br> |
| 81 |
|
| 82 |
<sub> |
| 83 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 84 |
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall |
| 85 |
be dual licensed as above, without any additional terms or conditions. |
| 86 |
</sub> |