Bug 1902540 - Crashtest. r=dholbert
[gecko.git] / third_party / rust / inherent / README.md
blob8921d595ab1e08936ad76541bc23976ee781e7ce
1 \#\[inherent\]
2 ==============
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)
9 This crate provides an attribute macro to make trait methods callable without
10 the trait in scope.
12 ```toml
13 [dependencies]
14 inherent = "1.0"
15 ```
17 ## Example
19 ```rust
20 mod types {
21     use inherent::inherent;
23     trait Trait {
24         fn f(self);
25     }
27     pub struct Struct;
29     #[inherent]
30     impl Trait for Struct {
31         pub fn f(self) {}
32     }
35 fn main() {
36     // types::Trait is not in scope, but method can be called.
37     types::Struct.f();
39 ```
41 Without the `inherent` macro on the trait impl, this would have failed with the
42 following error:
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 ```
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:
63 ```rust
64 impl Struct {
65     pub fn f(self) {
66         <Self as Trait>::f(self)
67     }
69 ```
71 <br>
73 #### License
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>
80 <br>
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>