|
1 |
| -# Poisoning |
| 1 | +<!--# Poisoning--> |
| 2 | +#ポイズニング |
2 | 3 |
|
| 4 | +<!-- |
3 | 5 | Although all unsafe code *must* ensure it has minimal exception safety, not all
|
4 | 6 | types ensure *maximal* exception safety. Even if the type does, your code may
|
5 | 7 | ascribe additional meaning to it. For instance, an integer is certainly
|
6 | 8 | exception-safe, but has no semantics on its own. It's possible that code that
|
7 | 9 | panics could fail to correctly update the integer, producing an inconsistent
|
8 | 10 | program state.
|
| 11 | +--> |
| 12 | +全てのunsafeな型は最低限の例外安全性を満たしていることが**必要です**が、全ての |
| 13 | +unsafeな型が**最大限**の例外安全性を満たしている必要はありません。 |
| 14 | +仮に型自体が満たしていたとしても、実装が別の意味を暗黙に付与してしまう場合も |
| 15 | +あります。例えば整数型は間違いなく例外安全ですが、その(訳注: 最大限の例外安全性 |
| 16 | +を担保する)セマンティクスを独自に持つわけではないため、整数をアップデートする |
| 17 | +際にpanicを起こすと、プログラムが一貫性のない状態に陥る可能性があります。 |
9 | 18 |
|
| 19 | +<!-- |
10 | 20 | This is *usually* fine, because anything that witnesses an exception is about
|
11 | 21 | to get destroyed. For instance, if you send a Vec to another thread and that
|
12 | 22 | thread panics, it doesn't matter if the Vec is in a weird state. It will be
|
13 | 23 | dropped and go away forever. However some types are especially good at smuggling
|
14 | 24 | values across the panic boundary.
|
| 25 | +--> |
| 26 | +これは**通常は**問題になることはありません。というのも例外を発見した処理は直後に |
| 27 | +死ぬためです。例えばVecを別のスレッドに送り、そのスレッドがパニックし、結果として |
| 28 | +Vecが奇妙な状態に陥ったとしても、dropされて永久に闇の彼方に葬られてしまうためです。 |
| 29 | +とはいえ型によってはpanicの境界をまたいでくる場合もあります。 |
15 | 30 |
|
| 31 | +<!-- |
16 | 32 | These types may choose to explicitly *poison* themselves if they witness a panic.
|
17 | 33 | Poisoning doesn't entail anything in particular. Generally it just means
|
18 | 34 | preventing normal usage from proceeding. The most notable example of this is the
|
19 | 35 | standard library's Mutex type. A Mutex will poison itself if one of its
|
20 | 36 | MutexGuards (the thing it returns when a lock is obtained) is dropped during a
|
21 | 37 | panic. Any future attempts to lock the Mutex will return an `Err` or panic.
|
| 38 | +--> |
| 39 | +こういった型は、panicに直面した際に、意図的に自分自身を**poison**する可能性があり |
| 40 | +ます。poisoningは自体は特に何か別の事態を引き起こすわけではありません。一般的に |
| 41 | +通常の手続きの継続を止めるべきであることを表しています。よく知られた例として |
| 42 | +標準ライブラリのMutex型があります。この型は対応するMutexGuards(`lock()`の返り値 |
| 43 | +の型)が、panicによってdropされた際に自分自身をpoisonします。以後Mutexをlockしよう |
| 44 | +とすると`Err`を返すかpanicします。 |
22 | 45 |
|
| 46 | +<!-- |
23 | 47 | Mutex poisons not for true safety in the sense that Rust normally cares about. It
|
24 | 48 | poisons as a safety-guard against blindly using the data that comes out of a Mutex
|
25 | 49 | that has witnessed a panic while locked. The data in such a Mutex was likely in the
|
26 | 50 | middle of being modified, and as such may be in an inconsistent or incomplete state.
|
27 | 51 | It is important to note that one cannot violate memory safety with such a type
|
28 | 52 | if it is correctly written. After all, it must be minimally exception-safe!
|
| 53 | +--> |
| 54 | +Mutexのpoisonは、通常の文脈で語られるRustの安全性とは異なる用途のためのものです。 |
| 55 | +Mutexを扱うスレッドがlock中にパニックを引き起こした場合、Mutexの中のデータは変更中 |
| 56 | +であった可能性が高く、一貫性を欠いていたり変更が未完了の状態であったりするため、 |
| 57 | +そのようなデータを盲目的に扱う危険性に対する安全装置として動作します。 |
| 58 | +注意しておきたいのはそのような型が適切に実装されていた場合、メモリ安全性を満たさない |
| 59 | +わけではないという点です。つまるところ、最低限の例外安全性は満たしていなくては |
| 60 | +ならないということです。 |
29 | 61 |
|
| 62 | +<!-- |
30 | 63 | However if the Mutex contained, say, a BinaryHeap that does not actually have the
|
31 | 64 | heap property, it's unlikely that any code that uses it will do
|
32 | 65 | what the author intended. As such, the program should not proceed normally.
|
33 | 66 | Still, if you're double-plus-sure that you can do *something* with the value,
|
34 | 67 | the Mutex exposes a method to get the lock anyway. It *is* safe, after all.
|
35 | 68 | Just maybe nonsense.
|
| 69 | +--> |
| 70 | +しかしながら、Mutexが例えばBinaryHeapを持っていたとして、その値が実際にはヒープ |
| 71 | +として要件を満たさなかったような場合、そのデータ構造を利用するプログラムが作成者の |
| 72 | +意図通りの挙動をするということは考えにくいです。通常とは異なる振る舞いをする |
| 73 | +でしょう。とはいえ、十分に注意すればそのような場合でもその値が**何かに**使える |
| 74 | +可能性はあります。safe**では**あるのです。ただ、それはナンセンスであるということと |
| 75 | +は矛盾しないのです。 |
0 commit comments