Skip to content

Commit 145e008

Browse files
Merge pull request #494 from ehuss/restricted-self-types
Document Rc/Arc method receivers.
2 parents d201abc + 1ec7c04 commit 145e008

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

src/items/associated-items.md

+47-4
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,37 @@ Associated functions whose first parameter is named `self` are called *methods*
9393
and may be invoked using the [method call operator], for example, `x.foo()`, as
9494
well as the usual function call notation.
9595

96-
If the type of the `self` parameter is specified, it is limited to the type
97-
being implemented (or `Self`), or a reference or mutable reference to the
98-
type, or a boxed value of the type being implemented (such as `Box<Self>`).
96+
If the type of the `self` parameter is specified, it is limited to one of the
97+
following types:
98+
99+
- `Self`
100+
- `&Self`
101+
- `&mut Self`
102+
- [`Box<Self>`]
103+
- [`Rc<Self>`]
104+
- [`Arc<Self>`]
105+
- [`Pin<P>`] where `P` is one of the above types except `Self`.
106+
107+
The `Self` term can be replaced with the type being implemented.
108+
109+
```rust
110+
# use std::rc::Rc;
111+
# use std::sync::Arc;
112+
# use std::pin::Pin;
113+
struct Example;
114+
impl Example {
115+
fn by_value(self: Self) {}
116+
fn by_ref(self: &Self) {}
117+
fn by_ref_mut(self: &mut Self) {}
118+
fn by_box(self: Box<Self>) {}
119+
fn by_rc(self: Rc<Self>) {}
120+
fn by_arc(self: Arc<Self>) {}
121+
fn by_pin(self: Pin<&Self>) {}
122+
fn explicit_type(self: Arc<Example>) {}
123+
fn with_lifetime<'a>(self: &'a Self) {}
124+
}
125+
```
126+
99127
Shorthand syntax can be used without specifying a type, which have the
100128
following equivalents:
101129

@@ -107,7 +135,17 @@ Shorthand | Equivalent
107135

108136
> Note: Lifetimes can be and usually are elided with this shorthand.
109137
110-
Consider the following trait:
138+
If the `self` parameter is prefixed with `mut`, it becomes a mutable variable,
139+
similar to regular parameters using a `mut` [identifier pattern]. For example:
140+
141+
```rust
142+
trait Changer: Sized {
143+
fn change(mut self) {}
144+
fn modify(mut self: Box<Self>) {}
145+
}
146+
```
147+
148+
As an example of methods on a trait, consider the following:
111149

112150
```rust
113151
# type Surface = i32;
@@ -294,11 +332,16 @@ fn main() {
294332
[_Lifetime_]: trait-bounds.html
295333
[_Type_]: types.html#type-expressions
296334
[_WhereClause_]: items/generics.html#where-clauses
335+
[`Arc<Self>`]: special-types-and-traits.html#arct
336+
[`Box<Self>`]: special-types-and-traits.html#boxt
337+
[`Pin<P>`]: special-types-and-traits.html#pinp
338+
[`Rc<Self>`]: special-types-and-traits.html#rct
297339
[trait]: items/traits.html
298340
[traits]: items/traits.html
299341
[type aliases]: items/type-aliases.html
300342
[inherent implementations]: items/implementations.html#inherent-implementations
301343
[identifier]: identifiers.html
344+
[identifier pattern]: patterns.html#identifier-patterns
302345
[trait object]: types/trait-object.html
303346
[implementations]: items/implementations.html
304347
[type]: types.html#type-expressions

src/special-types-and-traits.md

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ defined types.
1616
* A trait may be implemented for `Box<T>` in the same crate as `T`, which the
1717
[orphan rules] prevent for other generic types.
1818

19+
## `Rc<T>`
20+
21+
[Methods] can take [`Rc<Self>`] as a receiver.
22+
23+
## `Arc<T>`
24+
25+
[Methods] can take [`Arc<Self>`] as a receiver.
26+
27+
## `Pin<P>`
28+
29+
[Methods] can take [`Pin<P>`] as a receiver.
30+
1931
## `UnsafeCell<T>`
2032

2133
[`std::cell::UnsafeCell<T>`] is used for [interior mutability]. It ensures that
@@ -123,12 +135,15 @@ compile-time; that is, it's not a [dynamically sized type]. [Type parameters]
123135
are `Sized` by default. `Sized` is always implemented automatically by the
124136
compiler, not by [implementation items].
125137

138+
[`Arc<Self>`]: ../std/sync/struct.Arc.html
126139
[`Box<T>`]: ../std/boxed/struct.Box.html
127140
[`Clone`]: ../std/clone/trait.Clone.html
128141
[`Copy`]: ../std/marker/trait.Copy.html
129142
[`Deref`]: ../std/ops/trait.Deref.html
130143
[`DerefMut`]: ../std/ops/trait.DerefMut.html
131144
[`Drop`]: ../std/ops/trait.Drop.html
145+
[`Pin<P>`]: ../std/pin/struct.Pin.html
146+
[`Rc<Self>`]: ../std/rc/struct.Rc.html
132147
[`RefUnwindSafe`]: ../std/panic/trait.RefUnwindSafe.html
133148
[`Send`]: ../std/marker/trait.Send.html
134149
[`Sized`]: ../std/marker/trait.Sized.html

src/variables.md

+1-12
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,10 @@ Local variables are immutable unless declared otherwise. For example:
1111
`let mut x = ...`.
1212

1313
Function parameters are immutable unless declared with `mut`. The `mut` keyword
14-
applies only to the following parameter. For example: `|mut x, y|` and
14+
applies only to the following parameter. For example: `|mut x, y|` and
1515
`fn f(mut x: Box<i32>, y: Box<i32>)` declare one mutable variable `x` and one
1616
immutable variable `y`.
1717

18-
Methods that take either `self` or `Box<Self>` can optionally place them in a
19-
mutable variable by prefixing them with `mut` (similar to regular arguments).
20-
For example:
21-
22-
```rust
23-
trait Changer: Sized {
24-
fn change(mut self) {}
25-
fn modify(mut self: Box<Self>) {}
26-
}
27-
```
28-
2918
Local variables are not initialized when allocated. Instead, the entire frame
3019
worth of local variables are allocated, on frame-entry, in an uninitialized
3120
state. Subsequent statements within a function may or may not initialize the

0 commit comments

Comments
 (0)