Skip to content

Commit c4af511

Browse files
committed
Update Unsafe to 1.9
- Update the contents. - Change the comment style to use the same style to other pages.
1 parent 66a52ad commit c4af511

File tree

2 files changed

+90
-160
lines changed

2 files changed

+90
-160
lines changed

1.9/ja/book/unsafe.md

Lines changed: 90 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
% Unsafe
22

3-
<!--
4-
Rust’s main draw is its powerful static guarantees about behavior. But safety
5-
checks are conservative by nature: there are some programs that are actually
6-
safe, but the compiler is not able to verify this is true. To write these kinds
7-
of programs, we need to tell the compiler to relax its restrictions a bit. For
8-
this, Rust has a keyword, `unsafe`. Code using `unsafe` has less restrictions
9-
than normal code does.
10-
-->
3+
<!-- Rust’s main draw is its powerful static guarantees about behavior. But safety -->
4+
<!-- checks are conservative by nature: there are some programs that are actually -->
5+
<!-- safe, but the compiler is not able to verify this is true. To write these kinds -->
6+
<!-- of programs, we need to tell the compiler to relax its restrictions a bit. For -->
7+
<!-- this, Rust has a keyword, `unsafe`. Code using `unsafe` has fewer restrictions -->
8+
<!-- than normal code does. -->
119
Rustの主たる魅力は、プログラムの動作についての強力で静的な保証です。
1210
しかしながら、安全性検査は本来保守的なものです。
1311
すなわち、実際には安全なのに、そのことがコンパイラには検証できないプログラムがいくらか存在します。
1412
その類のプログラムを書くためには、制約を少し緩和するようコンパイラに対して伝えることが要ります。
1513
そのために、Rustには `unsafe` というキーワードがあります。
1614
`unsafe` を使ったコードは、普通のコードよりも制約が少なくなります。
1715

18-
<!--
19-
Let’s go over the syntax, and then we’ll talk semantics. `unsafe` is used in
20-
four contexts. The first one is to mark a function as unsafe:
21-
-->
16+
<!-- Let’s go over the syntax, and then we’ll talk semantics. `unsafe` is used in -->
17+
<!-- four contexts. The first one is to mark a function as unsafe: -->
2218
まずシンタックスをみて、それからセマンティクスについて話しましょう。
2319
`unsafe` は4つの場面で使われます。
2420
1つめは、関数がアンセーフであることを印付ける場合です。
@@ -30,10 +26,8 @@ unsafe fn danger_will_robinson() {
3026
}
3127
```
3228

33-
<!--
34-
All functions called from [FFI][ffi] must be marked as `unsafe`, for example.
35-
The second use of `unsafe` is an unsafe block:
36-
-->
29+
<!-- All functions called from [FFI][ffi] must be marked as `unsafe`, for example. -->
30+
<!-- The second use of `unsafe` is an unsafe block: -->
3731
たとえば、[FFI][ffi]から呼び出されるすべての関数は`unsafe`で印付けることが必要です。
3832
`unsafe`の2つめの用途は、アンセーフブロックです。
3933

@@ -46,37 +40,33 @@ unsafe {
4640
}
4741
```
4842

49-
<!--The third is for unsafe traits:-->
43+
<!-- The third is for unsafe traits: -->
5044
3つめは、アンセーフトレイトです。
5145

5246
```rust
5347
unsafe trait Scary { }
5448
```
5549

56-
<!--And the fourth is for `impl`ementing one of those traits:-->
50+
<!-- And the fourth is for `impl`ementing one of those traits: -->
5751
そして、4つめは、そのアンセーフトレイトを実装する場合です。
5852

5953
```rust
6054
# unsafe trait Scary { }
6155
unsafe impl Scary for i32 {}
6256
```
6357

64-
<!--
65-
It’s important to be able to explicitly delineate code that may have bugs that
66-
cause big problems. If a Rust program segfaults, you can be sure it’s somewhere
67-
in the sections marked `unsafe`.
68-
-->
58+
<!-- It’s important to be able to explicitly delineate code that may have bugs that -->
59+
<!-- cause big problems. If a Rust program segfaults, you can be sure the cause is -->
60+
<!-- related to something marked `unsafe`. -->
6961
大きな問題を引き起こすバグがあるかもしれないコードを明示できるのは重要なことです。
7062
もしRustのプログラムがセグメンテーション違反を起こしても、バグは `unsafe` で印付けられた区間のどこかにあると確信できます。
7163

64+
<!-- # What does ‘safe’ mean? -->
7265
# 「安全」とはどういう意味か?
73-
<!--# What does ‘safe’ mean?-->
7466

75-
<!--
76-
Safe, in the context of Rust, means ‘doesn’t do anything unsafe’. It’s also
77-
important to know that there are certain behaviors that are probably not
78-
desirable in your code, but are expressly _not_ unsafe:
79-
-->
67+
<!-- Safe, in the context of Rust, means ‘doesn’t do anything unsafe’. It’s also -->
68+
<!-- important to know that there are certain behaviors that are probably not -->
69+
<!-- desirable in your code, but are expressly _not_ unsafe: -->
8070
Rustの文脈で、安全とは「どのようなアンセーフなこともしない」ことを意味します。
8171

8272
> 訳注:
@@ -86,63 +76,55 @@ Rustの文脈で、安全とは「どのようなアンセーフなこともし
8676

8777
知っておくべき重要なことに、たいていのコードにおいて望ましくないが、アンセーフ _ではない_ とされている動作がいくらか存在するということがあります。
8878

89-
<!--
90-
* Deadlocks
91-
* Leaks of memory or other resources
92-
* Exiting without calling destructors
93-
* Integer overflow
94-
-->
79+
<!-- * Deadlocks -->
80+
<!-- * Leaks of memory or other resources -->
81+
<!-- * Exiting without calling destructors -->
82+
<!-- * Integer overflow -->
9583
* デッドロック
9684
* メモリやその他のリソースのリーク
9785
* デストラクタを呼び出さないプログラム終了
9886
* 整数オーバーフロー
9987

100-
<!--
101-
Rust cannot prevent all kinds of software problems. Buggy code can and will be
102-
written in Rust. These things aren’t great, but they don’t qualify as `unsafe`
103-
specifically.
104-
-->
88+
<!-- Rust cannot prevent all kinds of software problems. Buggy code can and will be -->
89+
<!-- written in Rust. These things aren’t great, but they don’t qualify as `unsafe` -->
90+
<!-- specifically. -->
10591
Rustはソフトウェアが抱えるすべての種類の問題を防げるわけではありません。
10692
Rustでバグのあるコードを書くことはできますし、実際に書かれるでしょう。
10793
これらの動作は良いことではありませんが、特にアンセーフだとは見なされません。
10894

109-
<!--
110-
In addition, the following are all undefined behaviors in Rust, and must be
111-
avoided, even when writing `unsafe` code:
112-
-->
95+
<!-- In addition, the following are all undefined behaviors in Rust, and must be -->
96+
<!-- avoided, even when writing `unsafe` code: -->
11397
さらに、Rustにおいては、次のものは未定義動作で、 `unsafe` コード中であっても、避ける必要があります。
11498

11599
> 訳注:
116-
関数に付いている`unsafe`は「その関数の処理はアンセーフである」ということを表します。
117-
その一方で、ブロックに付いている`unsafe`は「ブロック中の個々の操作はアンセーフだが、全体としては安全な処理である」ということを表します。
118-
避ける必要があるのは、未定義動作が起こりうる処理をアンセーフブロックの中に書くことです。
119-
それは、アンセーフブロックの処理が安全であるために、その内部で未定義動作が決して起こらないことが必要だからです。
120-
アンセーフ関数には安全性の保証が要らないので、未定義動作が起こりうるアンセーフ関数を定義することに問題はありません。
121-
122-
<!--
123-
* Data races
124-
* Dereferencing a null/dangling raw pointer
125-
* Reads of [undef][undef] (uninitialized) memory
126-
* Breaking the [pointer aliasing rules][aliasing] with raw pointers.
127-
* `&mut T` and `&T` follow LLVM’s scoped [noalias][noalias] model, except if
128-
the `&T` contains an `UnsafeCell<U>`. Unsafe code must not violate these
129-
aliasing guarantees.
130-
* Mutating an immutable value/reference without `UnsafeCell<U>`
131-
* Invoking undefined behavior via compiler intrinsics:
132-
* Indexing outside of the bounds of an object with `std::ptr::offset`
133-
(`offset` intrinsic), with
134-
the exception of one byte past the end which is permitted.
135-
* Using `std::ptr::copy_nonoverlapping_memory` (`memcpy32`/`memcpy64`
136-
intrinsics) on overlapping buffers
137-
* Invalid values in primitive types, even in private fields/locals:
138-
* Null/dangling references or boxes
139-
* A value other than `false` (0) or `true` (1) in a `bool`
140-
* A discriminant in an `enum` not included in its type definition
141-
* A value in a `char` which is a surrogate or above `char::MAX`
142-
* Non-UTF-8 byte sequences in a `str`
143-
* Unwinding into Rust from foreign code or unwinding from Rust into foreign
144-
code.
145-
-->
100+
> 関数に付いている`unsafe`は「その関数の処理はアンセーフである」ということを表します。
101+
> その一方で、ブロックに付いている`unsafe`は「ブロック中の個々の操作はアンセーフだが、全体としては安全な処理である」ということを表します。
102+
> 避ける必要があるのは、未定義動作が起こりうる処理をアンセーフブロックの中に書くことです。
103+
> それは、アンセーフブロックの処理が安全であるために、その内部で未定義動作が決して起こらないことが必要だからです。
104+
> アンセーフ関数には安全性の保証が要らないので、未定義動作が起こりうるアンセーフ関数を定義することに問題はありません。
105+
106+
<!-- * Data races -->
107+
<!-- * Dereferencing a null/dangling raw pointer -->
108+
<!-- * Reads of [undef][undef] (uninitialized) memory -->
109+
<!-- * Breaking the [pointer aliasing rules][aliasing] with raw pointers. -->
110+
<!-- * `&mut T` and `&T` follow LLVM’s scoped [noalias][noalias] model, except if -->
111+
<!-- the `&T` contains an `UnsafeCell<U>`. Unsafe code must not violate these -->
112+
<!-- aliasing guarantees. -->
113+
<!-- * Mutating an immutable value/reference without `UnsafeCell<U>` -->
114+
<!-- * Invoking undefined behavior via compiler intrinsics: -->
115+
<!-- * Indexing outside of the bounds of an object with `std::ptr::offset` -->
116+
<!-- (`offset` intrinsic), with -->
117+
<!-- the exception of one byte past the end which is permitted. -->
118+
<!-- * Using `std::ptr::copy_nonoverlapping_memory` (`memcpy32`/`memcpy64` -->
119+
<!-- intrinsics) on overlapping buffers -->
120+
<!-- * Invalid values in primitive types, even in private fields/locals: -->
121+
<!-- * Null/dangling references or boxes -->
122+
<!-- * A value other than `false` (0) or `true` (1) in a `bool` -->
123+
<!-- * A discriminant in an `enum` not included in its type definition -->
124+
<!-- * A value in a `char` which is a surrogate or above `char::MAX` -->
125+
<!-- * Non-UTF-8 byte sequences in a `str` -->
126+
<!-- * Unwinding into Rust from foreign code or unwinding from Rust into foreign -->
127+
<!-- code. -->
146128
* データ競合
147129
* ヌル・ダングリング生ポインタの参照外し
148130
* [undef][undef] (未初期化)メモリの読み出し
@@ -165,100 +147,80 @@ avoided, even when writing `unsafe` code:
165147
[undef]: http://llvm.org/docs/LangRef.html#undefined-values
166148
[aliasing]: http://llvm.org/docs/LangRef.html#pointer-aliasing-rules
167149

150+
<!-- # Unsafe Superpowers -->
168151
# アンセーフの能力
169-
<!--# Unsafe Superpowers-->
170152

171-
<!--
172-
In both unsafe functions and unsafe blocks, Rust will let you do three things
173-
that you normally can not do. Just three. Here they are:
174-
-->
153+
<!-- In both unsafe functions and unsafe blocks, Rust will let you do three things -->
154+
<!-- that you normally can not do. Just three. Here they are: -->
175155
アンセーフ関数・アンセーフブロックでは、Rustは普段できない3つのことをさせてくれます。たった3つです。それは、
176156

177-
<!--
178-
1. Access or update a [static mutable variable][static].
179-
2. Dereference a raw pointer.
180-
3. Call unsafe functions. This is the most powerful ability.
181-
-->
157+
<!-- 1. Access or update a [static mutable variable][static]. -->
158+
<!-- 2. Dereference a raw pointer. -->
159+
<!-- 3. Call unsafe functions. This is the most powerful ability. -->
182160
1. [静的ミュータブル変数][static]のアクセスとアップデート。
183161
2. 生ポインタの参照外し。
184162
3. アンセーフ関数の呼び出し。これが最も強力な能力です。
185163

186-
<!--
187-
That’s it. It’s important that `unsafe` does not, for example, ‘turn off the
188-
borrow checker’. Adding `unsafe` to some random Rust code doesn’t change its
189-
semantics, it won’t just start accepting anything. But it will let you write
190-
things that _do_ break some of the rules.
191-
-->
164+
<!-- That’s it. It’s important that `unsafe` does not, for example, ‘turn off the -->
165+
<!-- borrow checker’. Adding `unsafe` to some random Rust code doesn’t change its -->
166+
<!-- semantics, it won’t start accepting anything. But it will let you write -->
167+
<!-- things that _do_ break some of the rules. -->
192168
以上です。
193169
重要なのは、 `unsafe` が、たとえば「借用チェッカをオフにする」といったことを行わないことです。
194-
Rustのコードの適当な位置に `unsafe` を加えてもセマンティクスは変わらず、何でもただ受理するようになるということにはなりません
170+
Rustのコードの適当な位置に `unsafe` を加えてもセマンティクスは変わらず、何でも受理するようになるということにはなりません
195171
それでも、`unsafe` はルールのいくつかを破るコードを書けるようにはするのです。
196172

197-
<!--
198-
You will also encounter the `unsafe` keyword when writing bindings to foreign
199-
(non-Rust) interfaces. You're encouraged to write a safe, native Rust interface
200-
around the methods provided by the library.
201-
-->
173+
<!-- You will also encounter the `unsafe` keyword when writing bindings to foreign -->
174+
<!-- (non-Rust) interfaces. You're encouraged to write a safe, native Rust interface -->
175+
<!-- around the methods provided by the library. -->
202176
また、`unsafe` キーワードは、Rust以外の言語とのインターフェースを書くときに遭遇するでしょう。
203177
ライブラリの提供するメソッドの周りに、安全な、Rustネイティブのインターフェースを書くことが推奨されています。
204178

205-
<!--
206-
Let’s go over the basic three abilities listed, in order.
207-
-->
179+
<!-- Let’s go over the basic three abilities listed, in order. -->
208180
これから、その基本的な3つの能力を順番に見ていきましょう。
209181

182+
<!-- ## Access or update a `static mut` -->
210183
## `static mut` のアクセスとアップデート。
211-
<!--## Access or update a `static mut`-->
212184

213-
<!--
214-
Rust has a feature called ‘`static mut`’ which allows for mutable global state.
215-
Doing so can cause a data race, and as such is inherently not safe. For more
216-
details, see the [static][static] section of the book.
217-
-->
185+
<!-- Rust has a feature called ‘`static mut`’ which allows for mutable global state. -->
186+
<!-- Doing so can cause a data race, and as such is inherently not safe. For more -->
187+
<!-- details, see the [static][static] section of the book. -->
218188
Rustには「`static mut`」という、ミュータブルでグローバルな状態を実現する機能があります。
219189
これを使うことはデータレースが起こるおそれがあるので、本質的に安全ではありません。
220190
詳細は、この本の[static][static]セクションを参照してください。
221191

222192
[static]: const-and-static.html#static
223193

194+
<!-- ## Dereference a raw pointer -->
224195
## 生ポインタの参照外し
225-
<!--## Dereference a raw pointer-->
226-
227-
<!--
228-
Raw pointers let you do arbitrary pointer arithmetic, and can cause a number of
229-
different memory safety and security issues. In some senses, the ability to
230-
dereference an arbitrary pointer is one of the most dangerous things you can
231-
do. For more on raw pointers, see [their section of the book][rawpointers].
232-
-->
196+
197+
<!-- Raw pointers let you do arbitrary pointer arithmetic, and can cause a number of -->
198+
<!-- different memory safety and security issues. In some senses, the ability to -->
199+
<!-- dereference an arbitrary pointer is one of the most dangerous things you can -->
200+
<!-- do. For more on raw pointers, see [their section of the book][rawpointers]. -->
233201
生ポインタによって任意のポインタ演算が可能になりますが、いくつもの異なるメモリ安全とセキュリティの問題が起こるおそれがあります。
234202
ある意味で、任意のポインタを参照外しする能力は行いうる操作のうち最も危険なもののひとつです。
235203
詳細は、[この本の生ポインタに関するセクション][rawpointers]を参照してください。
236204

237205
[rawpointers]: raw-pointers.html
238206

207+
<!-- ## Call unsafe functions -->
239208
## アンセーフ関数の呼び出し
240-
<!--## Call unsafe functions-->
241209

242-
<!--
243-
This last ability works with both aspects of `unsafe`: you can only call
244-
functions marked `unsafe` from inside an unsafe block.
245-
-->
210+
<!-- This last ability works with both aspects of `unsafe`: you can only call -->
211+
<!-- functions marked `unsafe` from inside an unsafe block. -->
246212
この最後の能力は、`unsafe`の両面とともに働きます。
247213
すなわち、`unsafe`で印付けられた関数は、アンセーフブロックの内部からのみ呼び出すことができます。
248214

249-
<!--
250-
This ability is powerful and varied. Rust exposes some [compiler
251-
intrinsics][intrinsics] as unsafe functions, and some unsafe functions bypass
252-
safety checks, trading safety for speed.
253-
-->
215+
<!-- This ability is powerful and varied. Rust exposes some [compiler -->
216+
<!-- intrinsics][intrinsics] as unsafe functions, and some unsafe functions bypass -->
217+
<!-- safety checks, trading safety for speed. -->
254218
この能力は強力で多彩です。
255219
Rustはいくらかの[compiler intrinsics][intrinsics]をアンセーフ関数として公開しており、また、いくつかのアンセーフ関数は安全性検査を回避することで、安全性とスピードを引き換えています。
256220

257-
<!--
258-
I’ll repeat again: even though you _can_ do arbitrary things in unsafe blocks
259-
and functions doesn’t mean you should. The compiler will act as though you’re
260-
upholding its invariants, so be careful!
261-
-->
221+
<!-- I’ll repeat again: even though you _can_ do arbitrary things in unsafe blocks -->
222+
<!-- and functions doesn’t mean you should. The compiler will act as though you’re -->
223+
<!-- upholding its invariants, so be careful! -->
262224
繰り返しになりますが、アンセーフブロックと関数の内部で任意のことが _できる_ としても、それをすべきだということを意味しません。コンパイラは、あなたが不変量を守っているかのように動作しますから、注意してください!
263225

264226
[intrinsics]: intrinsics.html

0 commit comments

Comments
 (0)