Skip to content

Commit 671be06

Browse files
Merge pull request #275 from alercah/methods
More cleanups related to methods.
2 parents 7629634 + 025a9fd commit 671be06

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

src/expressions/method-call-expr.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
A _method call_ consists of an expression (the *receiver*) followed by a single
44
dot, an [identifier], and a parenthesized expression-list. Method calls are
5-
resolved to associated methods of specific types, either statically dispatching
6-
to a method if the exact `self`-type of the left-hand-side is known, or
7-
dynamically dispatching if the left-hand-side expression is an indirect [trait
8-
object](types.html#trait-objects).
5+
resolved to associated [methods] on specific traits, either statically
6+
dispatching to a method if the exact `self`-type of the left-hand-side is known,
7+
or dynamically dispatching if the left-hand-side expression is an indirect
8+
[trait object](types.html#trait-objects).
99

1010
```rust
1111
let pi: Result<f32, _> = "3.14".parse();
@@ -95,3 +95,4 @@ and function invocation.
9595
[trait objects]: types.html#trait-objects
9696
[disambiguate call]: expressions/call-expr.html#disambiguating-function-calls
9797
[dereference]: expressions/operator-expr.html#the-dereference-operator
98+
[methods]: items/associated-items.html#methods

src/items/associated-items.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,19 @@ let _: f64 = <f64 as Num>::from_i32(42);
7777
let _: f64 = f64::from_i32(42);
7878
```
7979

80+
### Methods
81+
8082
Associated functions whose first parameter is named `self` are called *methods*
8183
and may be invoked using the [method call operator], for example, `x.foo()`, as
8284
well as the usual function call notation.
8385

84-
When the first parameter is named `self`, the following shorthands may be used.
86+
The `self` parameter must have one of the following types. As a result, the
87+
following shorthands may be used to declare `self`:
8588

8689
* `self` -> `self: Self`
8790
* `&'lifetime self` -> `self: &'lifetime Self`
8891
* `&'lifetime mut self` -> `self: &'lifetime mut Self`
92+
* `self : Box<Self>` (no shorthand)
8993

9094
> Note: Lifetimes can be and usually are elided with this shorthand.
9195

src/items/functions.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ as parameters, through which the caller passes arguments into the function, and
77
the *output* [*type*][type] of the value the function will return to its caller
88
on completion.
99

10-
[block]: expressions/block-expr.html
11-
[variables]: variables.html
12-
[type]: types.html
13-
1410
When referred to, a _function_ yields a first-class *value* of the
1511
corresponding zero-sized [*function item type*], which
1612
when called evaluates to a direct call to the function.
1713

18-
[*function item type*]: types.html#function-item-types
19-
2014
For example, this is a simple function:
2115
```rust
2216
fn answer_to_life_the_universe_and_everything() -> i32 {
@@ -61,7 +55,7 @@ fn foo<A, B>(x: A, y: B) {
6155
```
6256

6357
Inside the function signature and body, the name of the type parameter can be
64-
used as a type name. [Trait](items/traits.html) bounds can be specified for type
58+
used as a type name. [Trait] bounds can be specified for type
6559
parameters to allow methods with that trait to be called on values of that
6660
type. This is specified using the `where` syntax:
6761

@@ -91,8 +85,6 @@ component after the function name. This might be necessary if there is not
9185
sufficient context to determine the type parameters. For example,
9286
`mem::size_of::<u32>() == 4`.
9387

94-
[path]: paths.html
95-
9688
## Extern functions
9789

9890
Extern functions are part of Rust's foreign function interface, providing the
@@ -124,3 +116,9 @@ of an extern function will cause the process to abort. In LLVM, this is
124116
implemented by executing an illegal instruction.
125117

126118
[external blocks]: items/external-blocks.html
119+
[path]: paths.html
120+
[block]: expressions/block-expr.html
121+
[variables]: variables.html
122+
[type]: types.html
123+
[*function item type*]: types.html#function-item-types
124+
[Trait]: items/traits.html

src/items/traits.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ other traits and so forth as usual.
1414

1515
Traits are implemented for specific types through separate [implementations].
1616

17+
Items associated with a trait do not need to be defined in the trait, but they
18+
may be. If the trait provides a definition, then this definition acts as a
19+
default for any implementation which does not override it. If it does not, then
20+
any implementation must provide a definition.
21+
1722
## Trait bounds
1823

1924
Generic functions may use traits as _bounds_ on their type parameters. This
2025
will have three effects:
2126

2227
- Only types that have the trait may instantiate the parameter.
23-
- Within the generic function, the methods of the trait can be called on values
24-
that have the parameter's type. Associated types can be used in the
28+
- Within the generic function, the functions of the trait can be called on
29+
values that have the parameter's type. Associated types can be used in the
2530
function's signature, and associated constants can be used in expressions
2631
within the function body.
2732
- Generic functions and types with the same or weaker bounds can use the
@@ -63,11 +68,10 @@ Object safe traits can be the base trait of a [trait object]. A trait is
6368
*object safe* if it has the following qualities (defined in [RFC 255]):
6469

6570
* It must not require `Self: Sized`
66-
* All associated functions must either have a `where Self: Sized` bound or
67-
* Not have any type parameters (although lifetime parameters are allowed)
68-
* Must be a method: its first parameter must be called self, with type
69-
`Self`, `&Self`, `&mut Self`, `Box<Self>`.
70-
* `Self` may only be used in the type of the receiver.
71+
* All associated functions must either have a `where Self: Sized` bound, or
72+
* Not have any type parameters (although lifetime parameters are allowed),
73+
and
74+
* Be a [method] that does not use `Self` except in the type of the receiver.
7175
* It must not have any associated constants.
7276

7377
## Supertraits
@@ -142,6 +146,6 @@ let nonsense = mycircle.radius() * mycircle.area();
142146
[`RefUnwindSafe`]: ../std/panic/trait.RefUnwindSafe.html
143147
[trait object]: types.html#trait-objects
144148
[explicit]: expressions/operator-expr.html#type-cast-expressions
145-
[methods called]: expressions/method-call-expr.html
146149
[RFC 255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
147-
[associated items]: items/associated-items.html
150+
[associated items]: items/associated-items.html
151+
[method]: items/functions.html#methods

0 commit comments

Comments
 (0)