1+ <!--
12# Coercions
3+ -->
24
5+ # 型強制
6+
7+ <!--
38Types can implicitly be coerced to change in certain contexts. These changes are
49generally just *weakening* of types, largely focused around pointers and
510lifetimes. They mostly exist to make Rust "just work" in more cases, and are
611largely harmless.
12+ -->
13+
14+ 特定の状況では、暗黙に型変換を強制することが出来ます。これらの変換は、一般には
15+ 単に型を* 弱く* していて、主にポインタやライフタイム周りに着目されます。
16+ これらはほとんどが、より多くのケースで Rust が "単に動く" ようにするために存在し、
17+ そして大部分において、ほとんど害はありません。
718
19+ <!--
820Here's all the kinds of coercion:
21+ -->
22+
23+ これらは全ての種類の型強制です:
924
25+ <!--
1026Coercion is allowed between the following types:
27+ -->
1128
29+ 型強制は以下の型の間で認められています:
30+
31+ <!--
1232* Transitivity: `T_1` to `T_3` where `T_1` coerces to `T_2` and `T_2` coerces to
1333 `T_3`
1434* Pointer Weakening:
@@ -18,11 +38,28 @@ Coercion is allowed between the following types:
1838 * `&mut T` to `*mut T`
1939* Unsizing: `T` to `U` if `T` implements `CoerceUnsized<U>`
2040* 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> ` )
2151
52+ <!--
2253`CoerceUnsized<Pointer<U>> for Pointer<T> where T: Unsize<U>` is implemented
2354for all pointer types (including smart pointers like Box and Rc). Unsize is
2455only implemented automatically, and enables the following transformations:
56+ -->
57+
58+ ` CoerceUnsized<Pointer<U>> for Pointer<T> where T: Unsize<U> ` は
59+ 全てのポインタ型 (Box や Rc のようなスマートポインタを含む) で実装されています。
60+ アンサイズは自動的にのみ実装され、以下の変換を有効にします。
2561
62+ <!--
2663* `[T; n]` => `[T]`
2764* `T` => `Trait` where `T: Trait`
2865* `Foo<..., T, ...>` => `Foo<..., U, ...>` where:
@@ -31,25 +68,58 @@ only implemented automatically, and enables the following transformations:
3168 * Only the last field of `Foo` has type involving `T`
3269 * `T` is not part of the type of any other fields
3370 * `Bar<T>: Unsize<Bar<U>>`, if the last field of `Foo` has type `Bar<T>`
71+ -->
3472
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+ <!--
3583Coercions occur at a *coercion site*. Any location that is explicitly typed
3684will cause a coercion to its type. If inference is necessary, the coercion will
3785not be performed. Exhaustively, the coercion sites for an expression `e` to
3886type `U` are:
87+ -->
3988
89+ 型強制は、* 型強制サイト* で起こります。明確に型が指定されている全ての場所で、
90+ その型への型強制が発生します。もし推論が必要ならば、型強制は行われません。
91+ 余すことなく言えば、式 ` e ` に対する型 ` U ` への型強制サイトは以下の通りです。
92+
93+ <!--
4094* let statements, statics, and consts: `let x: U = e`
4195* Arguments to functions: `takes_a_U(e)`
4296* Any expression that will be returned: `fn foo() -> U { e }`
4397* Struct literals: `Foo { some_u: e }`
4498* Array literals: `let x: [U; 10] = [e, ..]`
4599* Tuple literals: `let x: (U, ..) = (e, ..)`
46100* 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 } `
47110
111+ <!--
48112Note that we do not perform coercions when matching traits (except for
49113receivers, see below). If there is an impl for some type `U` and `T` coerces to
50114`U`, that does not constitute an implementation for `T`. For example, the
51115following will not type check, even though it is OK to coerce `t` to `&T` and
52116there is an impl for `&T`:
117+ -->
118+
119+ トレイトをマッチさせる場合、型強制が行われないことに注意してください (レシーバは例外です、
120+ 以下を見てください) 。もしある型 ` U ` に対する impl が存在し、 ` T ` が ` U ` に型強制される場合、 ` T ` に対しては
121+ 実装が構成されません。例えば、以下の例では ` t ` が ` &T ` に型強制されても問題なく、 ` &T ` に対する impl が存在するにも関わらず、
122+ 型チェックに通りません。
53123
54124``` rust,ignore
55125trait Trait {}
@@ -67,6 +137,7 @@ fn main() {
67137
68138``` text
69139<anon>:10:5: 10:8 error: the trait bound `&mut i32 : Trait` is not satisfied [E0277]
140+ (エラー: トレイト境界 `&mut i32: Trait` が満たされていません)
70141<anon>:10 foo(t);
71142 ^~~
72143```
0 commit comments