Skip to content

Commit

Permalink
Remove conversions for allocated pointers
Browse files Browse the repository at this point in the history
One was now unused, and `NonNull::new(…).ok_or(AllocErr)` feels short enough
for the few cases that need the other conversion.
  • Loading branch information
SimonSapin committed Apr 12, 2018
1 parent fddf51e commit ed29777
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/liballoc/alloc.rs
Expand Up @@ -122,7 +122,7 @@ unsafe impl GlobalAlloc for Global {
unsafe impl Alloc for Global {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
GlobalAlloc::alloc(self, layout).into()
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
}

#[inline]
Expand All @@ -137,12 +137,12 @@ unsafe impl Alloc for Global {
new_size: usize)
-> Result<NonNull<Void>, AllocErr>
{
GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size).into()
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}

#[inline]
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
GlobalAlloc::alloc_zeroed(self, layout).into()
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
}

#[inline]
Expand Down
16 changes: 8 additions & 8 deletions src/liballoc_system/lib.rs
Expand Up @@ -51,12 +51,12 @@ pub struct System;
unsafe impl Alloc for System {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
GlobalAlloc::alloc(self, layout).into()
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
}

#[inline]
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
GlobalAlloc::alloc_zeroed(self, layout).into()
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
}

#[inline]
Expand All @@ -67,9 +67,9 @@ unsafe impl Alloc for System {
#[inline]
unsafe fn realloc(&mut self,
ptr: NonNull<Void>,
old_layout: Layout,
layout: Layout,
new_size: usize) -> Result<NonNull<Void>, AllocErr> {
GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size).into()
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}

#[inline]
Expand All @@ -83,12 +83,12 @@ unsafe impl Alloc for System {
unsafe impl<'a> Alloc for &'a System {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
GlobalAlloc::alloc(*self, layout).into()
NonNull::new(GlobalAlloc::alloc(*self, layout)).ok_or(AllocErr)
}

#[inline]
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
GlobalAlloc::alloc_zeroed(*self, layout).into()
NonNull::new(GlobalAlloc::alloc_zeroed(*self, layout)).ok_or(AllocErr)
}

#[inline]
Expand All @@ -99,9 +99,9 @@ unsafe impl<'a> Alloc for &'a System {
#[inline]
unsafe fn realloc(&mut self,
ptr: NonNull<Void>,
old_layout: Layout,
layout: Layout,
new_size: usize) -> Result<NonNull<Void>, AllocErr> {
GlobalAlloc::realloc(*self, ptr.as_ptr(), old_layout, new_size).into()
NonNull::new(GlobalAlloc::realloc(*self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}

#[inline]
Expand Down
17 changes: 0 additions & 17 deletions src/libcore/alloc.rs
Expand Up @@ -41,23 +41,6 @@ impl Void {
}
}

/// Convert from a return value of GlobalAlloc::alloc to that of Alloc::alloc
impl From<*mut Void> for Result<NonNull<Void>, AllocErr> {
fn from(ptr: *mut Void) -> Self {
NonNull::new(ptr).ok_or(AllocErr)
}
}

/// Convert from a return value of Alloc::alloc to that of GlobalAlloc::alloc
impl From<Result<NonNull<Void>, AllocErr>> for *mut Void {
fn from(result: Result<NonNull<Void>, AllocErr>) -> Self {
match result {
Ok(ptr) => ptr.as_ptr(),
Err(_) => Void::null_mut(),
}
}
}

/// Represents the combination of a starting address and
/// a total capacity of the returned block.
#[derive(Debug)]
Expand Down

0 comments on commit ed29777

Please sign in to comment.