1
+ <!--
1
2
# Higher-Rank Trait Bounds (HRTBs)
3
+ -->
2
4
5
+ # 高階トレイト境界
6
+
7
+ <!--
3
8
Rust's `Fn` traits are a little bit magic. For instance, we can write the
4
9
following code:
10
+ -->
11
+
12
+ Rust の ` Fn ` トレイトはちょっとした魔法です。例えば、次のように書くことができます。
5
13
6
14
``` rust
7
15
struct Closure <F > {
@@ -25,8 +33,12 @@ fn main() {
25
33
}
26
34
```
27
35
36
+ <!--
28
37
If we try to naively desugar this code in the same way that we did in the
29
38
lifetimes section, we run into some trouble:
39
+ -->
40
+
41
+ ライフタイムの節と同じように単純に脱糖しようとすると、問題が起こります。
30
42
31
43
``` rust,ignore
32
44
struct Closure<F> {
@@ -52,22 +64,42 @@ fn main() {
52
64
}
53
65
```
54
66
67
+ <!--
55
68
How on earth are we supposed to express the lifetimes on `F`'s trait bound? We
56
69
need to provide some lifetime there, but the lifetime we care about can't be
57
70
named until we enter the body of `call`! Also, that isn't some fixed lifetime;
58
71
`call` works with *any* lifetime `&self` happens to have at that point.
72
+ -->
73
+
74
+ ` F ` のトレイト境界は、一体どうすれば表現できるのでしょう?
75
+ なんらかのライフタイムを提供する必要がありますが、問題のライフタイムは ` call ` 関数が呼ばれるまで名前が無いのです。さらに、ライフタイムは固定されていません。
76
+ ` &self ` に* どんな* ライフタイムが割り当てられても、` call ` は動作します。
59
77
78
+ <!--
60
79
This job requires The Magic of Higher-Rank Trait Bounds (HRTBs). The way we
61
80
desugar this is as follows:
81
+ -->
82
+
83
+ この問題は、高階トレイト境界(HRTB: Higher-Rank Trait Bounds)という魔法で解決できます。
84
+ HRTB を使うとつぎのように脱糖できます。
62
85
63
86
``` rust,ignore
64
87
where for<'a> F: Fn(&'a (u8, u16)) -> &'a u8,
65
88
```
66
89
90
+ <!--
67
91
(Where `Fn(a, b, c) -> d` is itself just sugar for the unstable *real* `Fn`
68
92
trait)
93
+ -->
94
+
95
+ (` Fn(a, b, c) -> d ` 自体が、まだ仕様が安定していない* 本当の* ` Fn ` トレイトの糖衣構文です。)
69
96
97
+ <!--
70
98
`for<'a>` can be read as "for all choices of `'a`", and basically produces an
71
99
*infinite list* of trait bounds that F must satisfy. Intense. There aren't many
72
100
places outside of the `Fn` traits where we encounter HRTBs, and even for
73
101
those we have a nice magic sugar for the common cases.
102
+ -->
103
+
104
+ ` for<'a> ` は、「` 'a ` に何を選んだとしても」という意味で、つまり F が満たさなくてはならないトレイト境界の* 無限リスト* を生成します。強烈でしょう?
105
+ ` Fn ` トレイトを除けば、HRTB が使われる場所はほとんどありません。` Fn ` トレイトにおいても、ほとんどの場合は魔法の糖衣構文が良いされています。
0 commit comments