1
+ <!--
1
2
# Casts
3
+ -->
2
4
5
+ # キャスト
6
+
7
+ <!--
3
8
Casts are a superset of coercions: every coercion can be explicitly
4
9
invoked via a cast. However some conversions require a cast.
5
10
While coercions are pervasive and largely harmless, these "true casts"
6
11
are rare and potentially dangerous. As such, casts must be explicitly invoked
7
12
using the `as` keyword: `expr as Type`.
13
+ -->
14
+
15
+ キャストは型強制のスーパーセットです。すなわち、全ての型強制は、キャストを通じて
16
+ 明示的に引き起こすことが出来ます。しかし、いくつかの変換はキャストを必要とします。
17
+ 型強制は普及していて、大体の場合、害はないのですが、これらの "真のキャスト" は稀で、
18
+ 潜在的に危険です。ですから、キャストは ` as ` キーワードを用いて、明示的に
19
+ 呼び起こさなければなりません: ` expr as Type `
8
20
21
+ <!--
9
22
True casts generally revolve around raw pointers and the primitive numeric
10
23
types. Even though they're dangerous, these casts are infallible at runtime.
11
24
If a cast triggers some subtle corner case no indication will be given that
12
25
this occurred. The cast will simply succeed. That said, casts must be valid
13
26
at the type level, or else they will be prevented statically. For instance,
14
27
`7u8 as bool` will not compile.
28
+ -->
29
+
30
+ 真のキャストは一般的に、生ポインタやプリミティブ型の数値型に関係します。
31
+ 真のキャストは危険ですが、これらのキャストは実行時に失敗しません。
32
+ もしキャストが何か微妙なコーナーケースを引き起こしたとしても、
33
+ 何の指摘もされないでしょう。キャストは単に成功します。そうは言ったものの、
34
+ キャストは型レベルで正しくなければなりません。でなければそのキャストは静的に
35
+ 防がれます。例えば、 ` 7u8 as bool ` はコンパイルされません。
15
36
37
+ <!--
16
38
That said, casts aren't `unsafe` because they generally can't violate memory
17
39
safety *on their own*. For instance, converting an integer to a raw pointer can
18
40
very easily lead to terrible things. However the act of creating the pointer
19
41
itself is safe, because actually using a raw pointer is already marked as
20
42
`unsafe`.
43
+ -->
21
44
45
+ そうは言っていますが、キャストは ` unsafe ` ではありません。なぜなら、
46
+ キャストは一般的に、* それ自体で* メモリ安全性を侵害しないからです。
47
+ 例えば、整数を生ポインタに変換すると、非常に簡単にひどい問題を引き起しうるでしょう。
48
+ しかしながら、ポインタを生成する事自体は安全です。なぜなら、実際に生ポインタを使用すること
49
+ が既に ` unsafe ` としてマークされているからです。
50
+
51
+ <!--
22
52
Here's an exhaustive list of all the true casts. For brevity, we will use `*`
23
53
to denote either a `*const` or `*mut`, and `integer` to denote any integral
24
54
primitive:
55
+ -->
56
+
57
+ これは、全ての真のキャストを網羅しているリストです。簡潔にするため、 ` * ` を ` *const ` か ` *mut ` の
58
+ どちらかとして使い、 ` integer ` を整数型プリミティブの何かとして用います。
25
59
60
+ <!--
26
61
* `*T as *U` where `T, U: Sized`
27
62
* `*T as *U` TODO: explain unsized situation
28
63
* `*T as integer`
@@ -35,16 +70,45 @@ primitive:
35
70
* `&[T; n] as *const T`
36
71
* `fn as *T` where `T: Sized`
37
72
* `fn as integer`
73
+ -->
38
74
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
+ <!--
39
89
Note that lengths are not adjusted when casting raw slices -
40
90
`*const [u16] as *const [u8]` creates a slice that only includes
41
91
half of the original memory.
92
+ -->
42
93
94
+ 生スライスをキャストする時、その長さは調整されないことに注意してください。 ` *const [u16] as *const [u8] ` は、
95
+ 元のメモリの半分しか含まないスライスを生成します。
96
+
97
+ <!--
43
98
Casting is not transitive, that is, even if `e as U1 as U2` is a valid
44
99
expression, `e as U2` is not necessarily so.
100
+ -->
101
+
102
+ キャストは推移的ではありません。つまり、 ` e as U1 as U2 ` が有効な式だとしても、 ` e as U2 ` は
103
+ 必ずしも有効とは限りません。
45
104
105
+ <!--
46
106
For numeric casts, there are quite a few cases to consider:
107
+ -->
47
108
109
+ 数値のキャストに関しては、かなり多くの事項について考える必要があります。
110
+
111
+ <!--
48
112
* casting between two integers of the same size (e.g. i32 -> u32) is a no-op
49
113
* casting from a larger integer to a smaller integer (e.g. u32 -> u8) will
50
114
truncate
@@ -65,6 +129,21 @@ For numeric casts, there are quite a few cases to consider:
65
129
is finite but larger or smaller than the largest or smallest finite
66
130
value representable by f32][float-float]**. This is a bug and will
67
131
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
+ これはバグで、将来修正されます。
68
147
69
148
70
149
[ float-int ] : https://github.com/rust-lang/rust/issues/10184
0 commit comments