Skip to content

Commit

Permalink
Update from review.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Feb 6, 2020
1 parent b3d68ee commit f183df7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
25 changes: 14 additions & 11 deletions src/items/associated-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,28 @@ Associated functions whose first parameter is named `self` are called *methods*
and may be invoked using the [method call operator], for example, `x.foo()`, as
well as the usual function call notation.

If the type of the `self` parameter is specified, it is limited to one of the
following types:
If the type of the `self` parameter is specified, it is limited to semantic
types generated by the following grammar (where `'lt` denotes some arbitrary
lifetime):

- `Self`
- `&Self`
- `&mut Self`
- [`Box<Self>`]
- [`Rc<Self>`]
- [`Arc<Self>`]
- [`Pin<P>`] where `P` is one of the above types except `Self`.
```text
P = &'lt S | &'lt mut S | Box<S> | Rc<S> | Arc<S> | Pin<P>
S = Self | P
```

The `Self` term can be replaced with the type being implemented, including
type aliases for the type, or any nested combination of the above types.
The `Self` terminal in this grammar is the semantic `Self` type and can be
replaced with the type being implemented, including type aliases or associated
type projections for the type.

```rust
# use std::rc::Rc;
# use std::sync::Arc;
# use std::pin::Pin;
// Examples of methods implemented on struct `Example`.
struct Example;
type Alias = Example;
trait Trait { type Output; }
impl Trait for Example { type Output = Example; }
impl Example {
fn by_value(self: Self) {}
fn by_ref(self: &Self) {}
Expand All @@ -129,6 +131,7 @@ impl Example {
fn explicit_type(self: Arc<Example>) {}
fn with_lifetime<'a>(self: &'a Self) {}
fn nested<'a>(self: &mut &'a Arc<Rc<Box<Alias>>>) {}
fn via_projection(self: <Example as Trait>::Output) {}
}
```

Expand Down
5 changes: 2 additions & 3 deletions src/items/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ trait TraitMethods {
```

```rust,compile_fail
// These are object-safe, but cannot be dispatched on a trait object.
// This trait is object-safe, but these methods cannot be dispatched on a trait object.
trait NonDispatchable {
// Non-methods cannot be dispatched.
fn foo() where Self: Sized {}
Expand All @@ -135,7 +135,6 @@ trait NonDispatchable {
// `other` may be a different concrete type of the receiver.
fn param(&self, other: Self) where Self: Sized {}
// Generics are not compatible with vtables.
// Alternate solution is to use a trait object instead.
fn typed<T>(&self, x: T) where Self: Sized {}
}
Expand Down Expand Up @@ -178,7 +177,7 @@ let obj: Box<dyn TraitWithSize> = Box::new(S); // ERROR
```

```rust,compile_fail
// Not object safe if `Self` is a type parameter.
// Not object safe if `Self` is a type argument.
trait Super<A> {}
trait WithSelf: Super<Self> where Self: Sized {}
Expand Down

0 comments on commit f183df7

Please sign in to comment.