Skip to content

Commit a0fffee

Browse files
committed
Fix documentation
1 parent ea89051 commit a0fffee

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/doc/unstable-book/src/language-features/global-allocator.md

+10-13
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ looks like:
2929
```rust
3030
#![feature(global_allocator, allocator_api, heap_api)]
3131

32-
use std::alloc::{GlobalAlloc, System, Layout, Opaque};
32+
use std::alloc::{AllocErr, GlobalAlloc, System, Layout, Opaque};
3333
use std::ptr::NonNull;
3434

3535
struct MyAllocator;
3636

3737
unsafe impl GlobalAlloc for MyAllocator {
38-
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
38+
unsafe fn alloc(&self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
3939
System.alloc(layout)
4040
}
4141

42-
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
42+
unsafe fn dealloc(&self, ptr: NonNull<Opaque>, layout: Layout) {
4343
System.dealloc(ptr, layout)
4444
}
4545
}
@@ -55,18 +55,15 @@ fn main() {
5555
```
5656

5757
And that's it! The `#[global_allocator]` attribute is applied to a `static`
58-
which implements the `Alloc` trait in the `std::alloc` module. Note, though,
59-
that the implementation is defined for `&MyAllocator`, not just `MyAllocator`.
60-
You may wish, however, to also provide `Alloc for MyAllocator` for other use
61-
cases.
58+
which implements the `GlobalAlloc` trait in the `std::alloc` module.
6259

6360
A crate can only have one instance of `#[global_allocator]` and this instance
6461
may be loaded through a dependency. For example `#[global_allocator]` above
6562
could have been placed in one of the dependencies loaded through `extern crate`.
6663

67-
Note that `Alloc` itself is an `unsafe` trait, with much documentation on the
68-
trait itself about usage and for implementors. Extra care should be taken when
69-
implementing a global allocator as well as the allocator may be called from many
70-
portions of the standard library, such as the panicking routine. As a result it
71-
is highly recommended to not panic during allocation and work in as many
72-
situations with as few dependencies as possible as well.
64+
Note that `GlobalAlloc` itself is an `unsafe` trait, with much documentation on
65+
the trait itself about usage and for implementors. Extra care should be taken
66+
when implementing a global allocator as well as the allocator may be called from
67+
many portions of the standard library, such as the panicking routine. As a
68+
result it is highly recommended to not panic during allocation and work in as
69+
many situations with as few dependencies as possible as well.

0 commit comments

Comments
 (0)