Skip to content

Commit

Permalink
The const_mut_refs feature is stable now and no longer required
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-opp committed Jan 15, 2025
1 parent 4de9527 commit 69a498c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ impl ListNode {
}
```

この型は`new`という単純なコンストラクタ関数を持ち、表現する領域の開始・終端アドレスを計算するメソッドを持っています。`new`関数は[const関数][const function]としていますが、これは後で静的な連結リストアロケータを作る際に必要になるためです。const関数においては、あらゆる可変参照の使用(`next`フィールドを`None`にすることも含め)はunstableであることに注意してください。コンパイルを通すためには、`#![feature(const_mut_refs)]``lib.rs`の最初に追加する必要があります。
この型は`new`という単純なコンストラクタ関数を持ち、表現する領域の開始・終端アドレスを計算するメソッドを持っています。`new`関数は[const関数][const function]としていますが、これは後で静的な連結リストアロケータを作る際に必要になるためです。

[const function]: https://doc.rust-lang.org/reference/items/functions.html#const-functions

Expand Down Expand Up @@ -968,8 +968,6 @@ impl FixedSizeBlockAllocator {

[`empty`]: https://docs.rs/linked_list_allocator/0.9.0/linked_list_allocator/struct.Heap.html#method.empty

もし`LinkedListAllocator`を実装するときにまだやっていないのなら、 **`#![feature(const_mut_refs)]`**`lib.rs`の最初に追記しないといけません。const関数内におけるあらゆる可変参照型の使用はまだunstableで、それには`list_heads`フィールドの配列要素の型である`Option<&'static mut ListNode>`も(その値を`None`にしているにもかかわらず)含まれるからです。

このunsafeな`init`関数は`fallback_allocator`[`init`]関数を呼ぶだけで、`list_heads`配列の初期化などは行いません。これらの配列の初期化は、`alloc``dealloc`呼び出しが行われたときに初めて行います。

[`init`]: https://docs.rs/linked_list_allocator/0.9.0/linked_list_allocator/struct.Heap.html#method.init
Expand Down
4 changes: 1 addition & 3 deletions blog/content/edition-2/posts/11-allocator-designs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl ListNode {
}
```

The type has a simple constructor function named `new` and methods to calculate the start and end addresses of the represented region. We make the `new` function a [const function], which will be required later when constructing a static linked list allocator. Note that any use of mutable references in const functions (including setting the `next` field to `None`) is still unstable. In order to get it to compile, we need to add **`#![feature(const_mut_refs)]`** to the beginning of our `lib.rs`.
The type has a simple constructor function named `new` and methods to calculate the start and end addresses of the represented region. We make the `new` function a [const function], which will be required later when constructing a static linked list allocator.

[const function]: https://doc.rust-lang.org/reference/items/functions.html#const-functions

Expand Down Expand Up @@ -967,8 +967,6 @@ The `new` function just initializes the `list_heads` array with empty nodes and

[`empty`]: https://docs.rs/linked_list_allocator/0.9.0/linked_list_allocator/struct.Heap.html#method.empty

If you haven't done so already for the `LinkedListAllocator` implementation, you also need to add **`#![feature(const_mut_refs)]`** to the top of your `lib.rs`. The reason is that any use of mutable reference types in const functions is still unstable, including the `Option<&'static mut ListNode>` array element type of the `list_heads` field (even if we set it to `None`).

The unsafe `init` function only calls the [`init`] function of the `fallback_allocator` without doing any additional initialization of the `list_heads` array. Instead, we will initialize the lists lazily on `alloc` and `dealloc` calls.

[`init`]: https://docs.rs/linked_list_allocator/0.9.0/linked_list_allocator/struct.Heap.html#method.init
Expand Down

0 comments on commit 69a498c

Please sign in to comment.