-
Notifications
You must be signed in to change notification settings - Fork 4
add boxed module #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
add boxed module #29
Conversation
|
現在少し忙しく、レビューが遅れるかもしれません。申し訳ありません・・・ |
hinshiba
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
貢献ありがとうございます.
いくつかの用語について提案がありますので
確認していただけると幸いです.
|
|
||
| ## 概要 | ||
|
|
||
| `Box<T>`はヒープアロケーションに分類される。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Box<T>がヒープアロケーションなのではなく,
Box<T>はヒープアロケーション(ヒープへのメモリアロケーション)を行うための型です.
そのあたりの動詞の関係が不明瞭に思えます.
| let val: u8 = *boxed; | ||
| ``` | ||
|
|
||
| 再帰構造の作成: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 再帰構造の作成: | |
| 再帰データ構造の作成: |
軽微なことですがどうでしょうか
| ``` | ||
|
|
||
| 上記のプログラムの結果は`Cons(1, Cons(2, Nil))`となる。 | ||
| 再帰構造にはBox化が必要である。例えば`Cons`を下記のように定義した場合コンパイルエラーとなる: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 再帰構造にはBox化が必要である。例えば`Cons`を下記のように定義した場合コンパイルエラーとなる: | |
| 再帰データ構造にはBox化が必要である。例えば`Cons`を下記のように定義した場合コンパイルエラーとなる: |
上を修正する場合は統一のために
| Cons(T, List<T>), | ||
| ``` | ||
|
|
||
| なぜなら`List`のスタックサイズはリスト内にどれだけのリストがあるかにより、このままでは`Cons`に対するスタックの割り当て量を把握できないためである。スタックサイズが決まっている[`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html)の導入により、`Cons`に必要なスタックサイズが把握できるようになる。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| なぜなら`List`のスタックサイズはリスト内にどれだけのリストがあるかにより、このままでは`Cons`に対するスタックの割り当て量を把握できないためである。スタックサイズが決まっている[`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html)の導入により、`Cons`に必要なスタックサイズが把握できるようになる。 | |
| なぜなら`List`のサイズはリスト内にどれだけのリストがあるかにより、このままでは`Cons`に対するスタックの割り当て量を把握できないためである。サイズが決まっている[`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html)の導入により、`Cons`に必要なスタックサイズが把握できるようになる。 |
Listのスタックサイズという表現はやや不親切です.
Listのスタック上でのサイズというべきですし,ヒープ上でも管理領域を除けば同じ大きさなのですから,単にサイズというほうが良いかと思います.
| Cons(T, List<T>), | ||
| ``` | ||
|
|
||
| なぜなら`List`のスタックサイズはリスト内にどれだけのリストがあるかにより、このままでは`Cons`に対するスタックの割り当て量を把握できないためである。スタックサイズが決まっている[`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html)の導入により、`Cons`に必要なスタックサイズが把握できるようになる。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| なぜなら`List`のスタックサイズはリスト内にどれだけのリストがあるかにより、このままでは`Cons`に対するスタックの割り当て量を把握できないためである。スタックサイズが決まっている[`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html)の導入により、`Cons`に必要なスタックサイズが把握できるようになる。 | |
| なぜなら`List`のスタックサイズはリスト内にどれだけのリストがあるかにより、このままでは`Cons`に対するスタックの割り当て量を把握できないためである。スタックサイズが決まっている[`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html)を用いることで、`Cons`に必要なスタックサイズが把握できるようになる。 |
introduceは導入ですが,型を変えることを日本語的には導入と表現することに違和感があります.
|
|
||
| ## メモリーレイアウト | ||
|
|
||
| サイズが0でない値では、[`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html)はメモリー割り当てのため[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターを使用する。アロケーターとともに使用される[`Layout`](https://doc.rust-lang.org/std/alloc/struct.Layout.html)が適切な型であり、生ポインターがその型の有効な値を示す場合に、[`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html)と[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターに割り当てられた生ポインターは相互に変換できる。より詳しく言うと、[`Layout::for_value(&*value)`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value)とともに使用される[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターに割り当てられた`value: *mut T`は[`Box::<T>::from_raw(value)`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.from_raw)を使用してBoxへと変換できる。逆に[`Box::<T>::into_raw`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.into_raw)から得られた`value: *mut T`は[`Layout::for_value(&*value)`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value)と[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターによって解放できる。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| サイズが0でない値では、[`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html)はメモリー割り当てのため[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターを使用する。アロケーターとともに使用される[`Layout`](https://doc.rust-lang.org/std/alloc/struct.Layout.html)が適切な型であり、生ポインターがその型の有効な値を示す場合に、[`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html)と[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターに割り当てられた生ポインターは相互に変換できる。より詳しく言うと、[`Layout::for_value(&*value)`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value)とともに使用される[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターに割り当てられた`value: *mut T`は[`Box::<T>::from_raw(value)`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.from_raw)を使用してBoxへと変換できる。逆に[`Box::<T>::into_raw`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.into_raw)から得られた`value: *mut T`は[`Layout::for_value(&*value)`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value)と[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターによって解放できる。 | |
| サイズが0でない値では、[`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html)はメモリー割り当てのため[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターを使用する。アロケーターの引数として使用される[`Layout`](https://doc.rust-lang.org/std/alloc/struct.Layout.html)が適切な型であり、生ポインターがその型の有効な値を示す場合に、[`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html)と[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターに割り当てられた生ポインターは相互に変換できる。より詳しく言うと、[`Layout::for_value(&*value)`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value)とともに使用される[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターに割り当てられた`value: *mut T`は[`Box::<T>::from_raw(value)`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.from_raw)を使用してBoxへと変換できる。逆に[`Box::<T>::into_raw`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.into_raw)から得られた`value: *mut T`は[`Layout::for_value(&*value)`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value)と[`Global`](https://doc.rust-lang.org/std/alloc/struct.Global.html)アロケーターによって解放できる。 |
ともに用いられるわけではなく,メモリアロケーションのための引数として使用されます.
https://doc.rust-lang.org/std/alloc/struct.Global.html
|
|
||
| これらの基本的なLayoutの要件に加えて、`Box<T>`は有効な値`T`を示さなければならない。 | ||
|
|
||
| `T: Sized`である限り`Box<T>`は単一のポインターであることを保証するとともにCのポインター(CのT*型)とのABI互換性を持つ。これはRustでCから呼び出す関数を作成する際に`Box<T>`で定義したものがCでは`T*`になるという意味である。例として`Foo`の作成と破棄を行う関数を宣言するCのヘッダーをあげる: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| `T: Sized`である限り`Box<T>`は単一のポインターであることを保証するとともにCのポインター(CのT*型)とのABI互換性を持つ。これはRustでCから呼び出す関数を作成する際に`Box<T>`で定義したものがCでは`T*`になるという意味である。例として`Foo`の作成と破棄を行う関数を宣言するCのヘッダーをあげる: | |
| `T: Sized`である限り`Box<T>`は単一のポインターであることを保証するとともにCのポインター(CのT*型)とのABI互換性を持つ。これはRustでCから呼び出す関数を作成する際に`Box<T>`で定義したものがCでは`T*`になるという意味である。例として`Foo`の作成と破棄を行う関数を宣言するCのヘッダーを示す: |
些細なことですが,語感的にはこちらはどうでしょうか?
| ```C | ||
| /* C header */ | ||
|
|
||
| /* Returns ownership to the caller */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
できればコメントも翻訳していただけると助かります.
意図があるなら大丈夫です.
| void foo_delete(struct Foo*); | ||
| ``` | ||
|
|
||
| この2つの関数はRustで下記のように実装できる。ここでは`struct Foo*`型に変換された`Box<Foo>`は所有権の制約を補足する。`Box<Foo>`が非ヌルであるため引数がヌルになる可能性のある`foo_delete`の引数はRustでは`Option<Box<Foo>>`になることに注意しなければならない。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| この2つの関数はRustで下記のように実装できる。ここでは`struct Foo*`型に変換された`Box<Foo>`は所有権の制約を補足する。`Box<Foo>`が非ヌルであるため引数がヌルになる可能性のある`foo_delete`の引数はRustでは`Option<Box<Foo>>`になることに注意しなければならない。 | |
| この2つの関数はRustで下記のように実装できる。ここでは`struct Foo*`型に変換された`Box<Foo>`は所有権の制約を補足する。`Box<Foo>`が非ヌルであるため、引数がヌルになる可能性のある`foo_delete`の引数はRustでは`Option<Box<Foo>>`になることに注意しなければならない。 |
日本語だと長くなるので読点があるとありがたいです.
|
|
||
| | 名前 | 説明 | | ||
| | --------------------------------------------------------------------------------- | ---------------------------------------- | | ||
| | [Box](https://doc.rust-lang.org/std/boxed/struct.Box.html) | T 型のヒープを一意に所有するポインター型 | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| | [Box](https://doc.rust-lang.org/std/boxed/struct.Box.html) | T 型のヒープを一意に所有するポインター型 | | |
| | [Box](https://doc.rust-lang.org/std/boxed/struct.Box.html) | ヒープに配置された T 型を一意に所有するポインター型 | |
ヒープはデータ構造であり,所有することはできません.
おそらくこう書くのがいいのではないでしょうか?
|
その他登録やビルド可能性等は確認済みです. |
概要
boxedモジュールの追加
チェックリスト
mdbook buildを実行して確認したレビューで見てほしい点
メモリー関連に明るくないのでおかしな言い回し等あればご指摘お願いします。