Skip to content

Commit 543a7c7

Browse files
committed
Document Rc/Arc method receivers.
1 parent aa6f004 commit 543a7c7

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/items/associated-items.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,34 @@ 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+
106+
The `Self` portion of the type may be replaced with the type being
107+
implemented.
108+
109+
```rust
110+
# use std::rc::Rc;
111+
# use std::sync::Arc;
112+
struct Example;
113+
impl Example {
114+
fn by_value(self: Self) {}
115+
fn by_ref(self: &Self) {}
116+
fn by_ref_mut(self: &mut Self) {}
117+
fn by_box(self: Box<Self>) {}
118+
fn by_rc(self: Rc<Self>) {}
119+
fn by_arc(self: Arc<Self>) {}
120+
fn explicit_type(self: Arc<Example>) {}
121+
}
122+
```
123+
99124
Shorthand syntax can be used without specifying a type, which have the
100125
following equivalents:
101126

@@ -294,6 +319,9 @@ fn main() {
294319
[_Lifetime_]: trait-bounds.html
295320
[_Type_]: types.html#type-expressions
296321
[_WhereClause_]: items/generics.html#where-clauses
322+
[`Arc<Self>`]: special-types-and-traits.html#arct
323+
[`Box<Self>`]: special-types-and-traits.html#boxt
324+
[`Rc<Self>`]: special-types-and-traits.html#rct
297325
[trait]: items/traits.html
298326
[traits]: items/traits.html
299327
[type aliases]: items/type-aliases.html

src/special-types-and-traits.md

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ 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+
1927
## `UnsafeCell<T>`
2028

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

134+
[`Arc<Self>`]: ../std/sync/struct.Arc.html
126135
[`Box<T>`]: ../std/boxed/struct.Box.html
127136
[`Clone`]: ../std/clone/trait.Clone.html
128137
[`Copy`]: ../std/marker/trait.Copy.html
129138
[`Deref`]: ../std/ops/trait.Deref.html
130139
[`DerefMut`]: ../std/ops/trait.DerefMut.html
131140
[`Drop`]: ../std/ops/trait.Drop.html
141+
[`Rc<Self>`]: ../std/rc/struct.Rc.html
132142
[`RefUnwindSafe`]: ../std/panic/trait.RefUnwindSafe.html
133143
[`Send`]: ../std/marker/trait.Send.html
134144
[`Sized`]: ../std/marker/trait.Sized.html

src/variables.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ 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:
18+
[Methods] that take `self` or one of the supported `Self` types such as
19+
`Box<Self>` can optionally place them in a mutable variable by prefixing them
20+
with `mut` (similar to regular arguments). For example:
2121

2222
```rust
2323
trait Changer: Sized {
@@ -52,3 +52,5 @@ fn initialization_example() {
5252
// uninit_after_if; // err: use of possibly uninitialized `uninit_after_if`
5353
}
5454
```
55+
56+
[Methods]: items/associated-items.html#methods

0 commit comments

Comments
 (0)