@@ -26,24 +26,19 @@ 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(unique, allocator_api )]
30
30
31
- extern crate alloc;
32
-
33
- use std :: ptr :: {drop_in_place, Unique };
31
+ use std :: heap :: {Heap , Alloc , Layout };
34
32
use std :: mem;
35
-
36
- use alloc :: heap;
33
+ use std :: ptr :: {drop_in_place, Unique };
37
34
38
35
struct Box <T >{ ptr : Unique <T > }
39
36
40
37
impl <T > Drop for Box <T > {
41
38
fn drop (& mut self ) {
42
39
unsafe {
43
40
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 >());
41
+ Heap . dealloc (self . ptr. as_ptr () as * mut u8 , Layout :: new :: <T >())
47
42
}
48
43
}
49
44
}
@@ -57,24 +52,19 @@ use-after-free the `ptr` because when drop exits, it becomes inaccessible.
57
52
However this wouldn't work:
58
53
59
54
``` rust
60
- #![feature(alloc, heap_api, unique)]
61
-
62
- extern crate alloc;
55
+ #![feature(allocator_api, unique)]
63
56
57
+ use std :: heap :: {Heap , Alloc , Layout };
64
58
use std :: ptr :: {drop_in_place, Unique };
65
59
use std :: mem;
66
60
67
- use alloc :: heap;
68
-
69
61
struct Box <T >{ ptr : Unique <T > }
70
62
71
63
impl <T > Drop for Box <T > {
72
64
fn drop (& mut self ) {
73
65
unsafe {
74
66
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 >());
67
+ Heap . dealloc (self . ptr. as_ptr () as * mut u8 , Layout :: new :: <T >());
78
68
}
79
69
}
80
70
}
@@ -86,9 +76,7 @@ impl<T> Drop for SuperBox<T> {
86
76
unsafe {
87
77
// Hyper-optimized: deallocate the box's contents for it
88
78
// 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 >());
79
+ Heap . dealloc (self . my_box. ptr. as_ptr () as * mut u8 , Layout :: new :: <T >());
92
80
}
93
81
}
94
82
}
@@ -135,24 +123,19 @@ The classic safe solution to overriding recursive drop and allowing moving out
135
123
of Self during ` drop ` is to use an Option:
136
124
137
125
``` rust
138
- #![feature(alloc, heap_api, unique)]
139
-
140
- extern crate alloc;
126
+ #![feature(allocator_api, unique)]
141
127
128
+ use std :: heap :: {Alloc , Heap , Layout };
142
129
use std :: ptr :: {drop_in_place, Unique };
143
130
use std :: mem;
144
131
145
- use alloc :: heap;
146
-
147
132
struct Box <T >{ ptr : Unique <T > }
148
133
149
134
impl <T > Drop for Box <T > {
150
135
fn drop (& mut self ) {
151
136
unsafe {
152
137
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 >());
138
+ Heap . dealloc (self . ptr. as_ptr () as * mut u8 , Layout :: new :: <T >());
156
139
}
157
140
}
158
141
}
@@ -166,9 +149,7 @@ impl<T> Drop for SuperBox<T> {
166
149
// without `drop`ing the contents. Need to set the `box`
167
150
// field as `None` to prevent Rust from trying to Drop it.
168
151
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 >());
152
+ Heap . dealloc (my_box . ptr. as_ptr () as * mut u8 , Layout :: new :: <T >());
172
153
mem :: forget (my_box );
173
154
}
174
155
}
0 commit comments