Debian Codegraph @

README.md

raw
1 # easy-ext
2
3 [![crates.io](https://img.shields.io/crates/v/easy-ext?style=flat-square&logo=rust)](https://crates.io/crates/easy-ext)
4 [![docs.rs](https://img.shields.io/badge/docs.rs-easy--ext-blue?style=flat-square&logo=docs.rs)](https://docs.rs/easy-ext)
5 [![license](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue?style=flat-square)](#license)
6 [![msrv](https://img.shields.io/badge/msrv-1.31-blue?style=flat-square&logo=rust)](https://www.rust-lang.org)
7 [![github actions](https://img.shields.io/github/actions/workflow/status/taiki-e/easy-ext/ci.yml?branch=main&style=flat-square&logo=github)](https://github.com/taiki-e/easy-ext/actions)
8
9 <!-- tidy:sync-markdown-to-rustdoc:start:src/lib.rs -->
10
11 A lightweight attribute macro for easily writing [extension trait pattern][rfc0445].
12
13 ```toml
14 [dependencies]
15 easy-ext = "1"
16 ```
17
18 ## Examples
19
20 ```rust
21 use easy_ext::ext;
22
23 #[ext(ResultExt)]
24 pub impl<T, E> Result<T, E> {
25 fn err_into<U>(self) -> Result<T, U>
26 where
27 E: Into<U>,
28 {
29 self.map_err(Into::into)
30 }
31 }
32 ```
33
34 Code like this will be generated:
35
36 ```rust
37 pub trait ResultExt<T, E> {
38 fn err_into<U>(self) -> Result<T, U>
39 where
40 E: Into<U>;
41 }
42
43 impl<T, E> ResultExt<T, E> for Result<T, E> {
44 fn err_into<U>(self) -> Result<T, U>
45 where
46 E: Into<U>,
47 {
48 self.map_err(Into::into)
49 }
50 }
51 ```
52
53 You can elide the trait name.
54
55 ```rust
56 use easy_ext::ext;
57
58 #[ext]
59 impl<T, E> Result<T, E> {
60 fn err_into<U>(self) -> Result<T, U>
61 where
62 E: Into<U>,
63 {
64 self.map_err(Into::into)
65 }
66 }
67 ```
68
69 Note that in this case, `#[ext]` assigns a random name, so you cannot
70 import/export the generated trait.
71
72 ### Visibility
73
74 There are two ways to specify visibility.
75
76 #### Impl-level visibility
77
78 The first way is to specify visibility at the impl level. For example:
79
80 ```rust
81 use easy_ext::ext;
82
83 #[ext(StrExt)] // generate `pub trait StrExt`
84 pub impl str {
85 fn foo(&self) {}
86 }
87 ```
88
89 #### Associated-item-level visibility
90
91 Another way is to specify visibility at the associated item level.
92
93 For example, if the method is `pub` then the trait will also be `pub`:
94
95 ```rust
96 use easy_ext::ext;
97
98 #[ext(ResultExt)] // generate `pub trait ResultExt`
99 impl<T, E> Result<T, E> {
100 pub fn err_into<U>(self) -> Result<T, U>
101 where
102 E: Into<U>,
103 {
104 self.map_err(Into::into)
105 }
106 }
107 ```
108
109 This is useful when migrate from an inherent impl to an extension trait.
110
111 Note that the visibility of all the associated items in the `impl` must be identical.
112
113 Note that you cannot specify impl-level visibility and associated-item-level visibility at the same time.
114
115 ### [Supertraits](https://doc.rust-lang.org/reference/items/traits.html#supertraits)
116
117 If you want the extension trait to be a subtrait of another trait,
118 add `Self: SubTrait` bound to the `where` clause.
119
120 ```rust
121 use easy_ext::ext;
122
123 #[ext(Ext)]
124 impl<T> T
125 where
126 Self: Default,
127 {
128 fn method(&self) {}
129 }
130 ```
131
132 ### Supported items
133
134 #### [Associated functions (methods)](https://doc.rust-lang.org/reference/items/associated-items.html#associated-functions-and-methods)
135
136 ```rust
137 use easy_ext::ext;
138
139 #[ext]
140 impl<T> T {
141 fn method(&self) {}
142 }
143 ```
144
145 #### [Associated constants](https://doc.rust-lang.org/reference/items/associated-items.html#associated-constants)
146
147 ```rust
148 use easy_ext::ext;
149
150 #[ext]
151 impl<T> T {
152 const MSG: &'static str = "Hello!";
153 }
154 ```
155
156 #### [Associated types](https://doc.rust-lang.org/reference/items/associated-items.html#associated-types)
157
158 ```rust
159 use easy_ext::ext;
160
161 #[ext]
162 impl str {
163 type Owned = String;
164
165 fn method(&self) -> Self::Owned {
166 self.to_owned()
167 }
168 }
169 ```
170
171 [rfc0445]: https://rust-lang.github.io/rfcs/0445-extension-trait-conventions.html
172
173 <!-- tidy:sync-markdown-to-rustdoc:end -->
174
175 ## License
176
177 Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
178 [MIT license](LICENSE-MIT) at your option.
179
180 Unless you explicitly state otherwise, any contribution intentionally submitted
181 for inclusion in the work by you, as defined in the Apache-2.0 license, shall
182 be dual licensed as above, without any additional terms or conditions.