Skip to content

Commit b070fa8

Browse files
authored
Remove wrong suggestion part (#983)
1 parent bf4f881 commit b070fa8

File tree

1 file changed

+0
-5
lines changed
  • blog/content/edition-2/posts/11-allocator-designs

1 file changed

+0
-5
lines changed

blog/content/edition-2/posts/11-allocator-designs/index.md

-5
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,6 @@ Note that we don't perform any bounds checks or alignment adjustments, so this i
172172
error[E0594]: cannot assign to `self.next` which is behind a `&` reference
173173
--> src/allocator/bump.rs:29:9
174174
|
175-
26 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
176-
| ----- help: consider changing this to be a mutable reference: `&mut self`
177-
...
178175
29 | self.next = alloc_start + layout.size();
179176
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be written
180177
```
@@ -186,8 +183,6 @@ The error occurs because the [`alloc`] and [`dealloc`] methods of the `GlobalAll
186183
[`alloc`]: https://doc.rust-lang.org/alloc/alloc/trait.GlobalAlloc.html#tymethod.alloc
187184
[`dealloc`]: https://doc.rust-lang.org/alloc/alloc/trait.GlobalAlloc.html#tymethod.dealloc
188185

189-
Note that the compiler suggestion to change `&self` to `&mut self` in the method declaration does not work here. The reason is that the method signature is defined by the `GlobalAlloc` trait and can't be changed on the implementation side. (I opened an [issue](https://github.com/rust-lang/rust/issues/68049) in the Rust repository about the invalid suggestion.)
190-
191186
#### `GlobalAlloc` and Mutability
192187

193188
Before we look at a possible solution to this mutability problem, let's try to understand why the `GlobalAlloc` trait methods are defined with `&self` arguments: As we saw [in the previous post][global-allocator], the global heap allocator is defined by adding the `#[global_allocator]` attribute to a `static` that implements the `GlobalAlloc` trait. Static variables are immutable in Rust, so there is no way to call a method that takes `&mut self` on the static allocator. For this reason, all the methods of `GlobalAlloc` only take an immutable `&self` reference.

0 commit comments

Comments
 (0)