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