@@ -28,17 +28,18 @@ For instance, a custom implementation of `Box` might write `Drop` like this:
28
28
``` rust
29
29
#![feature(ptr_internals, allocator_api, unique)]
30
30
31
- use std :: alloc :: {Global , GlobalAlloc , Layout };
31
+ use std :: alloc :: {Alloc , Global , GlobalAlloc , Layout };
32
32
use std :: mem;
33
- use std :: ptr :: {drop_in_place, Unique };
33
+ use std :: ptr :: {drop_in_place, NonNull , Unique };
34
34
35
35
struct Box <T >{ ptr : Unique <T > }
36
36
37
37
impl <T > Drop for Box <T > {
38
38
fn drop (& mut self ) {
39
39
unsafe {
40
40
drop_in_place (self . ptr. as_ptr ());
41
- Global . dealloc (self . ptr. as_ptr () as * mut _ , Layout :: new :: <T >())
41
+ let c : NonNull <T > = self . ptr. into ();
42
+ Global . dealloc (c . cast (), Layout :: new :: <T >())
42
43
}
43
44
}
44
45
}
@@ -54,8 +55,8 @@ However this wouldn't work:
54
55
``` rust
55
56
#![feature(allocator_api, ptr_internals, unique)]
56
57
57
- use std :: alloc :: {Global , GlobalAlloc , Layout };
58
- use std :: ptr :: {drop_in_place, Unique };
58
+ use std :: alloc :: {Alloc , Global , GlobalAlloc , Layout };
59
+ use std :: ptr :: {drop_in_place, Unique , NonNull };
59
60
use std :: mem;
60
61
61
62
struct Box <T >{ ptr : Unique <T > }
@@ -64,7 +65,8 @@ impl<T> Drop for Box<T> {
64
65
fn drop (& mut self ) {
65
66
unsafe {
66
67
drop_in_place (self . ptr. as_ptr ());
67
- Global . dealloc (self . ptr. as_ptr () as * mut _ , Layout :: new :: <T >());
68
+ let c : NonNull <T > = self . ptr. into ();
69
+ Global . dealloc (c . cast (), Layout :: new :: <T >());
68
70
}
69
71
}
70
72
}
@@ -76,7 +78,8 @@ impl<T> Drop for SuperBox<T> {
76
78
unsafe {
77
79
// Hyper-optimized: deallocate the box's contents for it
78
80
// without `drop`ing the contents
79
- Global . dealloc (self . my_box. ptr. as_ptr () as * mut _ , Layout :: new :: <T >());
81
+ let c : NonNull <T > = self . my_box. ptr. into ();
82
+ Global . dealloc (c . cast :: <u8 >(), Layout :: new :: <T >());
80
83
}
81
84
}
82
85
}
@@ -125,8 +128,8 @@ of Self during `drop` is to use an Option:
125
128
``` rust
126
129
#![feature(allocator_api, ptr_internals, unique)]
127
130
128
- use std :: alloc :: {GlobalAlloc , Global , Layout };
129
- use std :: ptr :: {drop_in_place, Unique };
131
+ use std :: alloc :: {Alloc , GlobalAlloc , Global , Layout };
132
+ use std :: ptr :: {drop_in_place, Unique , NonNull };
130
133
use std :: mem;
131
134
132
135
struct Box <T >{ ptr : Unique <T > }
@@ -135,7 +138,8 @@ impl<T> Drop for Box<T> {
135
138
fn drop (& mut self ) {
136
139
unsafe {
137
140
drop_in_place (self . ptr. as_ptr ());
138
- Global . dealloc (self . ptr. as_ptr () as * mut _ , Layout :: new :: <T >());
141
+ let c : NonNull <T > = self . ptr. into ();
142
+ Global . dealloc (c . cast (), Layout :: new :: <T >());
139
143
}
140
144
}
141
145
}
@@ -149,7 +153,8 @@ impl<T> Drop for SuperBox<T> {
149
153
// without `drop`ing the contents. Need to set the `box`
150
154
// field as `None` to prevent Rust from trying to Drop it.
151
155
let my_box = self . my_box. take (). unwrap ();
152
- Global . dealloc (my_box . ptr. as_ptr () as * mut _ , Layout :: new :: <T >());
156
+ let c : NonNull <T > = my_box . ptr. into ();
157
+ Global . dealloc (c . cast (), Layout :: new :: <T >());
153
158
mem :: forget (my_box );
154
159
}
155
160
}
0 commit comments