]> git.lizzy.rs Git - rust.git/commitdiff
std: Fix implementation of `Alloc::alloc_one`
authorAlex Crichton <alex@alexcrichton.com>
Sun, 25 Jun 2017 18:33:47 +0000 (11:33 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sun, 25 Jun 2017 18:35:05 +0000 (11:35 -0700)
This had an accidental `u8 as *mut T` where it was intended to have just a
normal pointer-to-pointer cast.

Closes #42827

src/liballoc/allocator.rs
src/test/run-pass/allocator-alloc-one.rs [new file with mode: 0644]

index 9bddce29957e1a514b5ad5d8f93ca323fc8a2fcd..bf38629ed38a7a344eb5214842a49c340428fab1 100644 (file)
@@ -873,7 +873,7 @@ fn alloc_one<T>(&mut self) -> Result<Unique<T>, AllocErr>
     {
         let k = Layout::new::<T>();
         if k.size() > 0 {
-            unsafe { self.alloc(k).map(|p|Unique::new(*p as *mut T)) }
+            unsafe { self.alloc(k).map(|p| Unique::new(p as *mut T)) }
         } else {
             Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
         }
diff --git a/src/test/run-pass/allocator-alloc-one.rs b/src/test/run-pass/allocator-alloc-one.rs
new file mode 100644 (file)
index 0000000..7cc547d
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(alloc, allocator_api, heap_api, unique)]
+
+extern crate alloc;
+
+use alloc::heap::HeapAlloc;
+use alloc::allocator::Alloc;
+
+fn main() {
+    unsafe {
+        let ptr = HeapAlloc.alloc_one::<i32>().unwrap_or_else(|e| {
+            HeapAlloc.oom(e)
+        });
+        *ptr.as_ptr() = 4;
+        assert_eq!(*ptr.as_ptr(), 4);
+        HeapAlloc.dealloc_one(ptr);
+    }
+}