1+ <!--
12# Casts
3+ -->
24
5+ # キャスト
6+
7+ <!--
38Casts are a superset of coercions: every coercion can be explicitly
49invoked via a cast. However some conversions require a cast.
510While coercions are pervasive and largely harmless, these "true casts"
611are rare and potentially dangerous. As such, casts must be explicitly invoked
712using the `as` keyword: `expr as Type`.
13+ -->
14+
15+ キャストは型強制のスーパーセットです。すなわち、全ての型強制は、キャストを通じて
16+ 明示的に引き起こすことが出来ます。しかし、いくつかの変換はキャストを必要とします。
17+ 型強制は普及していて、大体の場合、害はないのですが、これらの "真のキャスト" は稀で、
18+ 潜在的に危険です。ですから、キャストは ` as ` キーワードを用いて、明示的に
19+ 呼び起こさなければなりません: ` expr as Type `
820
21+ <!--
922True casts generally revolve around raw pointers and the primitive numeric
1023types. Even though they're dangerous, these casts are infallible at runtime.
1124If a cast triggers some subtle corner case no indication will be given that
1225this occurred. The cast will simply succeed. That said, casts must be valid
1326at the type level, or else they will be prevented statically. For instance,
1427`7u8 as bool` will not compile.
28+ -->
29+
30+ 真のキャストは一般的に、生ポインタやプリミティブ型の数値型に関係します。
31+ 真のキャストは危険ですが、これらのキャストは実行時に失敗しません。
32+ もしキャストが何か微妙なコーナーケースを引き起こしたとしても、
33+ 何の指摘もされないでしょう。キャストは単に成功します。そうは言ったものの、
34+ キャストは型レベルで正しくなければなりません。でなければそのキャストは静的に
35+ 防がれます。例えば、 ` 7u8 as bool ` はコンパイルされません。
1536
37+ <!--
1638That said, casts aren't `unsafe` because they generally can't violate memory
1739safety *on their own*. For instance, converting an integer to a raw pointer can
1840very easily lead to terrible things. However the act of creating the pointer
1941itself is safe, because actually using a raw pointer is already marked as
2042`unsafe`.
43+ -->
2144
45+ そうは言っていますが、キャストは ` unsafe ` ではありません。なぜなら、
46+ キャストは一般的に、* それ自体で* メモリ安全性を侵害しないからです。
47+ 例えば、整数を生ポインタに変換すると、非常に簡単にひどい問題を引き起しうるでしょう。
48+ しかしながら、ポインタを生成する事自体は安全です。なぜなら、実際に生ポインタを使用すること
49+ が既に ` unsafe ` としてマークされているからです。
50+
51+ <!--
2252Here's an exhaustive list of all the true casts. For brevity, we will use `*`
2353to denote either a `*const` or `*mut`, and `integer` to denote any integral
2454primitive:
55+ -->
56+
57+ これは、全ての真のキャストを網羅しているリストです。簡潔にするため、 ` * ` を ` *const ` か ` *mut ` の
58+ どちらかとして使い、 ` integer ` を整数型プリミティブの何かとして用います。
2559
60+ <!--
2661 * `*T as *U` where `T, U: Sized`
2762 * `*T as *U` TODO: explain unsized situation
2863 * `*T as integer`
@@ -35,16 +70,45 @@ primitive:
3570 * `&[T; n] as *const T`
3671 * `fn as *T` where `T: Sized`
3772 * `fn as integer`
73+ -->
3874
75+ * ` *T as *U ` 但し ` T, U: Sized `
76+ * ` *T as *U ` TODO: アンサイズの場合について説明する
77+ * ` *T as integer `
78+ * ` integer as *T `
79+ * ` number as number `
80+ * ` C-like-enum as integer `
81+ * ` bool as integer `
82+ * ` char as integer `
83+ * ` u8 as char `
84+ * ` &[T; n] as *const T `
85+ * ` fn as *T ` 但し ` T: Sized `
86+ * ` fn as integer `
87+
88+ <!--
3989Note that lengths are not adjusted when casting raw slices -
4090`*const [u16] as *const [u8]` creates a slice that only includes
4191half of the original memory.
92+ -->
4293
94+ 生スライスをキャストする時、その長さは調整されないことに注意してください。 ` *const [u16] as *const [u8] ` は、
95+ 元のメモリの半分しか含まないスライスを生成します。
96+
97+ <!--
4398Casting is not transitive, that is, even if `e as U1 as U2` is a valid
4499expression, `e as U2` is not necessarily so.
100+ -->
101+
102+ キャストは推移的ではありません。つまり、 ` e as U1 as U2 ` が有効な式だとしても、 ` e as U2 ` は
103+ 必ずしも有効とは限りません。
45104
105+ <!--
46106For numeric casts, there are quite a few cases to consider:
107+ -->
47108
109+ 数値のキャストに関しては、かなり多くの事項について考える必要があります。
110+
111+ <!--
48112* casting between two integers of the same size (e.g. i32 -> u32) is a no-op
49113* casting from a larger integer to a smaller integer (e.g. u32 -> u8) will
50114 truncate
@@ -65,6 +129,21 @@ For numeric casts, there are quite a few cases to consider:
65129 is finite but larger or smaller than the largest or smallest finite
66130 value representable by f32][float-float]**. This is a bug and will
67131 be fixed.
132+ -->
133+
134+ * 同じ大きさの 2 つの整数の間でのキャスト (例: i32 -> u32) は no-op です
135+ * 大きい方の整数から小さい方の整数へのキャスト (例: u32 -> u8) は切り捨てが発生します
136+ * 小さい方の整数から大きい方の整数へのキャスト (例: u8 -> u32) は
137+ * もし小さい方の整数が符号なしの場合、ゼロ拡張されます
138+ * もし小さい方の整数が符号ありの場合、符号拡張されます
139+ * 浮動小数点数から整数へのキャストは、小数点以下が切り捨てられます
140+ * ** [ 注意: 現在もし丸められた値が、キャスト先の整数型で表現できない場合、未定義動作を引き起こします] [ float-int ] ** 。
141+ これはバグで、将来修正されます。
142+ * 整数から浮動小数点数へのキャストは、整数を浮動小数点数で表現します。必要ならば丸められます (丸めの方針は指定されていません)
143+ * f32 から f64 へのキャストは完全で、損失はありません
144+ * f64 から f32 へのキャストは、最も近い表現可能な値となります (丸めの方針は指定されていません)
145+ * ** [ 注意: 現在もし値が f32 で表現可能な最大の値より大きい、あるいは最小の値より小さい有限の値である場合、未定義動作を引き起こします] [ float-float ] ** 。
146+ これはバグで、将来修正されます。
68147
69148
70149[ float-int ] : https://github.com/rust-lang/rust/issues/10184
0 commit comments