diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2018-04-04 18:09:39 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2018-04-12 22:53:14 +0200 |
| commit | 747cc749430d66bd2fca8e81fd8a1c994e36dcf1 (patch) | |
| tree | ba50ae6ca9a8e021439f2c643a0fc785d42c58c3 /src/libcore/alloc.rs | |
| parent | c957e99b305ecee113442a7ce0edd6b565200ca9 (diff) | |
| download | rust-747cc749430d66bd2fca8e81fd8a1c994e36dcf1.tar.gz rust-747cc749430d66bd2fca8e81fd8a1c994e36dcf1.zip | |
Conversions between Result<*mut u8, AllocErr>> and *mut Void
Diffstat (limited to 'src/libcore/alloc.rs')
| -rw-r--r-- | src/libcore/alloc.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 757f06e731f..cfa7df06a40 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -41,6 +41,27 @@ impl Void { } } +/// 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)] |
