Skip to content

Commit 13f02d1

Browse files
authored
Merge pull request #146 from Havvy/impl
Implementations improvements part 1
2 parents cf2fbba + a051754 commit 13f02d1

File tree

2 files changed

+109
-26
lines changed

2 files changed

+109
-26
lines changed

Diff for: src/glossary.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ the structure of the program when the compiler is compiling it.
77

88
### Arity
99

10-
Arity refers to the number of arguments a function or operation takes.
11-
For example, `(2, 3)` and `(4, 6)` have arity 2, and`(8, 2, 6)` has arity 3.
10+
Arity refers to the number of arguments a function or operator takes.
11+
For some examples, `f(2, 3)` and `g(4, 6)` have arity 2, while `h(8, 2, 6)`
12+
has arity 3. The `!` operator has arity 1.
1213

1314
### Array
1415

1516
An array, sometimes also called a fixed-size array or an inline array, is a value
1617
describing a collection of elements, each selected by an index that can be computed
1718
at run time by the program. It occupies a contiguous region of memory.
1819

20+
### Associated Item
21+
22+
An associated item is an item that is associated with another item. Associated
23+
items are defined in [implementations] and declared in [traits]. Only functions,
24+
constants, and type aliases can be associated.
25+
1926
### Bound
2027

2128
Bounds are constraints on a type or trait. For example, if a bound
@@ -52,6 +59,11 @@ A variable is initialized if it has been assigned a value and hasn't since been
5259
moved from. All other lvalues are assumed to be initialized. Only unsafe Rust
5360
can create such an lvalue without initializing it.
5461

62+
### Nominal Types
63+
64+
Types that can be referred to by a path directly. Specifically [enums],
65+
[structs], [unions], and [trait objects].
66+
5567
### Prelude
5668

5769
Prelude, or The Rust Prelude, is a small collection of items - mostly traits - that are
@@ -88,6 +100,13 @@ Strings slices are always valid UTF-8.
88100
### Trait
89101

90102
A trait is a language item that is used for describing the functionalities a type must provide.
91-
It allow a type to make certain promises about its behavior.
103+
It allows a type to make certain promises about its behavior.
104+
105+
Generic functions and generic structs can use traits to constrain, or bound, the types they accept.
92106

93-
Generic functions and generic structs can exploit traits to constrain, or bound, the types they accept.
107+
[enums]: items/enumerations.html
108+
[structs]: items/structs.html
109+
[unions]: items/unions.html
110+
[trait objects]: types.html#trait-objects
111+
[implementations]: items/implementations.html
112+
[traits]: items/traits.html

Diff for: src/items/implementations.md

+86-22
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
11
# Implementations
22

3-
An _implementation_ is an item that can implement a [trait](items/traits.html) for a
4-
specific type.
3+
An _implementation_ is an item that associates items with an *implementing type*.
4+
5+
There are two types of implementations: inherent implementations and [trait]
6+
implementations.
57

68
Implementations are defined with the keyword `impl`.
79

10+
## Interent Implementations
11+
12+
An inherent implementation is defined as the sequence of the `impl` keyword,
13+
generic type declarations, a path to a nomial type, a where clause, and a
14+
bracketed set of associable items.
15+
16+
The nominal type is called the *implementing type* and the associable items are
17+
the *associated items* to the implementing type.
18+
19+
Inherent implementations associate the associated items to the implementing
20+
type.
21+
22+
The associated item has a path of a path to the implementing type followed by
23+
the associate item's path component.
24+
25+
Inherent implementations cannot contain associated type aliases.
26+
27+
A type can have multiple inherent implementations.
28+
29+
The implementing type must be defined within the same crate.
30+
31+
```rust
32+
struct Point {x: i32, y: i32}
33+
34+
impl Point {
35+
fn log(&self) {
36+
println!("Point is at ({}, {})", self.x, self.y);
37+
}
38+
}
39+
40+
let my_point = Point {x: 10, y:11};
41+
my_point.log();
42+
```
43+
44+
## Trait Implementations
45+
46+
A *trait implementation* is defined like an inherent implementation except that
47+
the optional generic type declarations is followed by a [trait] followed
48+
by the keyword `for`. <!-- To understand this, you have to back-reference to
49+
the previous section. :( -->
50+
51+
The trait is known as the *implemented trait*.
52+
53+
The implementing type implements the implemented trait.
54+
55+
A trait implementation must define all non-default associated items declared
56+
by the implemented trait, may redefine default associated items defined by the
57+
implemented trait trait, and cannot define any other items.
58+
59+
The path to the associated items is `<` followed by a path to the implementing
60+
type followed by `as` followed by a path to the trait followed by `>` as a path
61+
component followed by the associated item's path component.
62+
863
```rust
964
# #[derive(Copy, Clone)]
1065
# struct Point {x: f64, y: f64};
@@ -37,33 +92,39 @@ impl Shape for Circle {
3792
}
3893
```
3994

40-
It is possible to define an implementation without referring to a trait. The
41-
methods in such an implementation can only be used as direct calls on the
42-
values of the type that the implementation targets. In such an implementation,
43-
the trait type and `for` after `impl` are omitted. Such implementations are
44-
limited to nominal types (enums, structs, unions, trait objects), and the
45-
implementation must appear in the same crate as the `Self` type:
95+
### Trait Implementation Coherence
4696

47-
```rust
48-
struct Point {x: i32, y: i32}
97+
A trait implementation is consider incoherent if either the orphan check fails
98+
or there are overlapping implementation instaces.
4999

50-
impl Point {
51-
fn log(&self) {
52-
println!("Point is at ({}, {})", self.x, self.y);
53-
}
54-
}
100+
Two trait implementations overlap when there is a non-empty intersection of the
101+
traits the implementation is for, the implementations can be instantiated with
102+
the same type. <!-- This is probably wrong? Source: No two implementations can
103+
be instantiable with the same set of types for the input type parameters. -->
55104

56-
let my_point = Point {x: 10, y:11};
57-
my_point.log();
58-
```
105+
The `Orphan Check` states that every trait implementation must meet either of
106+
the following conditions:
59107

60-
When a trait _is_ specified in an `impl`, all methods declared as part of the
61-
trait must be implemented, with matching types and type parameter counts.
108+
1. The trait being implemented is defined in the same crate.
109+
110+
2. At least one of either `Self` or a generic type parameter of the trait must
111+
meet the following grammar, where `C` is a nominal type defined
112+
within the containing crate:
113+
114+
```ignore
115+
T = C
116+
| &T
117+
| &mut T
118+
| Box<T>
119+
```
120+
121+
## Generic Implementations
62122

63123
An implementation can take type and lifetime parameters, which can be used in
64124
the rest of the implementation. Type parameters declared for an implementation
65-
must be used at least once in either the trait or the type of an
66-
implementation. Implementation parameters are written after the `impl` keyword.
125+
must be used at least once in either the trait or the implementing type of an
126+
implementation. Implementation parameters are written directly after the `impl`
127+
keyword.
67128

68129
```rust
69130
# trait Seq<T> { fn dummy(&self, _: T) { } }
@@ -74,3 +135,6 @@ impl Seq<bool> for u32 {
74135
/* Treat the integer as a sequence of bits */
75136
}
76137
```
138+
139+
140+
[trait]: items/traits.html

0 commit comments

Comments
 (0)