From 633de350a86f6ec94affb1233870936a260c9faa Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Wed, 15 Jul 2020 00:31:43 +0900 Subject: [PATCH 01/19] Translate title --- src/SUMMARY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 0f6df88..ca083e6 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -18,7 +18,7 @@ * [高階トレイト境界](hrtb.md) * [Subtyping and Variance](subtyping.md) * [Drop Check](dropck.md) - * [PhantomData](phantom-data.md) + * [ファントムデータ](phantom-data.md) * [借用の分割](borrow-splitting.md) * [型変換](conversions.md) * [型強制](coercions.md) From fa352522377896166222b9773be52274211cc3d9 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 20:14:56 +0900 Subject: [PATCH 02/19] Translate title --- src/phantom-data.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 72fa2e2..17e53af 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -1,4 +1,8 @@ + + +# ファントムデータ When working with unsafe code, we can often end up in a situation where types or lifetimes are logically associated with a struct, but not actually From 39229e607de8f84b7c5bbb42b4b068080a338771 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 20:31:52 +0900 Subject: [PATCH 03/19] Translate a paragraph --- src/phantom-data.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 17e53af..2f719d4 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -4,10 +4,16 @@ # ファントムデータ + + +アンセーフなコードを扱っているとき、しばしば型やライフタイムが論理的に構造体に結びついているけれども、 +フィールドの一部には結びついていない状況に陥る事があります。 +例えば、 `&'a [T]` に対する `Iter` は (大体) 以下のように定義されます。 ```rust,ignore struct Iter<'a, T: 'a> { From 87395afdc1897bf6c919733438b689a73df8918a Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 21:06:57 +0900 Subject: [PATCH 04/19] Translate a paragraph --- src/phantom-data.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 2f719d4..92db9cb 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -22,11 +22,19 @@ struct Iter<'a, T: 'a> { } ``` + + +しかし、 `'a` は構造体の本体内では使用されないため、 `'a` は*無制限*のライフタイムとなります。 +これが過去に引き起こしてきた問題のために、無制限のライフタイムと無制限の型は、 +構造体の定義内では*禁じられています*。それ故に、なんとかして本体内にあるこれらの型を +参照しなければなりません。正しくこれを行なうことは、正しい変性とドロップチェックを +得るために必要です。 We do this using `PhantomData`, which is a special marker type. `PhantomData` consumes no space, but simulates a field of the given type for the purpose of From cae6c04b9cfc36a969a2eb64e66b6bd6a1f330d6 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 21:22:05 +0900 Subject: [PATCH 05/19] Translate a paragraph --- src/phantom-data.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 92db9cb..2bf58bd 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -36,11 +36,18 @@ correct variance and drop checking. 参照しなければなりません。正しくこれを行なうことは、正しい変性とドロップチェックを 得るために必要です。 + + +これを、 `PhantomData` という、特別なマーカー型を使って行ないます。 +`PhantomData` はスペースを消費しませんが、静的分析のために、与えられた型のフィールドを装います。 +これは、明白に型システムに、欲しい変種の種類を伝えるよりも、エラーが起こりにくいと思われていた一方、 +例えばドロップチェッカが必要とする情報など、利便なものを提供していました。 Iter logically contains a bunch of `&'a T`s, so this is exactly what we tell the PhantomData to simulate: From 370d4a923af3138fc6fe3c74eeb2a4fe848825ae Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 21:45:14 +0900 Subject: [PATCH 06/19] Translate a sentence --- src/phantom-data.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 2bf58bd..7edf808 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -49,8 +49,13 @@ useful such as the information needed by drop check. これは、明白に型システムに、欲しい変種の種類を伝えるよりも、エラーが起こりにくいと思われていた一方、 例えばドロップチェッカが必要とする情報など、利便なものを提供していました。 + + +Iter は論理的には沢山の `&'a T` を保持しているため、この型が、 PhantomData に +装うよう伝えるものです。 ``` use std::marker; From 41e3027305984fa9b30746983f45f440c53d6225 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 21:46:07 +0900 Subject: [PATCH 07/19] Translate a paragraph --- src/phantom-data.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 7edf808..81e70f2 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -67,8 +67,13 @@ struct Iter<'a, T: 'a> { } ``` + + +これでよし。ライフタイムには制限が付き、イテレータは `'a` と `T` において変性になります。 +全てうまく行きます。 Another important example is Vec, which is (approximately) defined as follows: From 6f55dee86459ce5b598b60ddfd4f3cb2cc6620a4 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 21:46:39 +0900 Subject: [PATCH 08/19] Translate a sentence --- src/phantom-data.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 81e70f2..21b4a68 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -75,7 +75,11 @@ over `'a` and `T`. Everything Just Works. これでよし。ライフタイムには制限が付き、イテレータは `'a` と `T` において変性になります。 全てうまく行きます。 + + +もう一つ重要な例は Vec で、これは (大体) 以下のように定義されます。 ``` struct Vec { From 00df4481c18b54eaa2b8d47724501345dd73c755 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 21:47:24 +0900 Subject: [PATCH 09/19] Translate a code comment --- src/phantom-data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/phantom-data.md b/src/phantom-data.md index 21b4a68..bfc11d8 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -83,7 +83,7 @@ Another important example is Vec, which is (approximately) defined as follows: ``` struct Vec { - data: *const T, // *const for variance! + data: *const T, // 変性を得るため *const です! len: usize, cap: usize, } From d45d1831d7e54af7992c976e67955e56675762ad Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 21:53:58 +0900 Subject: [PATCH 10/19] Translate paragraphs --- src/phantom-data.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index bfc11d8..a547218 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -89,11 +89,21 @@ struct Vec { } ``` + + +前の例と違い、これは、全てが期待しているものと全く同じ*ように見えます*。 +Vec の全てのジェネリックな引数は少なくとも1つのフィールドに現れます。 +さあ準備完了! + + +いいえ。 The drop checker will generously determine that `Vec` does not own any values of type T. This will in turn make it conclude that it doesn't need to worry From 430e2cc2948f086c4785d12d6d0286176322f713 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 23:11:00 +0900 Subject: [PATCH 11/19] Translate a paragraph --- src/phantom-data.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index a547218..94734c7 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -105,11 +105,18 @@ Nope. いいえ。 + + +ドロップチェッカは、 `Vec` がいかなる型 `T` の値も持たないと惜しみなく決定するでしょう。 +これは結果的に、ドロップチェックの健全性を決定するために Vec のデストラクタ内で、 Vec がなにか T の +値をドロップするということを心配する必要がないと結論付けます。 +この結果、 Vec のデストラクタを使用して、無制限という性質を作り出すことを可能にするのです。 In order to tell dropck that we *do* own values of type T, and therefore may drop some T's when *we* drop, we must add an extra PhantomData saying exactly From b1d59997298cda8a9ffa5949d0d1d46637da5b89 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 23:22:53 +0900 Subject: [PATCH 12/19] Translate a paragraph we = Vec --- src/phantom-data.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 94734c7..caa678c 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -118,9 +118,15 @@ Vec's destructor. 値をドロップするということを心配する必要がないと結論付けます。 この結果、 Vec のデストラクタを使用して、無制限という性質を作り出すことを可能にするのです。 + + +ドロップチェックに、型 T の値を*本当に*保持していて、それ故に *Vec* が +ドロップする際、なにか T の値もドロップするかもしれないと伝えるために、 +追加の PhantomData を加えなければなりません。 ``` use std::marker; From 6940d9c9ea00a315b2582b76bf7f59602e9bd51b Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 23:23:58 +0900 Subject: [PATCH 13/19] Translate a code comment --- src/phantom-data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/phantom-data.md b/src/phantom-data.md index caa678c..82238af 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -132,7 +132,7 @@ that: use std::marker; struct Vec { - data: *const T, // *const for covariance! + data: *const T, // 変性を得るため *const です! len: usize, cap: usize, _marker: marker::PhantomData, From db77d47fb91ffc6abf430ae7003b491e935fa78d Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 23:34:09 +0900 Subject: [PATCH 14/19] Translate a paragraph --- src/phantom-data.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index 82238af..edfab37 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -139,8 +139,13 @@ struct Vec { } ``` + + +アロケーションを持つ生ポインタは普及しているパターンですので、 +標準ライブラリが以下の機能を持つ、 `Unique` と呼ばれるユーティリティを作りました。 * wraps a `*const T` for variance * includes a `PhantomData` From 467671e6cf41f98b3503b2917024640de3528204 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Tue, 16 Jun 2020 23:47:49 +0900 Subject: [PATCH 15/19] Translate a list --- src/phantom-data.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/phantom-data.md b/src/phantom-data.md index edfab37..5883a78 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -147,7 +147,14 @@ standard library made a utility for itself called `Unique` which: アロケーションを持つ生ポインタは普及しているパターンですので、 標準ライブラリが以下の機能を持つ、 `Unique` と呼ばれるユーティリティを作りました。 + + +* 変性を得るため、 `*const T` をラップします +* `PhantomData` を含みます +* T が Send/Sync を保持しているかのように、自動的に継承します +* ヌルポインタ最適化のため、ポインタを非 0 としてマークします From ed56bf76ba37e2125d4e08ad8969f0bbcffdf579 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Wed, 17 Jun 2020 09:13:09 +0900 Subject: [PATCH 16/19] Add missing subject --- src/phantom-data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/phantom-data.md b/src/phantom-data.md index 5883a78..742f195 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -124,7 +124,7 @@ drop some T's when *we* drop, we must add an extra PhantomData saying exactly that: --> -ドロップチェックに、型 T の値を*本当に*保持していて、それ故に *Vec* が +ドロップチェックに、 Vec が型 T の値を*本当に*保持していて、それ故に *Vec* が ドロップする際、なにか T の値もドロップするかもしれないと伝えるために、 追加の PhantomData を加えなければなりません。 From ad49c02e7ddc00a5004754fd749fb20be15b0e4f Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Wed, 17 Jun 2020 09:34:43 +0900 Subject: [PATCH 17/19] Fix translation --- src/phantom-data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/phantom-data.md b/src/phantom-data.md index 742f195..c947702 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -114,7 +114,7 @@ Vec's destructor. --> ドロップチェッカは、 `Vec` がいかなる型 `T` の値も持たないと惜しみなく決定するでしょう。 -これは結果的に、ドロップチェックの健全性を決定するために Vec のデストラクタ内で、 Vec がなにか T の +これは結果的に、ドロップチェックの健全性の決定によって、 Vec のデストラクタ内で、 Vec がなにか T の 値をドロップするということを心配する必要がないと結論付けます。 この結果、 Vec のデストラクタを使用して、無制限という性質を作り出すことを可能にするのです。 From 304073f838341e16a856d65e8ef0028217deb123 Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Wed, 15 Jul 2020 00:34:18 +0900 Subject: [PATCH 18/19] Translate a paragraph we = Vec --- src/phantom-data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/phantom-data.md b/src/phantom-data.md index c947702..788a306 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -124,7 +124,7 @@ drop some T's when *we* drop, we must add an extra PhantomData saying exactly that: --> -ドロップチェックに、 Vec が型 T の値を*本当に*保持していて、それ故に *Vec* が +ドロップチェックに、型 T の値を*本当に*保持していて、それ故に *Vec* が ドロップする際、なにか T の値もドロップするかもしれないと伝えるために、 追加の PhantomData を加えなければなりません。 From 95849d174f01175ad9f674ee020c2620034efbda Mon Sep 17 00:00:00 2001 From: toku-sa-n Date: Wed, 17 Jun 2020 09:13:09 +0900 Subject: [PATCH 19/19] Add missing subject --- src/phantom-data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/phantom-data.md b/src/phantom-data.md index 788a306..c947702 100644 --- a/src/phantom-data.md +++ b/src/phantom-data.md @@ -124,7 +124,7 @@ drop some T's when *we* drop, we must add an extra PhantomData saying exactly that: --> -ドロップチェックに、型 T の値を*本当に*保持していて、それ故に *Vec* が +ドロップチェックに、 Vec が型 T の値を*本当に*保持していて、それ故に *Vec* が ドロップする際、なにか T の値もドロップするかもしれないと伝えるために、 追加の PhantomData を加えなければなりません。