@@ -47,11 +47,15 @@ to declare that the implementation of that trait has adhered to whatever
47
47
contracts the trait's documentation requires.
48
48
-->
49
49
50
+ コードブロックに使われた ` unsafe ` は、そのブロックで呼ばれている危険な関数が要求する契約は守られていて、コードが信頼出来る事を意味します。` unsafe ` を trait の実装に使うと、その実装が trait のドキュメントに書かれている契約に準拠している事を示します。
50
51
51
-
52
-
52
+ <!--
53
53
The standard library has a number of unsafe functions, including:
54
+ -->
55
+
56
+ 標準ライブラリにはいくつもの危険な関数があります。例えば、
54
57
58
+ <!--
55
59
* `slice::get_unchecked`, which performs unchecked indexing, allowing
56
60
memory safety to be freely violated.
57
61
* `mem::transmute` reinterprets some value as having a given type, bypassing
@@ -61,29 +65,62 @@ The standard library has a number of unsafe functions, including:
61
65
defined by LLVM.
62
66
* All FFI functions are `unsafe` because the other language can do arbitrary
63
67
operations that the Rust compiler can't check.
68
+ -->
69
+
70
+ * ` slice::get_unchecked ` は未チェックのインデックス参照を実行します。自由自在にメモリ安全性に違反できます。
71
+ * ` mem::transmute ` は、型安全の仕組みを好きなようにすり抜けて、ある値が特定の型であると再解釈します(詳細は [ 変換] をみてください)。
72
+ * サイズが確定している型の生のポインタには、固有の ` offset ` メソッドがあります。渡されたオフセットが LLVM が定める "境界内" になければ、未定義の挙動を引き起こします。
73
+ * すべての FFI 関数は ` unsafe ` です。なぜなら Rust コンパイラは、他の言語が実行するどんな操作もチェックできないからです。
64
74
75
+ <!--
65
76
As of Rust 1.0 there are exactly two unsafe traits:
77
+ -->
66
78
79
+ Rust 1.0 現在、危険な traits は 2 つしかありません。
80
+
81
+ <!--
67
82
* `Send` is a marker trait (a trait with no API) that promises implementors are
68
83
safe to send (move) to another thread.
69
84
* `Sync` is a marker trait that promises threads can safely share implementors
70
85
through a shared reference.
86
+ -->
87
+
88
+ * ` Send ` は API を持たないマーカー trait で、実装された型が他のスレッドに安全に送れる(move できる)ことを約束します。
89
+ * ` Sync ` もマーカー trait で、この trait を実装した型は、共有リファレンスを使って安全に複数のスレッドで共有できる事を約束します。
71
90
91
+ <!--
72
92
Much of the Rust standard library also uses Unsafe Rust internally, although
73
93
these implementations are rigorously manually checked, and the Safe Rust
74
94
interfaces provided on top of these implementations can be assumed to be safe.
95
+ -->
96
+
97
+ また、多くの Rust 標準ライブラリは内部で危険な Rust を使っています。ただ、標準ライブラリの
98
+ 実装はプログラマが徹底的にチェックしているので、危険な Rust の上に実装された安全な Rust は安全であると仮定して良いでしょう。
75
99
100
+ <!--
76
101
The need for all of this separation boils down a single fundamental property
77
102
of Safe Rust:
78
103
79
104
**No matter what, Safe Rust can't cause Undefined Behavior.**
105
+ -->
106
+
107
+ このように分離する目的は、結局のところ、安全な Rust のたった一つの基本的な性質にあります。
80
108
109
+ ** どうやっても、安全な Rust では未定義な挙動を起こせない。**
110
+
111
+ <!--
81
112
The design of the safe/unsafe split means that Safe Rust inherently has to
82
113
trust that any Unsafe Rust it touches has been written correctly (meaning
83
114
the Unsafe Rust actually maintains whatever contracts it is supposed to
84
115
maintain). On the other hand, Unsafe Rust has to be very careful about
85
116
trusting Safe Rust.
117
+ -->
86
118
119
+ このように安全と危険の分けると、安全な Rust は、自分が利用する危険な Rust が正しく書かれている事、
120
+ つまり危険な Rust がそれが守るべき契約を実際に守っている事、を本質的に信頼しなくてはいけません。
121
+ 逆に、危険な Rust は安全な Rust を注意して信頼しなくてはいけません。
122
+
123
+ <!--
87
124
As an example, Rust has the `PartialOrd` and `Ord` traits to differentiate
88
125
between types which can "just" be compared, and those that provide a total
89
126
ordering (where every value of the type is either equal to, greater than,
@@ -95,15 +132,37 @@ Unsafe Rust code cannot assume that any `Ord` implementation it gets makes
95
132
sense. The unsafe portions of `BTreeMap`'s internals have to be careful to
96
133
maintain all necessary contracts, even if a key type's `Ord` implementation
97
134
does not implement a total ordering.
135
+ -->
136
+
137
+ 例えば、Rust には ` PartialOrd ` trait と ` Ord ` trait があり、単に比較可能な型と全順序が
138
+ 定義されている型(任意の値が同じ型の他の値と比べて等しいか、大きいか、小さい)とを区別します。
139
+ 順序つきマップの ` BTreeMap ` は半順序の型には使えないので、キーとして使われる型が ` Ord ` trait を
140
+ 実装している事を要求します。
141
+ しかし ` BTreeMap ` の実装は危険な Rust が使っていて、危険な Rust は渡された ` Ord ` の実装が
142
+ 適切であるとは仮定できません。
143
+ ` BTreeMap ` 内部の危険な部分は、キー型の ` Ord ` の実装が全順序ではない場合でも、必要な契約が
144
+ すべて守られるよう注意深く書かれなくてはいけません。
98
145
146
+ <!--
99
147
Unsafe Rust cannot automatically trust Safe Rust. When writing Unsafe Rust,
100
148
you must be careful to only rely on specific Safe Rust code, and not make
101
149
assumptions about potential future Safe Rust code providing the same
102
150
guarantees.
151
+ -->
152
+
153
+ 危険な Rust は安全な Rust を無意識には信頼できません。危険な Rust コードを書くときには、
154
+ 安全な Rust の特定のコードのみに依存する必要があり、
155
+ 安全な Rust が将来にわたって同様の安全性を提供すると仮定してはいけません。
103
156
157
+ <!--
104
158
This is the problem that `unsafe` traits exist to resolve. The `BTreeMap`
105
159
type could theoretically require that keys implement a new trait called
106
160
`UnsafeOrd`, rather than `Ord`, that might look like this:
161
+ -->
162
+
163
+ この問題を解決するために ` unsafe ` な trait が存在します。理論上は、` BTreeMap ` 型は
164
+ キーが ` Ord ` ではなく、新しい trait ` UnsafeOrd ` を実装する事を要求する事ができます。
165
+ このようなコードになるでしょう。
107
166
108
167
``` rust
109
168
use std :: cmp :: Ordering ;
@@ -113,13 +172,22 @@ unsafe trait UnsafeOrd {
113
172
}
114
173
```
115
174
175
+ <!--
116
176
Then, a type would use `unsafe` to implement `UnsafeOrd`, indicating that
117
177
they've ensured their implementation maintains whatever contracts the
118
178
trait expects. In this situation, the Unsafe Rust in the internals of
119
179
`BTreeMap` could trust that the key type's `UnsafeOrd` implementation is
120
180
correct. If it isn't, it's the fault of the unsafe trait implementation
121
181
code, which is consistent with Rust's safety guarantees.
182
+ -->
183
+
184
+ この場合、` UnsafeOrd ` を実装する型は、この trait が期待する契約に準拠している事を示すために
185
+ ` unsafe ` キーワードを使うことになります。
186
+ この状況では、` BTreeMap ` 内部の危険な Rust は、キー型が ` UnsafeOrd ` を正しく実装していると
187
+ 信用する事ができます。もしそうで無ければ、それは trait の実装の問題であり、
188
+ これは Rust の安全性の保証と一致しています。
122
189
190
+ <!--
123
191
The decision of whether to mark a trait `unsafe` is an API design choice.
124
192
Rust has traditionally avoided marking traits unsafe because it makes Unsafe
125
193
Rust pervasive, which is not desirable. `Send` and `Sync` are marked unsafe
@@ -129,19 +197,46 @@ possibly hope to defend against in the way it could defend against a bad
129
197
depends on the same sort of consideration. If `unsafe` code cannot reasonably
130
198
expect to defend against a bad implementation of the trait, then marking the
131
199
trait `unsafe` is a reasonable choice.
200
+ -->
201
+
202
+ trait に ` unsafe ` をつけるかどうかは API デザインにおける選択です。
203
+ Rust では従来 ` unsafe ` な trait を避けてきました。そうしないと危険な Rust が
204
+ 蔓延してしまい、好ましくないからです。
205
+ ` Send ` と ` Sync ` が ` unsafe ` となっているのは、スレッドの安全性が * 基本的な性質* であり、
206
+ 間違った ` Ord ` の実装に対して危険なコードが防衛できるのと同様の意味では防衛できないからです。
207
+ あなたが宣言した trait を ` unsafe ` とマークするかどうかも、同じようにじっくりと考えてください。
208
+ もし ` unsafe ` なコードがその trait の間違った実装から防御することが合理的に不可能であるなら、
209
+ その trait を ` unsafe ` とするのは合理的な選択です。
132
210
211
+ <!--
133
212
As an aside, while `Send` and `Sync` are `unsafe` traits, they are
134
213
automatically implemented for types when such derivations are provably safe
135
214
to do. `Send` is automatically derived for all types composed only of values
136
215
whose types also implement `Send`. `Sync` is automatically derived for all
137
216
types composed only of values whose types also implement `Sync`.
217
+ -->
218
+
219
+ 余談ですが、` unsafe ` な trait である ` Send ` と ` Sync ` は、それらを実装する事が安全だと
220
+ 実証可能な場合には自動的に実装されます。
221
+ ` Send ` は、` Send ` を実装した型だけから構成される型に対して、自動的に実装されます。
222
+ ` Sync ` は、` Sync ` を実装した型だけから構成される型に対して、自動的に実装されます。
138
223
224
+ <!--
139
225
This is the dance of Safe Rust and Unsafe Rust. It is designed to make using
140
226
Safe Rust as ergonomic as possible, but requires extra effort and care when
141
227
writing Unsafe Rust. The rest of the book is largely a discussion of the sort
142
228
of care that must be taken, and what contracts it is expected of Unsafe Rust
143
229
to uphold.
230
+ -->
231
+
232
+ これが安全な Rust と危険な Rust のダンスです。
233
+ これは、安全な Rust をできるだけ快適に使えるように、しかし危険な Rust を書くには
234
+ それ以上の努力と注意深さが要求されるようなデザインになっています。
235
+ この本の残りでは、どういう点に注意しなくはいけないのか、
236
+ 危険な Rust を維持するための契約とは何なのかを議論します。
237
+
238
+
144
239
145
240
[ drop flags ] : drop-flags.html
146
- [ conversions ] : conversions.html
241
+ [ 変換 ] : conversions.html
147
242
0 commit comments