Skip to content

Commit 86b7c82

Browse files
Merge pull request #255 from Havvy/trailing-plus
Trait object syntax + trailing plus
2 parents d0921d9 + 2f16bdf commit 86b7c82

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/types.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,18 +498,26 @@ Because captures are often by reference, the following general rules arise:
498498

499499
## Trait objects
500500

501+
> **<sup>Syntax</sup>**
502+
> _TraitObjectType_ :
503+
> &nbsp;&nbsp; _LifetimeOrPath_ ( `+` _LifetimeOrPath_ )<sup>\*</sup> `+`<sup>?</sup>
504+
>
505+
> _LifetimeOrPath_ :
506+
> &nbsp;&nbsp; [_Path_] | [_LIFETIME_OR_LABEL_]
507+
501508
A *trait object* is an opaque value of another type that implements a set of
502509
traits. The set of traits is made up of an [object safe] *base trait* plus any
503510
number of [auto traits].
504511

505512
Trait objects implement the base trait, its auto traits, and any super traits
506513
of the base trait.
507514

508-
Trait objects are written as the path to the base trait followed by the list
509-
of auto traits followed optionally by a lifetime bound all separated by `+`. For
510-
example, given a trait `Trait`, the following are all trait objects: `Trait`,
511-
`Trait + Send`, `Trait + Send + Sync`, `Trait + 'static`,
512-
`Trait + Send + 'static`.
515+
Trait objects are written the same as trait bounds, but with the following
516+
restrictions. All traits except the first trait must be auto traits, there may
517+
not be more than one lifetime, and opt-out bounds (e.g. `?sized`) are not
518+
allowed. For example, given a trait `Trait`, the following are all trait
519+
objects: `Trait`, `Trait + Send`, `Trait + Send + Sync`, `Trait + 'static`,
520+
`Trait + Send + 'static`, `Trait +`, `'static + Trait`.
513521

514522
Two trait object types alias each other if the base traits alias each other and
515523
if the sets of auto traits are the same and the lifetime bounds are the same.
@@ -644,3 +652,5 @@ impl Printable for String {
644652
[object safe]: items/traits.html#object-safety
645653
[issue 47010]: https://github.com/rust-lang/rust/issues/47010
646654
[issue 33140]: https://github.com/rust-lang/rust/issues/33140
655+
[_PATH_]: paths.html
656+
[_LIFETIME_OR_LABEL_]: tokens.html#lifetimes-and-loop-labels

0 commit comments

Comments
 (0)