@@ -164,27 +164,27 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
164164#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
165165unsafe impl AllocRef for Global {
166166 #[ inline]
167- fn alloc ( & mut self , layout : Layout ) -> Result < MemoryBlock , AllocErr > {
167+ fn alloc ( & mut self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > {
168168 let size = layout. size ( ) ;
169169 let ptr = if size == 0 {
170170 layout. dangling ( )
171171 } else {
172172 // SAFETY: `layout` is non-zero in size,
173173 unsafe { NonNull :: new ( alloc ( layout) ) . ok_or ( AllocErr ) ? }
174174 } ;
175- Ok ( MemoryBlock { ptr, size } )
175+ Ok ( NonNull :: slice_from_raw_parts ( ptr, size) )
176176 }
177177
178178 #[ inline]
179- fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < MemoryBlock , AllocErr > {
179+ fn alloc_zeroed ( & mut self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > {
180180 let size = layout. size ( ) ;
181181 let ptr = if size == 0 {
182182 layout. dangling ( )
183183 } else {
184184 // SAFETY: `layout` is non-zero in size,
185185 unsafe { NonNull :: new ( alloc_zeroed ( layout) ) . ok_or ( AllocErr ) ? }
186186 } ;
187- Ok ( MemoryBlock { ptr, size } )
187+ Ok ( NonNull :: slice_from_raw_parts ( ptr, size) )
188188 }
189189
190190 #[ inline]
@@ -202,7 +202,7 @@ unsafe impl AllocRef for Global {
202202 ptr : NonNull < u8 > ,
203203 layout : Layout ,
204204 new_size : usize ,
205- ) -> Result < MemoryBlock , AllocErr > {
205+ ) -> Result < NonNull < [ u8 ] > , AllocErr > {
206206 debug_assert ! (
207207 new_size >= layout. size( ) ,
208208 "`new_size` must be greater than or equal to `layout.size()`"
@@ -212,14 +212,16 @@ unsafe impl AllocRef for Global {
212212 // Other conditions must be upheld by the caller
213213 unsafe {
214214 match layout. size ( ) {
215- old_size if old_size == new_size => Ok ( MemoryBlock { ptr, size : new_size } ) ,
215+ old_size if old_size == new_size => {
216+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
217+ }
216218 0 => self . alloc ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
217219 old_size => {
218220 // `realloc` probably checks for `new_size > size` or something similar.
219221 intrinsics:: assume ( new_size > old_size) ;
220222 let raw_ptr = realloc ( ptr. as_ptr ( ) , layout, new_size) ;
221223 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
222- Ok ( MemoryBlock { ptr, size : new_size } )
224+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
223225 }
224226 }
225227 }
@@ -231,7 +233,7 @@ unsafe impl AllocRef for Global {
231233 ptr : NonNull < u8 > ,
232234 layout : Layout ,
233235 new_size : usize ,
234- ) -> Result < MemoryBlock , AllocErr > {
236+ ) -> Result < NonNull < [ u8 ] > , AllocErr > {
235237 debug_assert ! (
236238 new_size >= layout. size( ) ,
237239 "`new_size` must be greater than or equal to `layout.size()`"
@@ -241,15 +243,17 @@ unsafe impl AllocRef for Global {
241243 // Other conditions must be upheld by the caller
242244 unsafe {
243245 match layout. size ( ) {
244- old_size if old_size == new_size => Ok ( MemoryBlock { ptr, size : new_size } ) ,
246+ old_size if old_size == new_size => {
247+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
248+ }
245249 0 => self . alloc_zeroed ( Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ) ,
246250 old_size => {
247251 // `realloc` probably checks for `new_size > size` or something similar.
248252 intrinsics:: assume ( new_size > old_size) ;
249253 let raw_ptr = realloc ( ptr. as_ptr ( ) , layout, new_size) ;
250254 raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
251255 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?;
252- Ok ( MemoryBlock { ptr, size : new_size } )
256+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
253257 }
254258 }
255259 }
@@ -261,7 +265,7 @@ unsafe impl AllocRef for Global {
261265 ptr : NonNull < u8 > ,
262266 layout : Layout ,
263267 new_size : usize ,
264- ) -> Result < MemoryBlock , AllocErr > {
268+ ) -> Result < NonNull < [ u8 ] > , AllocErr > {
265269 let old_size = layout. size ( ) ;
266270 debug_assert ! (
267271 new_size <= old_size,
@@ -288,7 +292,7 @@ unsafe impl AllocRef for Global {
288292 NonNull :: new ( raw_ptr) . ok_or ( AllocErr ) ?
289293 } ;
290294
291- Ok ( MemoryBlock { ptr, size : new_size } )
295+ Ok ( NonNull :: slice_from_raw_parts ( ptr, new_size) )
292296 }
293297}
294298
@@ -300,7 +304,7 @@ unsafe impl AllocRef for Global {
300304unsafe fn exchange_malloc ( size : usize , align : usize ) -> * mut u8 {
301305 let layout = unsafe { Layout :: from_size_align_unchecked ( size, align) } ;
302306 match Global . alloc ( layout) {
303- Ok ( memory ) => memory . ptr . as_ptr ( ) ,
307+ Ok ( ptr ) => ptr. as_non_null_ptr ( ) . as_ptr ( ) ,
304308 Err ( _) => handle_alloc_error ( layout) ,
305309 }
306310}
0 commit comments