|
1 |
| -% Unsized Types |
| 1 | +% サイズ不定型 |
| 2 | +<!-- % Unsized Types --> |
2 | 3 |
|
3 |
| -Most types have a particular size, in bytes, that is knowable at compile time. |
4 |
| -For example, an `i32` is thirty-two bits big, or four bytes. However, there are |
5 |
| -some types which are useful to express, but do not have a defined size. These are |
6 |
| -called ‘unsized’ or ‘dynamically sized’ types. One example is `[T]`. This type |
7 |
| -represents a certain number of `T` in sequence. But we don’t know how many |
8 |
| -there are, so the size is not known. |
| 4 | +<!-- Most types have a particular size, in bytes, that is knowable at compile time. --> |
| 5 | +<!-- For example, an `i32` is thirty-two bits big, or four bytes. However, there are --> |
| 6 | +<!-- some types which are useful to express, but do not have a defined size. These are --> |
| 7 | +<!-- called ‘unsized’ or ‘dynamically sized’ types. One example is `[T]`. This type --> |
| 8 | +<!-- represents a certain number of `T` in sequence. But we don’t know how many --> |
| 9 | +<!-- there are, so the size is not known. --> |
| 10 | +ほとんどの型はコンパイル時に知れる、バイト数で測った、サイズがあります。 |
| 11 | +例えば、 `i32` 型は、32ビット(4バイト)というサイズです。 |
| 12 | +しかしながら、表現のためには便利であってもサイズが定まっていない型が存在します。 |
| 13 | +そのような方を 「サイズ不定」又は「動的サイズ」型と呼びます。 |
| 14 | +一例を上げると `[T]` 型は 一定のサイズの`T` のシーケンスを意味していますが、その要素数については規定されていないため、サイズは不定となります。 |
9 | 15 |
|
10 |
| -Rust understands a few of these types, but they have some restrictions. There |
11 |
| -are three: |
| 16 | +<!-- Rust understands a few of these types, but they have some restrictions. There --> |
| 17 | +<!-- are three: --> |
| 18 | +Rustはいくつかのそのような型を扱うことができますが、それらには以下の様な3つの制約が存在します: |
12 | 19 |
|
13 |
| -1. We can only manipulate an instance of an unsized type via a pointer. An |
14 |
| - `&[T]` works just fine, but a `[T]` does not. |
15 |
| -2. Variables and arguments cannot have dynamically sized types. |
16 |
| -3. Only the last field in a `struct` may have a dynamically sized type; the |
17 |
| - other fields must not. Enum variants must not have dynamically sized types as |
18 |
| - data. |
| 20 | +<!-- 1. We can only manipulate an instance of an unsized type via a pointer. An -> |
| 21 | +<!-- `&[T]` works just fine, but a `[T]` does not. --> |
| 22 | +<!-- 2. Variables and arguments cannot have dynamically sized types. --> |
| 23 | +<!-- 3. Only the last field in a `struct` may have a dynamically sized type; the --> |
| 24 | +<!-- other fields must not. Enum variants must not have dynamically sized types as --> |
| 25 | +<!-- data. --> |
| 26 | +1. サイズ不定型はポインタを通してのみ操作することができます、たとえば、 `&[T]` は大丈夫ですが、 `[T]` はそうではありません。 |
| 27 | +2. 変数や引数は動的なサイズを持つことはできません。 |
| 28 | +3. `struct` の最後のフィールドのみ、動的なサイズを持つことが許されます、その他のフィールドはサイズが不定であってはなりません。 |
| 29 | + また、Enumのバリアントはデータとして動的なサイズの型を持つ事はできません。 |
19 | 30 |
|
20 |
| -So why bother? Well, because `[T]` can only be used behind a pointer, if we |
21 |
| -didn’t have language support for unsized types, it would be impossible to write |
22 |
| -this: |
| 31 | +<!-- So why bother? Well, because `[T]` can only be used behind a pointer, if we --> |
| 32 | +<!-- didn’t have language support for unsized types, it would be impossible to write --> |
| 33 | +<!-- this: --> |
| 34 | +なぜこんなにややこしいのでしょうか? これは、`[T]` はポインタを通してのみ操作可能であるため、 |
| 35 | +もし言語がサイズ不定型をサポートしていなかった場合、以下のようなコードを書くことは不可能となります: |
23 | 36 |
|
24 | 37 | ```rust,ignore
|
25 | 38 | impl Foo for str {
|
26 | 39 | ```
|
27 | 40 |
|
28 |
| -or |
| 41 | +<!-- or --> |
| 42 | +また、以下の様なコードも: |
29 | 43 |
|
30 | 44 | ```rust,ignore
|
31 | 45 | impl<T> Foo for [T] {
|
32 | 46 | ```
|
33 | 47 |
|
34 |
| -Instead, you would have to write: |
| 48 | +<!-- Instead, you would have to write: --> |
| 49 | +このように書く代わりに、以下のように書く必要があることになるでしょう: |
35 | 50 |
|
36 | 51 | ```rust,ignore
|
37 | 52 | impl Foo for &str {
|
38 | 53 | ```
|
39 | 54 |
|
40 |
| -Meaning, this implementation would only work for [references][ref], and not |
41 |
| -other types of pointers. With the `impl for str`, all pointers, including (at |
42 |
| -some point, there are some bugs to fix first) user-defined custom smart |
43 |
| -pointers, can use this `impl`. |
| 55 | +<!-- Meaning, this implementation would only work for [references][ref], and not --> |
| 56 | +<!-- other types of pointers. With the `impl for str`, all pointers, including (at --> |
| 57 | +<!-- some point, there are some bugs to fix first) user-defined custom smart --> |
| 58 | +<!-- pointers, can use this `impl`. --> |
| 59 | +このように書いたとすると、このコードは [参照][ref] に対してのみ動作する用になり、他のポインタ型に対しては動作しないことになります。 |
| 60 | +`imp for str` のように書くことで、すべてのポインタ、ユーザーの定義した独自のスマートポインタ(いくつかの点についてバグがあるので、それを先ずは直さなくてはなりませんが)もこの `impl` を利用可能になります。 |
44 | 61 |
|
45 | 62 | [ref]: references-and-borrowing.html
|
46 | 63 |
|
47 | 64 | # ?Sized
|
48 | 65 |
|
49 |
| -If you want to write a function that accepts a dynamically sized type, you |
50 |
| -can use the special bound, `?Sized`: |
| 66 | +<!-- If you want to write a function that accepts a dynamically sized type, you --> |
| 67 | +<!-- can use the special bound, `?Sized`: --> |
| 68 | +もし動的サイズ型を引数に取れるような関数を定義したい場合、特別な境界 `?Sized` を利用できます: |
51 | 69 |
|
52 | 70 | ```rust
|
53 | 71 | struct Foo<T: ?Sized> {
|
54 | 72 | f: T,
|
55 | 73 | }
|
56 | 74 | ```
|
57 | 75 |
|
58 |
| -This `?`, read as “T may be `Sized`”, means that this bound is special: it |
59 |
| -lets us match more kinds, not less. It’s almost like every `T` implicitly has |
60 |
| -`T: Sized`, and the `?` undoes this default. |
| 76 | +<!-- This `?`, read as “T may be `Sized`”, means that this bound is special: it --> |
| 77 | +<!-- lets us match more kinds, not less. It’s almost like every `T` implicitly has --> |
| 78 | +<!-- `T: Sized`, and the `?` undoes this default. --> |
| 79 | +`?` は 「Tは `Sized` かもしれない」と読みます、これは `?` が特別な境界: より小さいカインドとマッチするのではなく、より大きいカインドとマッチする ということを意味しています。 |
| 80 | +これは、すべての `T` は暗黙的に `T : Sized` という制限がかけられていて、 `?` はその制限を解除するというようなものです。 |
0 commit comments