]> git.lizzy.rs Git - rust.git/commitdiff
Replace Unique<T> with NonZero<T> in Alloc trait
authorSimon Sapin <simon.sapin@exyr.org>
Fri, 22 Dec 2017 18:12:22 +0000 (19:12 +0100)
committerSimon Sapin <simon.sapin@exyr.org>
Sat, 20 Jan 2018 09:55:16 +0000 (10:55 +0100)
src/liballoc/allocator.rs
src/liballoc/raw_vec.rs
src/libcore/ptr.rs
src/test/run-pass/allocator-alloc-one.rs

index c2a8f5f8ff957e5004b9b2cf95ea0cb4d6dfe448..55e8c0b430f52c76f6b63617d8b6a0b90bef91d3 100644 (file)
@@ -19,7 +19,7 @@
 use core::fmt;
 use core::mem;
 use core::usize;
-use core::ptr::{self, Unique};
+use core::ptr::{self, NonNull};
 
 /// Represents the combination of a starting address and
 /// a total capacity of the returned block.
@@ -895,12 +895,12 @@ unsafe fn shrink_in_place(&mut self,
     /// Clients wishing to abort computation in response to an
     /// allocation error are encouraged to call the allocator's `oom`
     /// method, rather than directly invoking `panic!` or similar.
-    fn alloc_one<T>(&mut self) -> Result<Unique<T>, AllocErr>
+    fn alloc_one<T>(&mut self) -> Result<NonNull<T>, AllocErr>
         where Self: Sized
     {
         let k = Layout::new::<T>();
         if k.size() > 0 {
-            unsafe { self.alloc(k).map(|p| Unique::new_unchecked(p as *mut T)) }
+            unsafe { self.alloc(k).map(|p| NonNull::new_unchecked(p as *mut T)) }
         } else {
             Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
         }
@@ -923,7 +923,7 @@ fn alloc_one<T>(&mut self) -> Result<Unique<T>, AllocErr>
     /// * `ptr` must denote a block of memory currently allocated via this allocator
     ///
     /// * the layout of `T` must *fit* that block of memory.
-    unsafe fn dealloc_one<T>(&mut self, ptr: Unique<T>)
+    unsafe fn dealloc_one<T>(&mut self, ptr: NonNull<T>)
         where Self: Sized
     {
         let raw_ptr = ptr.as_ptr() as *mut u8;
@@ -963,7 +963,7 @@ unsafe fn dealloc_one<T>(&mut self, ptr: Unique<T>)
     /// Clients wishing to abort computation in response to an
     /// allocation error are encouraged to call the allocator's `oom`
     /// method, rather than directly invoking `panic!` or similar.
-    fn alloc_array<T>(&mut self, n: usize) -> Result<Unique<T>, AllocErr>
+    fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, AllocErr>
         where Self: Sized
     {
         match Layout::array::<T>(n) {
@@ -971,7 +971,7 @@ fn alloc_array<T>(&mut self, n: usize) -> Result<Unique<T>, AllocErr>
                 unsafe {
                     self.alloc(layout.clone())
                         .map(|p| {
-                            Unique::new_unchecked(p as *mut T)
+                            NonNull::new_unchecked(p as *mut T)
                         })
                 }
             }
@@ -1012,15 +1012,15 @@ fn alloc_array<T>(&mut self, n: usize) -> Result<Unique<T>, AllocErr>
     /// reallocation error are encouraged to call the allocator's `oom`
     /// method, rather than directly invoking `panic!` or similar.
     unsafe fn realloc_array<T>(&mut self,
-                               ptr: Unique<T>,
+                               ptr: NonNull<T>,
                                n_old: usize,
-                               n_new: usize) -> Result<Unique<T>, AllocErr>
+                               n_new: usize) -> Result<NonNull<T>, AllocErr>
         where Self: Sized
     {
         match (Layout::array::<T>(n_old), Layout::array::<T>(n_new), ptr.as_ptr()) {
             (Some(ref k_old), Some(ref k_new), ptr) if k_old.size() > 0 && k_new.size() > 0 => {
                 self.realloc(ptr as *mut u8, k_old.clone(), k_new.clone())
-                    .map(|p|Unique::new_unchecked(p as *mut T))
+                    .map(|p| NonNull::new_unchecked(p as *mut T))
             }
             _ => {
                 Err(AllocErr::invalid_input("invalid layout for realloc_array"))
@@ -1048,7 +1048,7 @@ unsafe fn realloc_array<T>(&mut self,
     /// constraints.
     ///
     /// Always returns `Err` on arithmetic overflow.
-    unsafe fn dealloc_array<T>(&mut self, ptr: Unique<T>, n: usize) -> Result<(), AllocErr>
+    unsafe fn dealloc_array<T>(&mut self, ptr: NonNull<T>, n: usize) -> Result<(), AllocErr>
         where Self: Sized
     {
         let raw_ptr = ptr.as_ptr() as *mut u8;
index dbf1fb1367dda257ecb88aa7118f0be03f1b0f80..621e190696137c7fe7d792733ab0f7978acaa23f 100644 (file)
@@ -322,7 +322,7 @@ pub fn double(&mut self) {
                     // would cause overflow
                     let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
                     match self.a.alloc_array::<T>(new_cap) {
-                        Ok(ptr) => (new_cap, ptr),
+                        Ok(ptr) => (new_cap, ptr.into()),
                         Err(e) => self.a.oom(e),
                     }
                 }
index fd8f9138f36513a3fad0aeb8a7b808f13c6ff1c9..89ecb3457fcb69baf43ccc94fbbaa1b50196df15 100644 (file)
@@ -2452,6 +2452,13 @@ fn from(reference: &'a T) -> Self {
     }
 }
 
+#[unstable(feature = "unique", issue = "27730")]
+impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
+    fn from(p: NonNull<T>) -> Self {
+        Unique { pointer: p.pointer, _marker: PhantomData }
+    }
+}
+
 /// Previous name of `NonNull`.
 #[rustc_deprecated(since = "1.24", reason = "renamed to `NonNull`")]
 #[unstable(feature = "shared", issue = "27730")]
index 712fa2d6001774b23ddb64226629f413f4682443..eaa5bc90805768e7643c8725eb6bd291b87bd78b 100644 (file)
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#![feature(allocator_api, unique)]
+#![feature(allocator_api, nonnull)]
 
 use std::heap::{Heap, Alloc};