Skip to content

Commit fb03a49

Browse files
committed
Replace Unique<T> with NonZero<T> in Alloc trait
1 parent f19baf0 commit fb03a49

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

src/liballoc/allocator.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::cmp;
1919
use core::fmt;
2020
use core::mem;
2121
use core::usize;
22-
use core::ptr::{self, Unique};
22+
use core::ptr::{self, NonNull};
2323

2424
/// Represents the combination of a starting address and
2525
/// a total capacity of the returned block.
@@ -895,12 +895,12 @@ pub unsafe trait Alloc {
895895
/// Clients wishing to abort computation in response to an
896896
/// allocation error are encouraged to call the allocator's `oom`
897897
/// method, rather than directly invoking `panic!` or similar.
898-
fn alloc_one<T>(&mut self) -> Result<Unique<T>, AllocErr>
898+
fn alloc_one<T>(&mut self) -> Result<NonNull<T>, AllocErr>
899899
where Self: Sized
900900
{
901901
let k = Layout::new::<T>();
902902
if k.size() > 0 {
903-
unsafe { self.alloc(k).map(|p| Unique::new_unchecked(p as *mut T)) }
903+
unsafe { self.alloc(k).map(|p| NonNull::new_unchecked(p as *mut T)) }
904904
} else {
905905
Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
906906
}
@@ -923,7 +923,7 @@ pub unsafe trait Alloc {
923923
/// * `ptr` must denote a block of memory currently allocated via this allocator
924924
///
925925
/// * the layout of `T` must *fit* that block of memory.
926-
unsafe fn dealloc_one<T>(&mut self, ptr: Unique<T>)
926+
unsafe fn dealloc_one<T>(&mut self, ptr: NonNull<T>)
927927
where Self: Sized
928928
{
929929
let raw_ptr = ptr.as_ptr() as *mut u8;
@@ -963,15 +963,15 @@ pub unsafe trait Alloc {
963963
/// Clients wishing to abort computation in response to an
964964
/// allocation error are encouraged to call the allocator's `oom`
965965
/// method, rather than directly invoking `panic!` or similar.
966-
fn alloc_array<T>(&mut self, n: usize) -> Result<Unique<T>, AllocErr>
966+
fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, AllocErr>
967967
where Self: Sized
968968
{
969969
match Layout::array::<T>(n) {
970970
Some(ref layout) if layout.size() > 0 => {
971971
unsafe {
972972
self.alloc(layout.clone())
973973
.map(|p| {
974-
Unique::new_unchecked(p as *mut T)
974+
NonNull::new_unchecked(p as *mut T)
975975
})
976976
}
977977
}
@@ -1012,15 +1012,15 @@ pub unsafe trait Alloc {
10121012
/// reallocation error are encouraged to call the allocator's `oom`
10131013
/// method, rather than directly invoking `panic!` or similar.
10141014
unsafe fn realloc_array<T>(&mut self,
1015-
ptr: Unique<T>,
1015+
ptr: NonNull<T>,
10161016
n_old: usize,
1017-
n_new: usize) -> Result<Unique<T>, AllocErr>
1017+
n_new: usize) -> Result<NonNull<T>, AllocErr>
10181018
where Self: Sized
10191019
{
10201020
match (Layout::array::<T>(n_old), Layout::array::<T>(n_new), ptr.as_ptr()) {
10211021
(Some(ref k_old), Some(ref k_new), ptr) if k_old.size() > 0 && k_new.size() > 0 => {
10221022
self.realloc(ptr as *mut u8, k_old.clone(), k_new.clone())
1023-
.map(|p|Unique::new_unchecked(p as *mut T))
1023+
.map(|p| NonNull::new_unchecked(p as *mut T))
10241024
}
10251025
_ => {
10261026
Err(AllocErr::invalid_input("invalid layout for realloc_array"))
@@ -1048,7 +1048,7 @@ pub unsafe trait Alloc {
10481048
/// constraints.
10491049
///
10501050
/// Always returns `Err` on arithmetic overflow.
1051-
unsafe fn dealloc_array<T>(&mut self, ptr: Unique<T>, n: usize) -> Result<(), AllocErr>
1051+
unsafe fn dealloc_array<T>(&mut self, ptr: NonNull<T>, n: usize) -> Result<(), AllocErr>
10521052
where Self: Sized
10531053
{
10541054
let raw_ptr = ptr.as_ptr() as *mut u8;

src/liballoc/raw_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<T, A: Alloc> RawVec<T, A> {
322322
// would cause overflow
323323
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
324324
match self.a.alloc_array::<T>(new_cap) {
325-
Ok(ptr) => (new_cap, ptr),
325+
Ok(ptr) => (new_cap, ptr.into()),
326326
Err(e) => self.a.oom(e),
327327
}
328328
}

src/libcore/ptr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,13 @@ impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
24522452
}
24532453
}
24542454

2455+
#[unstable(feature = "unique", issue = "27730")]
2456+
impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
2457+
fn from(p: NonNull<T>) -> Self {
2458+
Unique { pointer: p.pointer, _marker: PhantomData }
2459+
}
2460+
}
2461+
24552462
/// Previous name of `NonNull`.
24562463
#[rustc_deprecated(since = "1.24", reason = "renamed to `NonNull`")]
24572464
#[unstable(feature = "shared", issue = "27730")]

src/test/run-pass/allocator-alloc-one.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(allocator_api, unique)]
11+
#![feature(allocator_api, nonnull)]
1212

1313
use std::heap::{Heap, Alloc};
1414

0 commit comments

Comments
 (0)