]> git.lizzy.rs Git - rust.git/commitdiff
Conversions between Result<*mut u8, AllocErr>> and *mut Void
authorSimon Sapin <simon.sapin@exyr.org>
Wed, 4 Apr 2018 16:09:39 +0000 (18:09 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Thu, 12 Apr 2018 20:53:14 +0000 (22:53 +0200)
src/liballoc_system/lib.rs
src/libcore/alloc.rs

index 7b788a5f9898da1fc7df377b93117edfd4b8861c..6ffbd029281c426a6601f527f8f8d3ba76e05bac 100644 (file)
@@ -139,22 +139,12 @@ macro_rules! alloc_methods_based_on_global_alloc {
     () => {
         #[inline]
         unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
-            let ptr = GlobalAlloc::alloc(*self, layout);
-            if !ptr.is_null() {
-                Ok(ptr as *mut u8)
-            } else {
-                Err(AllocErr)
-            }
+            GlobalAlloc::alloc(*self, layout).into()
         }
 
         #[inline]
         unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
-            let ptr = GlobalAlloc::alloc_zeroed(*self, layout);
-            if !ptr.is_null() {
-                Ok(ptr as *mut u8)
-            } else {
-                Err(AllocErr)
-            }
+            GlobalAlloc::alloc_zeroed(*self, layout).into()
         }
 
         #[inline]
@@ -167,12 +157,7 @@ unsafe fn realloc(&mut self,
                           ptr: *mut u8,
                           old_layout: Layout,
                           new_size: usize) -> Result<*mut u8, AllocErr> {
-            let ptr = GlobalAlloc::realloc(*self, ptr as *mut Void, old_layout, new_size);
-            if !ptr.is_null() {
-                Ok(ptr as *mut u8)
-            } else {
-                Err(AllocErr)
-            }
+            GlobalAlloc::realloc(*self, ptr as *mut Void, old_layout, new_size).into()
         }
     }
 }
index 757f06e731ffca5027e2cac54c582dedb199a30c..cfa7df06a40e17e9e62089cdaa6f57a518798eee 100644 (file)
@@ -41,6 +41,27 @@ pub fn null_mut() -> *mut Self {
     }
 }
 
+/// Convert from a return value of GlobalAlloc::alloc to that of Alloc::alloc
+impl From<*mut Void> for Result<*mut u8, AllocErr> {
+    fn from(ptr: *mut Void) -> Self {
+        if !ptr.is_null() {
+            Ok(ptr as *mut u8)
+        } else {
+            Err(AllocErr)
+        }
+    }
+}
+
+/// Convert from a return value of Alloc::alloc to that of GlobalAlloc::alloc
+impl From<Result<*mut u8, AllocErr>> for *mut Void {
+    fn from(result: Result<*mut u8, AllocErr>) -> Self {
+        match result {
+            Ok(ptr) => ptr as *mut Void,
+            Err(_) => Void::null_mut(),
+        }
+    }
+}
+
 /// Represents the combination of a starting address and
 /// a total capacity of the returned block.
 #[derive(Debug)]