You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With today's `trait_alias`, it wouldn't make much difference for Joe. He would
134
-
just get a slightly different error message:
168
+
With today's `trait_alias`, it wouldn't make much difference for `downstream`.
169
+
`impl` blocks for `Frob` would still be broken.
135
170
136
-
```
137
-
error[E0404]: expected trait, found trait alias `Frobber`
138
-
--> src/lib.rs:6:6
139
-
|
140
-
6 | impl Frobber for MyType {
141
-
| ^^^^^^^ not a trait
142
-
```
143
-
144
-
## Speculative example: GATification of `Iterator`
171
+
## (Speculative) GATification of `Iterator`
145
172
146
173
*This example relies on some language features that are currently pure
147
174
speculation. Implementable trait aliases are potentially necessary to support
@@ -160,7 +187,7 @@ parameters. One could then define `Iterator` in terms of `LendingIterator`, like
160
187
so:
161
188
162
189
```rust
163
-
//! core::iter
190
+
//! `core::iter`
164
191
pubtraitLendingIterator {
165
192
typeItem<'a>
166
193
where
@@ -179,58 +206,56 @@ But, as with the previous example, we are foiled by the fact that trait aliases
179
206
aren't `impl`ementable, so this change would break every `impl Iterator` block
180
207
in existence.
181
208
182
-
## Speculative example:`Async` trait
209
+
## (Speculative)`Async` trait
183
210
184
211
There has been some discussion about a variant of the `Future` trait with an
185
-
`unsafe` poll method, to support structured concurrency ([here](https://rust-lang.github.io/wg-async/vision/roadmap/scopes/capability/variant_async_trait.html)
186
-
for example). *If* such a change ever happens, then the same "weak"/"strong"
187
-
relationship will arise: the safe-to-poll `Future` trait would be a "strong"
188
-
version of the unsafe-to-poll `Async`. As the linked design notes explain, there
189
-
are major problems with expressing that relationship in today's Rust.
212
+
`unsafe` poll method, to support structured concurrency ([wg-async design notes](https://rust-lang.github.io/wg-async/vision/roadmap/scopes/capability/variant_async_trait.html)).
213
+
*If* such a change ever happens, then the same "weak"/"strong" relationship will
214
+
arise: the safe-to-poll `Future` trait would be a "strong" version of the
215
+
unsafe-to-poll `Async`. As the linked design notes explain, there are major
216
+
problems with expressing that relationship in today's Rust.
190
217
191
218
# Guide-level explanation
192
219
193
220
With `#![feature(trait_alias)]` (RFC #1733), one can define trait aliases, for
194
221
use in bounds, trait objects, and `impl Trait`. This feature additionally allows
195
222
writing `impl` blocks for a subset of trait aliases.
196
223
197
-
Let's rewrite our AFIT example from before, in terms of this feature. Here's
198
-
what it looks like now:
224
+
Let's rewrite our AFIT example from before using this feature. Here's what it
225
+
looks like now:
199
226
200
227
```rust
201
-
//! crate frob-lib
228
+
//! crate `frob-lib`
202
229
#![feature(trait_alias)]
203
230
204
-
pubtraitLocalFrobber {
231
+
pubtraitLocalFrob {
205
232
asyncfnfrob(&self);
206
233
}
207
234
208
-
pubtraitFrobber=LocalFrobber<frob(..):Send>
235
+
pubtraitFrob=LocalFrob<frob(..):Send>
209
236
where
210
237
// not `+ Send`!
211
238
Self:Send;
212
239
```
213
240
214
241
```rust
215
-
//! crate joes-crate
242
+
//! crate `downstream`
216
243
#![feature(trait_alias_impl)]
217
244
218
-
usefrob_lib::Frobber;
245
+
usefrob_lib::Frob;
219
246
220
247
structMyType;
221
248
222
-
implFrobberforMyType {
223
-
asyncfnfrob(&self) {
224
-
println!("Sloo is 120% klutzed. Initiating brop sequence...")
225
-
}
249
+
implFrobforMyType {
250
+
asyncfnfrob(&self) { /* ... */ }
226
251
}
227
252
```
228
253
229
-
Joe's original code Just Works.
254
+
`impl`s of `Frob` now Just Work.
230
255
231
256
The rule of thumb is: if you can copy everything between the `=` and `;` of a
232
257
trait alias, paste it between the `for` and `{` of a trait `impl` block, and the
233
-
result is syntactically valid—then the trait alias is most likely implementable.
258
+
result is syntactically valid—then the trait alias is implementable.
234
259
235
260
# Reference-level explanation
236
261
@@ -323,7 +348,6 @@ struct Baz;
323
348
implIntIteratorforBaz {
324
349
// The alias constrains `Self::Item` to `i32`, so we don't need to specify it
325
350
// (though we are allowed to do so if desired).
326
-
// type Item = i32;
327
351
328
352
fnnext(&mutself) ->i32 {
329
353
-27
@@ -359,28 +383,26 @@ Alias `impl`s also allow omitting implied `#[refine]`s:
0 commit comments