diff --git a/src/items/associated-items.md b/src/items/associated-items.md index dc6c9faef4122..eba9507c7fdfe 100644 --- a/src/items/associated-items.md +++ b/src/items/associated-items.md @@ -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`] -- [`Rc`] -- [`Arc`] -- [`Pin

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

+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) {} @@ -129,6 +131,7 @@ impl Example { fn explicit_type(self: Arc) {} fn with_lifetime<'a>(self: &'a Self) {} fn nested<'a>(self: &mut &'a Arc>>) {} + fn via_projection(self: ::Output) {} } ``` diff --git a/src/items/traits.md b/src/items/traits.md index b2132a7e8c697..1a65c9239b6ad 100644 --- a/src/items/traits.md +++ b/src/items/traits.md @@ -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 {} @@ -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(&self, x: T) where Self: Sized {} } @@ -178,7 +177,7 @@ let obj: Box = 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 {} trait WithSelf: Super where Self: Sized {}