README.md
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&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/inherent)
7[<img alt="build status" src="https://img.shields.io/github/workflow/status/dtolnay/inherent/CI/master?style=for-the-badge" height="20">](https://github.com/dtolnay/inherent/actions?query=branch%3Amaster)
8
9This crate provides an attribute macro to make trait methods callable without
10the trait in scope.
11
12```toml
13[dependencies]
14inherent = "0.1"
15```
16
17## Example
18
19```rust
20mod types {
21 use inherent::inherent;
22
23 trait Trait {
24 fn f(self);
25 }
26
27 pub struct Struct;
28
29 #[inherent(pub)]
30 impl Trait for Struct {
31 fn f(self) {}
32 }
33}
34
35fn main() {
36 // types::Trait is not in scope, but method can be called.
37 types::Struct.f();
38}
39```
40
41Without the `inherent` macro on the trait impl, this would have failed with the
42following error:
43
44```console
45error[E0599]: no method named `f` found for type `types::Struct` in the current scope
46 --> src/main.rs:18:19
47 |
488 | pub struct Struct;
49 | ------------------ method `f` not found for this
50...
5118 | 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
59The `inherent` macro expands to inherent methods on the `Self` type of the trait
60impl that forward to the trait methods. In the case above, the generated code
61would be:
62
63```rust
64impl Struct {
65 pub fn f(self) {
66 <Self as Trait>::f(self)
67 }
68}
69```
70
71## Visibility
72
73Ordinary trait methods have the same visibility as the trait or the `Self` type,
74whichever's is smaller. Neither of these visibilities is knowable to the
75`inherent` macro, so we simply emit all inherent methods as private by default.
76A different visibility may be specified explicitly in the `inherent` macro
77invocation.
78
79```rust
80#[inherent] // private inherent methods are the default
81
82#[inherent(pub)] // all methods pub
83
84#[inherent(crate)] // all methods pub(crate)
85
86#[inherent(in path::to)] // all methods pub(in path::to)
87```
88
89<br>
90
91#### License
92
93<sup>
94Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
952.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
96</sup>
97
98<br>
99
100<sub>
101Unless you explicitly state otherwise, any contribution intentionally submitted
102for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
103be dual licensed as above, without any additional terms or conditions.
104</sub>
105