@@ -26,24 +26,23 @@ this is totally fine.
26
26
For instance, a custom implementation of ` Box ` might write ` Drop ` like this:
27
27
28
28
``` rust
29
- #![feature(alloc, heap_api, unique)]
29
+ #![feature(alloc, heap_api, unique, allocator_api )]
30
30
31
31
extern crate alloc;
32
32
33
33
use std :: ptr :: {drop_in_place, Unique };
34
34
use std :: mem;
35
35
36
- use alloc :: heap;
36
+ use alloc :: allocator :: {Layout , Alloc };
37
+ use alloc :: heap :: Heap ;
37
38
38
39
struct Box <T >{ ptr : Unique <T > }
39
40
40
41
impl <T > Drop for Box <T > {
41
42
fn drop (& mut self ) {
42
43
unsafe {
43
44
drop_in_place (self . ptr. as_ptr ());
44
- heap :: deallocate (self . ptr. as_ptr () as * mut u8 ,
45
- mem :: size_of :: <T >(),
46
- mem :: align_of :: <T >());
45
+ Heap . dealloc (self . ptr. as_ptr () as * mut u8 , Layout :: new :: <T >())
47
46
}
48
47
}
49
48
}
@@ -57,24 +56,23 @@ use-after-free the `ptr` because when drop exits, it becomes inaccessible.
57
56
However this wouldn't work:
58
57
59
58
``` rust
60
- #![feature(alloc, heap_api, unique)]
59
+ #![feature(alloc, allocator_api, heap_api, unique)]
61
60
62
61
extern crate alloc;
63
62
64
63
use std :: ptr :: {drop_in_place, Unique };
65
64
use std :: mem;
66
65
67
- use alloc :: heap;
66
+ use alloc :: allocator :: {Layout , Alloc };
67
+ use alloc :: heap :: Heap ;
68
68
69
69
struct Box <T >{ ptr : Unique <T > }
70
70
71
71
impl <T > Drop for Box <T > {
72
72
fn drop (& mut self ) {
73
73
unsafe {
74
74
drop_in_place (self . ptr. as_ptr ());
75
- heap :: deallocate (self . ptr. as_ptr () as * mut u8 ,
76
- mem :: size_of :: <T >(),
77
- mem :: align_of :: <T >());
75
+ Heap . dealloc (self . ptr. as_ptr () as * mut u8 , Layout :: new :: <T >());
78
76
}
79
77
}
80
78
}
@@ -86,9 +84,7 @@ impl<T> Drop for SuperBox<T> {
86
84
unsafe {
87
85
// Hyper-optimized: deallocate the box's contents for it
88
86
// without `drop`ing the contents
89
- heap :: deallocate (self . my_box. ptr. as_ptr () as * mut u8 ,
90
- mem :: size_of :: <T >(),
91
- mem :: align_of :: <T >());
87
+ Heap . dealloc (self . my_box. ptr. as_ptr () as * mut u8 , Layout :: new :: <T >());
92
88
}
93
89
}
94
90
}
@@ -135,24 +131,23 @@ The classic safe solution to overriding recursive drop and allowing moving out
135
131
of Self during ` drop ` is to use an Option:
136
132
137
133
``` rust
138
- #![feature(alloc, heap_api, unique)]
134
+ #![feature(alloc, allocator_api, heap_api, unique)]
139
135
140
136
extern crate alloc;
141
137
142
138
use std :: ptr :: {drop_in_place, Unique };
143
139
use std :: mem;
144
140
145
- use alloc :: heap;
141
+ use alloc :: allocator :: {Layout , Alloc };
142
+ use alloc :: heap :: Heap ;
146
143
147
144
struct Box <T >{ ptr : Unique <T > }
148
145
149
146
impl <T > Drop for Box <T > {
150
147
fn drop (& mut self ) {
151
148
unsafe {
152
149
drop_in_place (self . ptr. as_ptr ());
153
- heap :: deallocate (self . ptr. as_ptr () as * mut u8 ,
154
- mem :: size_of :: <T >(),
155
- mem :: align_of :: <T >());
150
+ Heap . dealloc (self . ptr. as_ptr () as * mut u8 , Layout :: new :: <T >());
156
151
}
157
152
}
158
153
}
@@ -166,9 +161,7 @@ impl<T> Drop for SuperBox<T> {
166
161
// without `drop`ing the contents. Need to set the `box`
167
162
// field as `None` to prevent Rust from trying to Drop it.
168
163
let my_box = self . my_box. take (). unwrap ();
169
- heap :: deallocate (my_box . ptr. as_ptr () as * mut u8 ,
170
- mem :: size_of :: <T >(),
171
- mem :: align_of :: <T >());
164
+ Heap . dealloc (my_box . ptr. as_ptr () as * mut u8 , Layout :: new :: <T >());
172
165
mem :: forget (my_box );
173
166
}
174
167
}
0 commit comments