@@ -29,17 +29,17 @@ looks like:
29
29
``` rust
30
30
#![feature(global_allocator, allocator_api, heap_api)]
31
31
32
- use std :: alloc :: {GlobalAlloc , System , Layout , Opaque };
32
+ use std :: alloc :: {AllocErr , GlobalAlloc , System , Layout , Opaque };
33
33
use std :: ptr :: NonNull ;
34
34
35
35
struct MyAllocator ;
36
36
37
37
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 > {
39
39
System . alloc (layout )
40
40
}
41
41
42
- unsafe fn dealloc (& self , ptr : * mut Opaque , layout : Layout ) {
42
+ unsafe fn dealloc (& self , ptr : NonNull < Opaque > , layout : Layout ) {
43
43
System . dealloc (ptr , layout )
44
44
}
45
45
}
@@ -55,18 +55,15 @@ fn main() {
55
55
```
56
56
57
57
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.
62
59
63
60
A crate can only have one instance of ` #[global_allocator] ` and this instance
64
61
may be loaded through a dependency. For example ` #[global_allocator] ` above
65
62
could have been placed in one of the dependencies loaded through ` extern crate ` .
66
63
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