Skip to content

Commit ed29777

Browse files
committed
Remove conversions for allocated pointers
One was now unused, and `NonNull::new(…).ok_or(AllocErr)` feels short enough for the few cases that need the other conversion.
1 parent fddf51e commit ed29777

File tree

3 files changed

+11
-28
lines changed

3 files changed

+11
-28
lines changed

src/liballoc/alloc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ unsafe impl GlobalAlloc for Global {
122122
unsafe impl Alloc for Global {
123123
#[inline]
124124
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
125-
GlobalAlloc::alloc(self, layout).into()
125+
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
126126
}
127127

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

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

148148
#[inline]

src/liballoc_system/lib.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ pub struct System;
5151
unsafe impl Alloc for System {
5252
#[inline]
5353
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
54-
GlobalAlloc::alloc(self, layout).into()
54+
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
5555
}
5656

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

6262
#[inline]
@@ -67,9 +67,9 @@ unsafe impl Alloc for System {
6767
#[inline]
6868
unsafe fn realloc(&mut self,
6969
ptr: NonNull<Void>,
70-
old_layout: Layout,
70+
layout: Layout,
7171
new_size: usize) -> Result<NonNull<Void>, AllocErr> {
72-
GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size).into()
72+
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
7373
}
7474

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

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

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

107107
#[inline]

src/libcore/alloc.rs

-17
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,6 @@ impl Void {
4141
}
4242
}
4343

44-
/// Convert from a return value of GlobalAlloc::alloc to that of Alloc::alloc
45-
impl From<*mut Void> for Result<NonNull<Void>, AllocErr> {
46-
fn from(ptr: *mut Void) -> Self {
47-
NonNull::new(ptr).ok_or(AllocErr)
48-
}
49-
}
50-
51-
/// Convert from a return value of Alloc::alloc to that of GlobalAlloc::alloc
52-
impl From<Result<NonNull<Void>, AllocErr>> for *mut Void {
53-
fn from(result: Result<NonNull<Void>, AllocErr>) -> Self {
54-
match result {
55-
Ok(ptr) => ptr.as_ptr(),
56-
Err(_) => Void::null_mut(),
57-
}
58-
}
59-
}
60-
6144
/// Represents the combination of a starting address and
6245
/// a total capacity of the returned block.
6346
#[derive(Debug)]

0 commit comments

Comments
 (0)