Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait object syntax + trailing plus #255

Merged
merged 2 commits into from
Mar 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,26 @@ Because captures are often by reference, the following general rules arise:

## Trait objects

> **<sup>Syntax</sup>**
> _TraitObjectType_ :
> &nbsp;&nbsp; _LifetimeOrPath_ ( `+` _LifetimeOrPath_ )<sup>\*</sup> `+`<sup>?</sup>
>
> _LifetimeOrPath_ :
> &nbsp;&nbsp; [_Path_] | [_LIFETIME_OR_LABEL_]

A *trait object* is an opaque value of another type that implements a set of
traits. The set of traits is made up of an [object safe] *base trait* plus any
number of [auto traits].

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

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

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