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
For example, `trait Foo<T> = PartialEq<T> where Self: Sync;` is a valid implementable alias. The `=` must be followed by a single trait (or implementable trait alias), and then some number of where clauses.
204
+
For example, `trait Foo<T> = PartialEq<T> where Self: Sync;` is a valid implementable alias. The `=` must be followed by a single trait (or implementable trait alias), and then some number of where clauses. The trait's generic parameter list may contain associated type constraints (for example `trait IntIterator = Iterator<Item = u32>`).
205
+
206
+
There is another restriction that trait aliases must adhere to in order to be implementable: all generic parameters of the alias itself must be used as generic parameters of the alias's primary trait.
207
+
208
+
```rust
209
+
// Implementable
210
+
traitFoo<T> =PartialEq<T>;
211
+
212
+
// Not implementable
213
+
traitFoo<T> =Copy;
214
+
traitFoo<T> =CopywhereT:Send;
215
+
traitFoo<T> =Iterator<Item=T>;
216
+
traitFoo<T> =CopywhereSelf:PartialEq<T>;
217
+
```
218
+
219
+
## Usage in `impl` blocks
203
220
204
221
An impl block for a trait alias looks just like an impl block for the underlying trait. The alias's where clauses are treated as if they had been written out in the `impl` header.
205
222
@@ -223,27 +240,14 @@ impl !Send for Bar;
223
240
// impl IntIterator for Bar { /* ... */ }
224
241
```
225
242
226
-
There is another restriction that trait aliases must adhere to in order to be implementable: all generic parameters of the alias itself must be used as generic parameters of the alias's primary trait.
227
-
228
-
```rust
229
-
// Implementable
230
-
traitFoo<T> =PartialEq<T>;
231
-
232
-
// Not implementable
233
-
traitFoo<T> =Copy;
234
-
traitFoo<T> =CopywhereT:Send;
235
-
traitFoo<T> =Iterator<Item=T>;
236
-
traitFoo<T> =CopywhereSelf:PartialEq<T>;
237
-
```
238
-
239
-
Bounds on such generic parameters are enforced at the `impl` site.
243
+
Bounds on generic parameters are also enforced at the `impl` site.
implAlias<*consti32> fori32 {} // Error: `*const i32` is not `Send`
247
251
```
248
252
249
253
If the trait alias uniquely constrains a portion of the `impl` block, that part can be omitted.
@@ -297,21 +301,32 @@ impl Frobber for MyType {
297
301
298
302
Trait aliases are `unsafe` to implement iff the underlying trait is marked `unsafe`.
299
303
304
+
## Usage in paths
305
+
300
306
Implementable trait aliases can also be used with trait-qualified and fully-qualified method call syntax, as well as in paths more generally. When used this way, they are treated equivalently to the underlying primary trait, with the additional restriction that all `where` clauses and type parameter/associated type bounds must be satisfied.
301
307
302
308
```rust
303
309
traitIntIter=Iterator<Item=u32> whereSelf:Clone;
304
310
305
-
fnfoo() {
306
-
letiter= [1_u32].into_iter();
307
-
let_:IntIter::Item=IntIter::next(&mutiter); // works
308
-
let_: <std::array::IntoIterasIntIter>::Item= <std::array::IntoIterasIntIter>::next(); // works
309
-
//IntIter::clone(&iter); // ERROR: trait `Iterator` has no method named `clone()`
310
-
letdyn_iter:&mutdynIterator<Item=u32> =&mutiter;
311
-
//IntIter::next(dyn_iter); // ERROR: `dyn Iterator<Item = u32>` does not implement `Clone`
312
-
letsigned_iter= [1_i32].into_iter();
313
-
//IntIter::next(&mut signed_iter); // ERROR: Expected `<Self as Iterator>::Item` to be `u32`, it is `i32`
314
-
}
311
+
letiter= [1_u32].into_iter();
312
+
let_:IntIter::Item=IntIter::next(&mutiter); // works
313
+
let_: <std::array::IntoIterasIntIter>::Item= <std::array::IntoIterasIntIter>::next(); // works
314
+
//IntIter::clone(&iter); // ERROR: trait `Iterator` has no method named `clone()`
315
+
letdyn_iter:&mutdynIterator<Item=u32> =&mutiter;
316
+
//IntIter::next(dyn_iter); // ERROR: `dyn Iterator<Item = u32>` does not implement `Clone`
317
+
letsigned_iter= [1_i32].into_iter();
318
+
//IntIter::next(&mut signed_iter); // ERROR: Expected `<Self as Iterator>::Item` to be `u32`, it is `i32`
319
+
```
320
+
321
+
Implementable trait aliases can also be used with associated type bounds; the associated type must belong to the alias's primary trait.
0 commit comments