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 1 commit
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
19 changes: 14 additions & 5 deletions src/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,25 @@ 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 with the exception that all
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be clearer as separate sentences instead of "with the exception that".

traits except the first trait must be auto traits and there may not be more than
one lifetime. For example, given a trait `Trait`, the following
Copy link
Contributor

@matthewjasper matthewjasper Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and ? bounds ("opt-out bounds"?) can't be used.

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 +708,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