Skip to content

Commit c223f9e

Browse files
committed
update docs
1 parent 5080e59 commit c223f9e

File tree

2 files changed

+260
-100
lines changed

2 files changed

+260
-100
lines changed

docs/print.html

Lines changed: 130 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -333,85 +333,165 @@ <h1 class="menu-title"></h1>
333333
to declare that the implementation of that trait has adhered to whatever
334334
contracts the trait's documentation requires.
335335
-->
336-
<p>The standard library has a number of unsafe functions, including:</p>
336+
<p>コードブロックに使われた <code>unsafe</code> は、そのブロックで呼ばれている危険な関数が要求する契約は守られていて、コードが信頼出来る事を意味します。<code>unsafe</code> を trait の実装に使うと、その実装が trait のドキュメントに書かれている契約に準拠している事を示します。</p>
337+
<!--
338+
The standard library has a number of unsafe functions, including:
339+
-->
340+
<p>標準ライブラリにはいくつもの危険な関数があります。例えば、</p>
341+
<!--
342+
* `slice::get_unchecked`, which performs unchecked indexing, allowing
343+
memory safety to be freely violated.
344+
* `mem::transmute` reinterprets some value as having a given type, bypassing
345+
type safety in arbitrary ways (see [conversions] for details).
346+
* Every raw pointer to a sized type has an intrinstic `offset` method that
347+
invokes Undefined Behavior if the passed offset is not "in bounds" as
348+
defined by LLVM.
349+
* All FFI functions are `unsafe` because the other language can do arbitrary
350+
operations that the Rust compiler can't check.
351+
-->
337352
<ul>
338-
<li><code>slice::get_unchecked</code>, which performs unchecked indexing, allowing
339-
memory safety to be freely violated.</li>
340-
<li><code>mem::transmute</code> reinterprets some value as having a given type, bypassing
341-
type safety in arbitrary ways (see <a href="conversions.html">conversions</a> for details).</li>
342-
<li>Every raw pointer to a sized type has an intrinstic <code>offset</code> method that
343-
invokes Undefined Behavior if the passed offset is not &quot;in bounds&quot; as
344-
defined by LLVM.</li>
345-
<li>All FFI functions are <code>unsafe</code> because the other language can do arbitrary
346-
operations that the Rust compiler can't check.</li>
353+
<li><code>slice::get_unchecked</code> は未チェックのインデックス参照を実行します。自由自在にメモリ安全性に違反できます。</li>
354+
<li><code>mem::transmute</code> は、型安全の仕組みを好きなようにすり抜けて、ある値が特定の型であると再解釈します(詳細は <a href="conversions.html">変換</a> をみてください)。</li>
355+
<li>サイズが確定している型の生のポインタには、固有の <code>offset</code> メソッドがあります。渡されたオフセットが LLVM が定める &quot;境界内&quot; になければ、未定義の挙動を引き起こします。</li>
356+
<li>すべての FFI 関数は <code>unsafe</code> です。なぜなら Rust コンパイラは、他の言語が実行するどんな操作もチェックできないからです。</li>
347357
</ul>
348-
<p>As of Rust 1.0 there are exactly two unsafe traits:</p>
358+
<!--
359+
As of Rust 1.0 there are exactly two unsafe traits:
360+
-->
361+
<p>Rust 1.0 現在、危険な traits は 2 つしかありません。</p>
362+
<!--
363+
* `Send` is a marker trait (a trait with no API) that promises implementors are
364+
safe to send (move) to another thread.
365+
* `Sync` is a marker trait that promises threads can safely share implementors
366+
through a shared reference.
367+
-->
349368
<ul>
350-
<li><code>Send</code> is a marker trait (a trait with no API) that promises implementors are
351-
safe to send (move) to another thread.</li>
352-
<li><code>Sync</code> is a marker trait that promises threads can safely share implementors
353-
through a shared reference.</li>
369+
<li><code>Send</code> は API を持たないマーカー trait で、実装された型が他のスレッドに安全に送れる(move できる)ことを約束します。</li>
370+
<li><code>Sync</code> もマーカー trait で、この trait を実装した型は、共有リファレンスを使って安全に複数のスレッドで共有できる事を約束します。</li>
354371
</ul>
355-
<p>Much of the Rust standard library also uses Unsafe Rust internally, although
372+
<!--
373+
Much of the Rust standard library also uses Unsafe Rust internally, although
356374
these implementations are rigorously manually checked, and the Safe Rust
357-
interfaces provided on top of these implementations can be assumed to be safe.</p>
358-
<p>The need for all of this separation boils down a single fundamental property
359-
of Safe Rust:</p>
360-
<p><strong>No matter what, Safe Rust can't cause Undefined Behavior.</strong></p>
361-
<p>The design of the safe/unsafe split means that Safe Rust inherently has to
375+
interfaces provided on top of these implementations can be assumed to be safe.
376+
-->
377+
<p>また、多くの Rust 標準ライブラリは内部で危険な Rust を使っています。ただ、標準ライブラリの
378+
実装はプログラマが徹底的にチェックしているので、危険な Rust の上に実装された安全な Rust は安全であると仮定して良いでしょう。</p>
379+
<!--
380+
The need for all of this separation boils down a single fundamental property
381+
of Safe Rust:
382+
<p><strong>No matter what, Safe Rust can't cause Undefined Behavior.</strong>
383+
--&gt;</p>
384+
<p>このように分離する目的は、結局のところ、安全な Rust のたった一つの基本的な性質にあります。</p>
385+
<p><strong>どうやっても、安全な Rust では未定義な挙動を起こせない。</strong></p>
386+
<!--
387+
The design of the safe/unsafe split means that Safe Rust inherently has to
362388
trust that any Unsafe Rust it touches has been written correctly (meaning
363389
the Unsafe Rust actually maintains whatever contracts it is supposed to
364390
maintain). On the other hand, Unsafe Rust has to be very careful about
365-
trusting Safe Rust.</p>
366-
<p>As an example, Rust has the <code>PartialOrd</code> and <code>Ord</code> traits to differentiate
367-
between types which can &quot;just&quot; be compared, and those that provide a total
391+
trusting Safe Rust.
392+
-->
393+
<p>このように安全と危険の分けると、安全な Rust は、自分が利用する危険な Rust が正しく書かれている事、
394+
つまり危険な Rust がそれが守るべき契約を実際に守っている事、を本質的に信頼しなくてはいけません。
395+
逆に、危険な Rust は安全な Rust を注意して信頼しなくてはいけません。</p>
396+
<!--
397+
As an example, Rust has the `PartialOrd` and `Ord` traits to differentiate
398+
between types which can "just" be compared, and those that provide a total
368399
ordering (where every value of the type is either equal to, greater than,
369400
or less than any other value of the same type). The sorted map type
370-
<code>BTreeMap</code> doesn't make sense for partially-ordered types, and so it
371-
requires that any key type for it implements the <code>Ord</code> trait. However,
372-
<code>BTreeMap</code> has Unsafe Rust code inside of its implementation, and this
373-
Unsafe Rust code cannot assume that any <code>Ord</code> implementation it gets makes
374-
sense. The unsafe portions of <code>BTreeMap</code>'s internals have to be careful to
375-
maintain all necessary contracts, even if a key type's <code>Ord</code> implementation
376-
does not implement a total ordering.</p>
377-
<p>Unsafe Rust cannot automatically trust Safe Rust. When writing Unsafe Rust,
401+
`BTreeMap` doesn't make sense for partially-ordered types, and so it
402+
requires that any key type for it implements the `Ord` trait. However,
403+
`BTreeMap` has Unsafe Rust code inside of its implementation, and this
404+
Unsafe Rust code cannot assume that any `Ord` implementation it gets makes
405+
sense. The unsafe portions of `BTreeMap`'s internals have to be careful to
406+
maintain all necessary contracts, even if a key type's `Ord` implementation
407+
does not implement a total ordering.
408+
-->
409+
<p>例えば、Rust には <code>PartialOrd</code> trait と <code>Ord</code> trait があり、単に比較可能な型と全順序が
410+
定義されている型(任意の値が同じ型の他の値と比べて等しいか、大きいか、小さい)とを区別します。
411+
順序つきマップの <code>BTreeMap</code> は半順序の型には使えないので、キーとして使われる型が <code>Ord</code> trait を
412+
実装している事を要求します。
413+
しかし <code>BTreeMap</code> の実装は危険な Rust が使っていて、危険な Rust は渡された <code>Ord</code> の実装が
414+
適切であるとは仮定できません。
415+
<code>BTreeMap</code> 内部の危険な部分は、キー型の <code>Ord</code> の実装が全順序ではない場合でも、必要な契約が
416+
すべて守られるよう注意深く書かれなくてはいけません。</p>
417+
<!--
418+
Unsafe Rust cannot automatically trust Safe Rust. When writing Unsafe Rust,
378419
you must be careful to only rely on specific Safe Rust code, and not make
379420
assumptions about potential future Safe Rust code providing the same
380-
guarantees.</p>
381-
<p>This is the problem that <code>unsafe</code> traits exist to resolve. The <code>BTreeMap</code>
421+
guarantees.
422+
-->
423+
<p>危険な Rust は安全な Rust を無意識には信頼できません。危険な Rust コードを書くときには、
424+
安全な Rust の特定のコードのみに依存する必要があり、
425+
安全な Rust が将来にわたって同様の安全性を提供すると仮定してはいけません。</p>
426+
<!--
427+
This is the problem that `unsafe` traits exist to resolve. The `BTreeMap`
382428
type could theoretically require that keys implement a new trait called
383-
<code>UnsafeOrd</code>, rather than <code>Ord</code>, that might look like this:</p>
429+
`UnsafeOrd`, rather than `Ord`, that might look like this:
430+
-->
431+
<p>この問題を解決するために <code>unsafe</code> な trait が存在します。理論上は、<code>BTreeMap</code> 型は
432+
キーが <code>Ord</code> ではなく、新しい trait <code>UnsafeOrd</code> を実装する事を要求する事ができます。
433+
このようなコードになるでしょう。</p>
384434
<pre><code class="language-rust">use std::cmp::Ordering;
385435

386436
unsafe trait UnsafeOrd {
387437
fn cmp(&amp;self, other: &amp;Self) -&gt; Ordering;
388438
}
389439
</code></pre>
390-
<p>Then, a type would use <code>unsafe</code> to implement <code>UnsafeOrd</code>, indicating that
440+
<!--
441+
Then, a type would use `unsafe` to implement `UnsafeOrd`, indicating that
391442
they've ensured their implementation maintains whatever contracts the
392443
trait expects. In this situation, the Unsafe Rust in the internals of
393-
<code>BTreeMap</code> could trust that the key type's <code>UnsafeOrd</code> implementation is
444+
`BTreeMap` could trust that the key type's `UnsafeOrd` implementation is
394445
correct. If it isn't, it's the fault of the unsafe trait implementation
395-
code, which is consistent with Rust's safety guarantees.</p>
396-
<p>The decision of whether to mark a trait <code>unsafe</code> is an API design choice.
446+
code, which is consistent with Rust's safety guarantees.
447+
-->
448+
<p>この場合、<code>UnsafeOrd</code> を実装する型は、この trait が期待する契約に準拠している事を示すために
449+
<code>unsafe</code> キーワードを使うことになります。
450+
この状況では、<code>BTreeMap</code> 内部の危険な Rust は、キー型が <code>UnsafeOrd</code> を正しく実装していると
451+
信用する事ができます。もしそうで無ければ、それは trait の実装の問題であり、
452+
これは Rust の安全性の保証と一致しています。</p>
453+
<!--
454+
The decision of whether to mark a trait `unsafe` is an API design choice.
397455
Rust has traditionally avoided marking traits unsafe because it makes Unsafe
398-
Rust pervasive, which is not desirable. <code>Send</code> and <code>Sync</code> are marked unsafe
399-
because thread safety is a <em>fundamental property</em> that unsafe code can't
456+
Rust pervasive, which is not desirable. `Send` and `Sync` are marked unsafe
457+
because thread safety is a *fundamental property* that unsafe code can't
400458
possibly hope to defend against in the way it could defend against a bad
401-
<code>Ord</code> implementation. The decision of whether to mark your own traits <code>unsafe</code>
402-
depends on the same sort of consideration. If <code>unsafe</code> code cannot reasonably
459+
`Ord` implementation. The decision of whether to mark your own traits `unsafe`
460+
depends on the same sort of consideration. If `unsafe` code cannot reasonably
403461
expect to defend against a bad implementation of the trait, then marking the
404-
trait <code>unsafe</code> is a reasonable choice.</p>
405-
<p>As an aside, while <code>Send</code> and <code>Sync</code> are <code>unsafe</code> traits, they are
462+
trait `unsafe` is a reasonable choice.
463+
-->
464+
<p>trait に <code>unsafe</code> をつけるかどうかは API デザインにおける選択です。
465+
Rust では従来 <code>unsafe</code> な trait を避けてきました。そうしないと危険な Rust が
466+
蔓延してしまい、好ましくないからです。
467+
<code>Send</code><code>Sync</code><code>unsafe</code> となっているのは、スレッドの安全性が <em>基本的な性質</em> であり、
468+
間違った <code>Ord</code> の実装に対して危険なコードが防衛できるのと同様の意味では防衛できないからです。
469+
あなたが宣言した trait を <code>unsafe</code> とマークするかどうかも、同じようにじっくりと考えてください。
470+
もし <code>unsafe</code> なコードがその trait の間違った実装から防御することが合理的に不可能であるなら、
471+
その trait を <code>unsafe</code> とするのは合理的な選択です。</p>
472+
<!--
473+
As an aside, while `Send` and `Sync` are `unsafe` traits, they are
406474
automatically implemented for types when such derivations are provably safe
407-
to do. <code>Send</code> is automatically derived for all types composed only of values
408-
whose types also implement <code>Send</code>. <code>Sync</code> is automatically derived for all
409-
types composed only of values whose types also implement <code>Sync</code>.</p>
410-
<p>This is the dance of Safe Rust and Unsafe Rust. It is designed to make using
475+
to do. `Send` is automatically derived for all types composed only of values
476+
whose types also implement `Send`. `Sync` is automatically derived for all
477+
types composed only of values whose types also implement `Sync`.
478+
-->
479+
<p>余談ですが、<code>unsafe</code> な trait である <code>Send</code><code>Sync</code> は、それらを実装する事が安全だと
480+
実証可能な場合には自動的に実装されます。
481+
<code>Send</code> は、<code>Send</code> を実装した型だけから構成される型に対して、自動的に実装されます。
482+
<code>Sync</code> は、<code>Sync</code> を実装した型だけから構成される型に対して、自動的に実装されます。</p>
483+
<!--
484+
This is the dance of Safe Rust and Unsafe Rust. It is designed to make using
411485
Safe Rust as ergonomic as possible, but requires extra effort and care when
412486
writing Unsafe Rust. The rest of the book is largely a discussion of the sort
413487
of care that must be taken, and what contracts it is expected of Unsafe Rust
414-
to uphold.</p>
488+
to uphold.
489+
-->
490+
<p>これが安全な Rust と危険な Rust のダンスです。
491+
これは、安全な Rust をできるだけ快適に使えるように、しかし危険な Rust を書くには
492+
それ以上の努力と注意深さが要求されるようなデザインになっています。
493+
この本の残りでは、どういう点に注意しなくはいけないのか、
494+
危険な Rust を維持するための契約とは何なのかを議論します。</p>
415495
<a class="header" href="#working-with-unsafe" name="working-with-unsafe"><h1>Working with Unsafe</h1></a>
416496
<p>Rust generally only gives us the tools to talk about Unsafe Rust in a scoped and
417497
binary manner. Unfortunately, reality is significantly more complicated than

0 commit comments

Comments
 (0)