1+ <!--
12# Splitting Borrows
3+ -->
24
5+ # 借用の分割
6+
7+ <!--
38The mutual exclusion property of mutable references can be very limiting when
49working with a composite structure. The borrow checker understands some basic
510stuff, but will fall over pretty easily. It does understand structs
611sufficiently to know that it's possible to borrow disjoint fields of a struct
712simultaneously. So this works today:
13+ -->
14+
15+ 可変参照の相互排他性は、複合構造体を使用している時に非常に制限を課してくる存在となります。
16+ 借用チェッカはいくつか基本事項を理解していますが、本当に簡単にすっ転びます。
17+ 借用チェッカは構造体について十分理解しているため、構造体の別々のフィールドを同時に借用することは可能です。
18+ ですから、このコードは今日動作します。
819
920``` rust
1021struct Foo {
@@ -23,8 +34,13 @@ let c2 = &x.c;
2334println! (" {} {} {} {}" , a , b , c , c2 );
2435```
2536
37+ <!--
2638However borrowck doesn't understand arrays or slices in any way, so this doesn't
2739work:
40+ -->
41+
42+ しかし借用チェッカは、配列やスライスについてはどんな状況でも理解しないため、
43+ このコードは動きません。
2844
2945``` rust,ignore
3046let mut x = [1, 2, 3];
@@ -35,12 +51,15 @@ println!("{} {}", a, b);
3551
3652``` text
3753<anon>:4:14: 4:18 error: cannot borrow `x[..]` as mutable more than once at a time
54+ (エラー: 一度に `x[..]` を可変として 2 回以上借用することはできません)
3855<anon>:4 let b = &mut x[1];
3956 ^~~~
4057<anon>:3:14: 3:18 note: previous borrow of `x[..]` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `x[..]` until the borrow ends
58+ (注釈: 以前の `x[..]` の借用はここで起きています。可変での借用は、その借用が終わるまで、その後のムーブや、借用、 `x[..]` の変更を防ぎます)
4159<anon>:3 let a = &mut x[0];
4260 ^~~~
4361<anon>:6:2: 6:2 note: previous borrow ends here
62+ (注釈: 以前の借用はここで終了しています)
4463<anon>:1 fn main() {
4564<anon>:2 let mut x = [1, 2, 3];
4665<anon>:3 let a = &mut x[0];
@@ -49,19 +68,37 @@ println!("{} {}", a, b);
4968<anon>:6 }
5069 ^
5170error: aborting due to 2 previous errors
71+ (エラー: 上記の 2 つのエラーのため中止)
5272```
5373
74+ <!--
5475While it was plausible that borrowck could understand this simple case, it's
5576pretty clearly hopeless for borrowck to understand disjointness in general
5677container types like a tree, especially if distinct keys actually *do* map
5778to the same value.
79+ -->
80+
81+ 仮に借用チェッカがこの単純なケースを理解しても良さそうに見えるかもしれませんが、
82+ 特に、異なるキーが* 本当に* 同じ値にマップされているときなど、
83+ 木のような一般的なコンテナ内の、各値の素集合性を借用チェッカが理解することを望むのは、
84+ 明らかに無駄です。
5885
86+ <!--
5987In order to "teach" borrowck that what we're doing is ok, we need to drop down
6088to unsafe code. For instance, mutable slices expose a `split_at_mut` function
6189that consumes the slice and returns two mutable slices. One for everything to
6290the left of the index, and one for everything to the right. Intuitively we know
6391this is safe because the slices don't overlap, and therefore alias. However
6492the implementation requires some unsafety:
93+ -->
94+
95+ 借用チェッカに我々が行なっていることが問題ないと "教える" ためには、
96+ アンセーフなコードに落とす必要があります。例えば、可変スライスには、
97+ スライスを消費し 2 つの可変スライスを返す ` split_at_mut ` 関数を使用します。
98+ 片方のスライスはインデックスの左側全てを、もう片方のスライスはインデックスの右側全てを
99+ 使用するためのものです。直感的に、これは安全と分かります。互いのスライスが重ならなず、それゆえ
100+ これらのスライスは元のスライスのエイリアスとなるからです。
101+ しかし、その実装には少しアンセーフなコードを必要とします。
65102
66103``` rust,ignore
67104fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
@@ -75,11 +112,21 @@ fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
75112}
76113```
77114
115+ <!--
78116This is actually a bit subtle. So as to avoid ever making two `&mut`'s to the
79117same value, we explicitly construct brand-new slices through raw pointers.
118+ -->
119+
120+ これは実際、ちょっと微妙です。 同じ値に対する 2 つの ` &mut ` を生成するのを
121+ 常に避けるため、生ポインタを通じて明確に完全に新しいスライスを構築します。
80122
123+ <!--
81124However more subtle is how iterators that yield mutable references work.
82125The iterator trait is defined as follows:
126+ -->
127+
128+ しかし、もっと微妙なのは、可変参照を生成するイテレータが
129+ どのように動作するかについてです。イテレータのトレイトは以下のように定義されます。
83130
84131``` rust
85132trait Iterator {
@@ -89,25 +136,55 @@ trait Iterator {
89136}
90137```
91138
139+ <!--
92140Given this definition, Self::Item has *no* connection to `self`. This means that
93141we can call `next` several times in a row, and hold onto all the results
94142*concurrently*. This is perfectly fine for by-value iterators, which have
95143exactly these semantics. It's also actually fine for shared references, as they
96144admit arbitrarily many references to the same thing (although the iterator needs
97145to be a separate object from the thing being shared).
146+ -->
147+
148+ 上記の定義によれば、 Self::Item は ` self ` と何のつながりも持ち* ません* 。
149+ これは、 ` next ` を続けて何回か呼ぶことができ、そしてそれらに対する全ての結果を
150+ * 同時に* 保持することができることを意味します。これは、値渡しのイテレータに対しては
151+ 全く問題ありません。値渡しのイテレータも全く同じセマンティクスを持つからです。
152+ そして、共有参照に対しても問題ありません。これらも同じものに対する任意の数の
153+ 参照を認めているからです (イテレータは共有されるオブジェクトと分離されている必要がありますが) 。
98154
155+ <!--
99156But mutable references make this a mess. At first glance, they might seem
100157completely incompatible with this API, as it would produce multiple mutable
101158references to the same object!
159+ -->
102160
161+ しかし、可変参照はこれをごちゃごちゃにします。ひと目見ただけでも、可変参照は
162+ この API に全く対応できないように見えるかもしれません。この API が同じオブジェクトに対する
163+ 複数の可変参照を生成するからです!
164+
165+ <!--
103166However it actually *does* work, exactly because iterators are one-shot objects.
104167Everything an IterMut yields will be yielded at most once, so we don't
105168actually ever yield multiple mutable references to the same piece of data.
169+ -->
170+
171+ しかし、この API は* 本当に* 動作します。まさにイテレータがその場限りのオブジェクトであるからです。
172+ IterMut が生成するすべてのものは高々 1 回しか生成されません。ですから実際には、
173+ 常に、何らかのひとかけらのデータに対する可変参照を、複数回生成していないのです。
106174
175+ <!--
107176Perhaps surprisingly, mutable iterators don't require unsafe code to be
108177implemented for many types!
178+ -->
109179
180+ もしかすると驚くかもしれませんが、可変のイテレータは多くの型に対して
181+ 実装する際、アンセーフなコードを必要としないのです!
182+
183+ <!--
110184For instance here's a singly linked list:
185+ -->
186+
187+ 例えばこれは、片方向リストです。
111188
112189``` rust
113190# fn main () {}
@@ -142,7 +219,11 @@ impl<'a, T> Iterator for IterMut<'a, T> {
142219}
143220```
144221
222+ <!--
145223Here's a mutable slice:
224+ -->
225+
226+ これは可変スライスです。
146227
147228``` rust
148229# fn main () {}
@@ -176,7 +257,11 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
176257}
177258```
178259
260+ <!--
179261And here's a binary tree:
262+ -->
263+
264+ そしてこれは二分木です。
180265
181266``` rust
182267# fn main () {}
@@ -284,8 +369,16 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
284369}
285370```
286371
372+ <!--
287373All of these are completely safe and work on stable Rust! This ultimately
288374falls out of the simple struct case we saw before: Rust understands that you
289375can safely split a mutable reference into subfields. We can then encode
290376permanently consuming a reference via Options (or in the case of slices,
291377replacing with an empty slice).
378+ -->
379+
380+ これらは全て、完全に安全で、安定版の Rust で動作します! これは究極には、
381+ 前に見た単純な構造体のケースから外れています。すなわち、 Rust は、
382+ 可変参照を複数の副フィールドに安全に分割できると理解しているのです。
383+ ですから Option を通じて、参照を消費することで、永続的にエンコードすることができます。
384+ (あるいはスライスの場合、空のスライスで置き換えます)
0 commit comments