Skip to content

Commit 1ef107a

Browse files
committed
Review comments
1 parent 5e8efd6 commit 1ef107a

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/items/associated-items.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,46 @@ types cannot be defined in [inherent implementations] nor can they be given a
205205
default implementation in traits.
206206

207207
An *associated type declaration* declares a signature for associated type
208-
definitions. It is written as `type`, then an [identifier], and
209-
finally an optional list of trait bounds.
208+
definitions. It is written in one of the following forms, where `Assoc` is the
209+
name of the associated type, `Params` is a comma-separated list of type,
210+
lifetime or const parameters, `Bounds` is a plus-separated list of trait bounds
211+
on the associated type, and `WhereBounds` is a comma-separated list of bounds on
212+
parameters:
213+
214+
```rust,ignore
215+
type Assoc;
216+
type Assoc: Bounds;
217+
type Assoc<Params>;
218+
type Assoc<Params>: Bounds;
219+
type Assoc<Params> where WhereBounds;
220+
type Assoc<Params>: Bounds where WhereBounds;
221+
```
210222

211223
The identifier is the name of the declared type alias. The optional trait bounds
212224
must be fulfilled by the implementations of the type alias.
213225
There is an implicit [`Sized`] bound on associated types that can be relaxed using the special `?Sized` bound.
214226

215-
An *associated type definition* defines a type alias on another type. It is
216-
written as `type`, then an [identifier], then an `=`, and finally a [type].
227+
An *associated type definition* defines a type alias on for the implementation
228+
of a trait on a type. They are written similarly to an *associated type declaration*,
229+
but cannot contain `Bounds`, but instead must contain a `Type`:
230+
231+
```rust,ignore
232+
type Assoc = Type;
233+
type Assoc<Params> = Type<Params>;
234+
type Assoc<Params> where WhereBounds = Type;
235+
type Assoc<Params> = Type where WhereBounds;
236+
```
217237

218238
If a type `Item` has an associated type `Assoc` from a trait `Trait`, then
219239
`<Item as Trait>::Assoc` is a type that is an alias of the type specified in the
220240
associated type definition. Furthermore, if `Item` is a type parameter, then
221241
`Item::Assoc` can be used in type parameters.
222242

223-
Associated types may include [generic parameters] or [where clauses]; these may
224-
be referred to as generic associated types, or GATs. If the type `Thing` has an
225-
associated type `Item` from a trait `Trait` with the generics `<'a>` , the type
226-
can be named like `<Thing as Trait>::Item<'x>`, where `'x` is some lifetime in
227-
scope. In this case, `'x` will be used wherever `'a` appears in the associated
243+
Associated types may include [generic parameters] and [where clauses]; these are
244+
often referred to as *generic associated types*, or *GATs*. If the type `Thing`
245+
has an associated type `Item` from a trait `Trait` with the generics `<'a>` , the
246+
type can be named like `<Thing as Trait>::Item<'x>`, where `'x` is some lifetime
247+
in scope. In this case, `'x` will be used wherever `'a` appears in the associated
228248
type definitions on impls.
229249

230250
```rust

src/items/traits.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Object safe traits can be the base trait of a [trait object]. A trait is
7070
* All [supertraits] must also be object safe.
7171
* `Sized` must not be a [supertrait][supertraits]. In other words, it must not require `Self: Sized`.
7272
* It must not have any associated constants.
73+
* It must not have any associated types with generics.
7374
* All associated functions must either be dispatchable from a trait object or be explicitly non-dispatchable:
7475
* Dispatchable functions require:
7576
* Not have any type parameters (although lifetime parameters are allowed),

src/items/type-aliases.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
> _TypeAlias_ :\
55
> &nbsp;&nbsp; `type` [IDENTIFIER]&nbsp;[_GenericParams_]<sup>?</sup>
66
> ( `:` [_TypeParamBounds_] )<sup>?</sup>
7-
> [_WhereClause_]<sup>?</sup> ( `=` [_Type_] )<sup>?</sup> [_WhereClause_]<sup>?</sup> `;`
7+
> [_WhereClause_]<sup>?</sup> ( `=` [_Type_] [_WhereClause_]<sup>?</sup>)<sup>?</sup> `;`
88
99
A _type alias_ defines a new name for an existing [type]. Type aliases are
1010
declared with the keyword `type`. Every value has a single, specific type, but
@@ -34,11 +34,15 @@ let _ = TypeAlias(5); // Doesn't work
3434
A type alias without the [_Type_] specification may only appear as an
3535
[associated type] in a [trait].
3636

37+
A type alias with the [_Type_] specification may only appear as an
38+
[associated type] in a [trait impl].
39+
3740
A type alias with [_TypeParamBounds_] may only specified when used as
3841
an [associated type] in a [trait].
3942

40-
A type alias with where clauses after the equals sign may only appear as an
41-
[associated type] in a [trait] or a [trait impl].
43+
Where clauses before the equals sign on a type alias in a [trait impl] (like
44+
`type TypeAlias<T> where T: Foo = Bar<T>`) are deprecated. Where clauses after
45+
the equals sign (like `type TypeAlias<T> where T: Foo = Bar<T>`) are preferred.
4246

4347
[IDENTIFIER]: ../identifiers.md
4448
[_GenericParams_]: generics.md

0 commit comments

Comments
 (0)