1
1
# Implementations
2
2
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.
5
7
6
8
Implementations are defined with the keyword ` impl ` .
7
9
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
+
8
63
``` rust
9
64
# #[derive(Copy , Clone )]
10
65
# struct Point {x : f64 , y : f64 };
@@ -37,33 +92,39 @@ impl Shape for Circle {
37
92
}
38
93
```
39
94
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
46
96
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.
49
99
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. -->
55
104
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:
59
107
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
62
122
63
123
An implementation can take type and lifetime parameters, which can be used in
64
124
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.
67
128
68
129
``` rust
69
130
# trait Seq <T > { fn dummy (& self , _ : T ) { } }
@@ -74,3 +135,6 @@ impl Seq<bool> for u32 {
74
135
/* Treat the integer as a sequence of bits */
75
136
}
76
137
```
138
+
139
+
140
+ [ trait ] : items/traits.html
0 commit comments