1
+ <!--
1
2
# Coercions
3
+ -->
2
4
5
+ # 型強制
6
+
7
+ <!--
3
8
Types can implicitly be coerced to change in certain contexts. These changes are
4
9
generally just *weakening* of types, largely focused around pointers and
5
10
lifetimes. They mostly exist to make Rust "just work" in more cases, and are
6
11
largely harmless.
12
+ -->
13
+
14
+ 特定の状況では、暗黙に型変換を強制することが出来ます。これらの変換は、一般には
15
+ 単に型を* 弱く* していて、主にポインタやライフタイム周りに着目されます。
16
+ これらはほとんどが、より多くのケースで Rust が "単に動く" ようにするために存在し、
17
+ そして大部分において、ほとんど害はありません。
7
18
19
+ <!--
8
20
Here's all the kinds of coercion:
21
+ -->
22
+
23
+ これらは全ての種類の型強制です:
9
24
25
+ <!--
10
26
Coercion is allowed between the following types:
27
+ -->
11
28
29
+ 型強制は以下の型の間で認められています:
30
+
31
+ <!--
12
32
* Transitivity: `T_1` to `T_3` where `T_1` coerces to `T_2` and `T_2` coerces to
13
33
`T_3`
14
34
* Pointer Weakening:
@@ -18,11 +38,28 @@ Coercion is allowed between the following types:
18
38
* `&mut T` to `*mut T`
19
39
* Unsizing: `T` to `U` if `T` implements `CoerceUnsized<U>`
20
40
* Deref coercion: Expression `&x` of type `&T` to `&*x` of type `&U` if `T` derefs to `U` (i.e. `T: Deref<Target=U>`)
41
+ -->
42
+
43
+ * 推移性: ` T_1 ` から ` T_3 ` 但し ` T_1 ` が ` T_2 ` に型強制可能で、 ` T_2 ` が ` T_3 ` に型強制可能な場合
44
+ * ポインタの弱化:
45
+ * ` &mut T ` から ` &T `
46
+ * ` *mut T ` から ` *const T `
47
+ * ` &T ` から ` *const T `
48
+ * ` &mut T ` から ` *mut T `
49
+ * アンサイジング: ` T ` から ` U ` 但し ` T ` が ` CoerceUnsized<U> ` を実装している場合
50
+ * 参照外しの型強制: 型 ` &T ` の式 ` &x ` から型 ` &U ` の式 ` &'x ` 但し ` T ` が ` U ` に参照外しされる場合 (例: ` T: Deref<Target=U> ` )
21
51
52
+ <!--
22
53
`CoerceUnsized<Pointer<U>> for Pointer<T> where T: Unsize<U>` is implemented
23
54
for all pointer types (including smart pointers like Box and Rc). Unsize is
24
55
only implemented automatically, and enables the following transformations:
56
+ -->
57
+
58
+ ` CoerceUnsized<Pointer<U>> for Pointer<T> where T: Unsize<U> ` は
59
+ 全てのポインタ型 (Box や Rc のようなスマートポインタを含む) で実装されています。
60
+ アンサイズは自動的にのみ実装され、以下の変換を有効にします。
25
61
62
+ <!--
26
63
* `[T; n]` => `[T]`
27
64
* `T` => `Trait` where `T: Trait`
28
65
* `Foo<..., T, ...>` => `Foo<..., U, ...>` where:
@@ -31,25 +68,58 @@ only implemented automatically, and enables the following transformations:
31
68
* Only the last field of `Foo` has type involving `T`
32
69
* `T` is not part of the type of any other fields
33
70
* `Bar<T>: Unsize<Bar<U>>`, if the last field of `Foo` has type `Bar<T>`
71
+ -->
34
72
73
+ * ` [T; n] ` => ` [T] `
74
+ * ` T ` => ` Trait ` 但し ` T: Trait `
75
+ * ` Foo<..., T, ...> ` => ` Foo<..., U, ...> ` 但し
76
+ * ` T: Unsize<U> `
77
+ * ` Foo ` は構造体
78
+ * ` Foo ` の最後のフィールドだけが ` T ` を含む型である
79
+ * ` T ` は他のフィールドの一部となっていない
80
+ * ` Bar<T>: Unsize<Bar<U>> ` 但し ` Foo ` の最後のフィールドが ` Bar<T> ` の型である場合
81
+
82
+ <!--
35
83
Coercions occur at a *coercion site*. Any location that is explicitly typed
36
84
will cause a coercion to its type. If inference is necessary, the coercion will
37
85
not be performed. Exhaustively, the coercion sites for an expression `e` to
38
86
type `U` are:
87
+ -->
39
88
89
+ 型強制は、* 型強制サイト* で起こります。明確に型が指定されている全ての場所で、
90
+ その型への型強制が発生します。もし推論が必要ならば、型強制は行われません。
91
+ 余すことなく言えば、式 ` e ` に対する型 ` U ` への型強制サイトは以下の通りです。
92
+
93
+ <!--
40
94
* let statements, statics, and consts: `let x: U = e`
41
95
* Arguments to functions: `takes_a_U(e)`
42
96
* Any expression that will be returned: `fn foo() -> U { e }`
43
97
* Struct literals: `Foo { some_u: e }`
44
98
* Array literals: `let x: [U; 10] = [e, ..]`
45
99
* Tuple literals: `let x: (U, ..) = (e, ..)`
46
100
* The last expression in a block: `let x: U = { ..; e }`
101
+ -->
102
+
103
+ * let 文、 static、 const: ` let x: U = e `
104
+ * 関数に対する引数: ` takes_a_U(e) `
105
+ * 返される全ての式: ` fn foo() -> U { e } `
106
+ * 構造体リテラル: ` Foo { some_u: e } `
107
+ * 配列リテラル: ` let x: [U; 10] = [e, ..] `
108
+ * タプルリテラル: ` let x: (U, ..) = (e, ..) `
109
+ * ブロックの最後の式: ` let x: U = { ..; e } `
47
110
111
+ <!--
48
112
Note that we do not perform coercions when matching traits (except for
49
113
receivers, see below). If there is an impl for some type `U` and `T` coerces to
50
114
`U`, that does not constitute an implementation for `T`. For example, the
51
115
following will not type check, even though it is OK to coerce `t` to `&T` and
52
116
there is an impl for `&T`:
117
+ -->
118
+
119
+ トレイトをマッチさせる場合、型強制が行われないことに注意してください (レシーバは例外です、
120
+ 以下を見てください) 。もしある型 ` U ` に対する impl が存在し、 ` T ` が ` U ` に型強制される場合、 ` T ` に対しては
121
+ 実装が構成されません。例えば、以下の例では ` t ` が ` &T ` に型強制されても問題なく、 ` &T ` に対する impl が存在するにも関わらず、
122
+ 型チェックに通りません。
53
123
54
124
``` rust,ignore
55
125
trait Trait {}
@@ -67,6 +137,7 @@ fn main() {
67
137
68
138
``` text
69
139
<anon>:10:5: 10:8 error: the trait bound `&mut i32 : Trait` is not satisfied [E0277]
140
+ (エラー: トレイト境界 `&mut i32: Trait` が満たされていません)
70
141
<anon>:10 foo(t);
71
142
^~~
72
143
```
0 commit comments