Skip to content

Commit c2b9ede

Browse files
committed
safe-unsafe-mearning
1 parent 43c5dfb commit c2b9ede

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

src/safe-unsafe-meaning.md

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ to declare that the implementation of that trait has adhered to whatever
4747
contracts the trait's documentation requires.
4848
-->
4949

50+
コードブロックに使われた `unsafe` は、そのブロックで呼ばれている危険な関数が要求する契約は守られていて、コードが信頼出来る事を意味します。`unsafe` を trait の実装に使うと、その実装が trait のドキュメントに書かれている契約に準拠している事を示します。
5051

51-
52-
52+
<!--
5353
The standard library has a number of unsafe functions, including:
54+
-->
55+
56+
標準ライブラリにはいくつもの危険な関数があります。例えば、
5457

58+
<!--
5559
* `slice::get_unchecked`, which performs unchecked indexing, allowing
5660
memory safety to be freely violated.
5761
* `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:
6165
defined by LLVM.
6266
* All FFI functions are `unsafe` because the other language can do arbitrary
6367
operations that the Rust compiler can't check.
68+
-->
69+
70+
* `slice::get_unchecked` は未チェックのインデックス参照を実行します。自由自在にメモリ安全性に違反できます。
71+
* `mem::transmute` は、型安全の仕組みを好きなようにすり抜けて、ある値が特定の型であると再解釈します(詳細は [変換] をみてください)。
72+
* サイズが確定している型の生のポインタには、固有の `offset` メソッドがあります。渡されたオフセットが LLVM が定める "境界内" になければ、未定義の挙動を引き起こします。
73+
* すべての FFI 関数は `unsafe` です。なぜなら Rust コンパイラは、他の言語が実行するどんな操作もチェックできないからです。
6474

75+
<!--
6576
As of Rust 1.0 there are exactly two unsafe traits:
77+
-->
6678

79+
Rust 1.0 現在、危険な traits は 2 つしかありません。
80+
81+
<!--
6782
* `Send` is a marker trait (a trait with no API) that promises implementors are
6883
safe to send (move) to another thread.
6984
* `Sync` is a marker trait that promises threads can safely share implementors
7085
through a shared reference.
86+
-->
87+
88+
* `Send` は API を持たないマーカー trait で、実装された型が他のスレッドに安全に送れる(move できる)ことを約束します。
89+
* `Sync` もマーカー trait で、この trait を実装した型は、共有リファレンスを使って安全に複数のスレッドで共有できる事を約束します。
7190

91+
<!--
7292
Much of the Rust standard library also uses Unsafe Rust internally, although
7393
these implementations are rigorously manually checked, and the Safe Rust
7494
interfaces provided on top of these implementations can be assumed to be safe.
95+
-->
96+
97+
また、多くの Rust 標準ライブラリは内部で危険な Rust を使っています。ただ、標準ライブラリの
98+
実装はプログラマが徹底的にチェックしているので、危険な Rust の上に実装された安全な Rust は安全であると仮定して良いでしょう。
7599

100+
<!--
76101
The need for all of this separation boils down a single fundamental property
77102
of Safe Rust:
78103
79104
**No matter what, Safe Rust can't cause Undefined Behavior.**
105+
-->
106+
107+
このように分離する目的は、結局のところ、安全な Rust のたった一つの基本的な性質にあります。
80108

109+
**どうやっても、安全な Rust では未定義な挙動を起こせない。**
110+
111+
<!--
81112
The design of the safe/unsafe split means that Safe Rust inherently has to
82113
trust that any Unsafe Rust it touches has been written correctly (meaning
83114
the Unsafe Rust actually maintains whatever contracts it is supposed to
84115
maintain). On the other hand, Unsafe Rust has to be very careful about
85116
trusting Safe Rust.
117+
-->
86118

119+
このように安全と危険の分けると、安全な Rust は、自分が利用する危険な Rust が正しく書かれている事、
120+
つまり危険な Rust がそれが守るべき契約を実際に守っている事、を本質的に信頼しなくてはいけません。
121+
逆に、危険な Rust は安全な Rust を注意して信頼しなくてはいけません。
122+
123+
<!--
87124
As an example, Rust has the `PartialOrd` and `Ord` traits to differentiate
88125
between types which can "just" be compared, and those that provide a total
89126
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
95132
sense. The unsafe portions of `BTreeMap`'s internals have to be careful to
96133
maintain all necessary contracts, even if a key type's `Ord` implementation
97134
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+
すべて守られるよう注意深く書かれなくてはいけません。
98145

146+
<!--
99147
Unsafe Rust cannot automatically trust Safe Rust. When writing Unsafe Rust,
100148
you must be careful to only rely on specific Safe Rust code, and not make
101149
assumptions about potential future Safe Rust code providing the same
102150
guarantees.
151+
-->
152+
153+
危険な Rust は安全な Rust を無意識には信頼できません。危険な Rust コードを書くときには、
154+
安全な Rust の特定のコードのみに依存する必要があり、
155+
安全な Rust が将来にわたって同様の安全性を提供すると仮定してはいけません。
103156

157+
<!--
104158
This is the problem that `unsafe` traits exist to resolve. The `BTreeMap`
105159
type could theoretically require that keys implement a new trait called
106160
`UnsafeOrd`, rather than `Ord`, that might look like this:
161+
-->
162+
163+
この問題を解決するために `unsafe` な trait が存在します。理論上は、`BTreeMap` 型は
164+
キーが `Ord` ではなく、新しい trait `UnsafeOrd` を実装する事を要求する事ができます。
165+
このようなコードになるでしょう。
107166

108167
```rust
109168
use std::cmp::Ordering;
@@ -113,13 +172,22 @@ unsafe trait UnsafeOrd {
113172
}
114173
```
115174

175+
<!--
116176
Then, a type would use `unsafe` to implement `UnsafeOrd`, indicating that
117177
they've ensured their implementation maintains whatever contracts the
118178
trait expects. In this situation, the Unsafe Rust in the internals of
119179
`BTreeMap` could trust that the key type's `UnsafeOrd` implementation is
120180
correct. If it isn't, it's the fault of the unsafe trait implementation
121181
code, which is consistent with Rust's safety guarantees.
182+
-->
183+
184+
この場合、`UnsafeOrd` を実装する型は、この trait が期待する契約に準拠している事を示すために
185+
`unsafe` キーワードを使うことになります。
186+
この状況では、`BTreeMap` 内部の危険な Rust は、キー型が `UnsafeOrd` を正しく実装していると
187+
信用する事ができます。もしそうで無ければ、それは trait の実装の問題であり、
188+
これは Rust の安全性の保証と一致しています。
122189

190+
<!--
123191
The decision of whether to mark a trait `unsafe` is an API design choice.
124192
Rust has traditionally avoided marking traits unsafe because it makes Unsafe
125193
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
129197
depends on the same sort of consideration. If `unsafe` code cannot reasonably
130198
expect to defend against a bad implementation of the trait, then marking the
131199
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` とするのは合理的な選択です。
132210

211+
<!--
133212
As an aside, while `Send` and `Sync` are `unsafe` traits, they are
134213
automatically implemented for types when such derivations are provably safe
135214
to do. `Send` is automatically derived for all types composed only of values
136215
whose types also implement `Send`. `Sync` is automatically derived for all
137216
types composed only of values whose types also implement `Sync`.
217+
-->
218+
219+
余談ですが、`unsafe` な trait である `Send``Sync` は、それらを実装する事が安全だと
220+
実証可能な場合には自動的に実装されます。
221+
`Send` は、`Send` を実装した型だけから構成される型に対して、自動的に実装されます。
222+
`Sync` は、`Sync` を実装した型だけから構成される型に対して、自動的に実装されます。
138223

224+
<!--
139225
This is the dance of Safe Rust and Unsafe Rust. It is designed to make using
140226
Safe Rust as ergonomic as possible, but requires extra effort and care when
141227
writing Unsafe Rust. The rest of the book is largely a discussion of the sort
142228
of care that must be taken, and what contracts it is expected of Unsafe Rust
143229
to uphold.
230+
-->
231+
232+
これが安全な Rust と危険な Rust のダンスです。
233+
これは、安全な Rust をできるだけ快適に使えるように、しかし危険な Rust を書くには
234+
それ以上の努力と注意深さが要求されるようなデザインになっています。
235+
この本の残りでは、どういう点に注意しなくはいけないのか、
236+
危険な Rust を維持するための契約とは何なのかを議論します。
237+
238+
144239

145240
[drop flags]: drop-flags.html
146-
[conversions]: conversions.html
241+
[変換]: conversions.html
147242

0 commit comments

Comments
 (0)