Skip to content

Commit

Permalink
Rollup merge of rust-lang#42901 - alexcrichton:alloc-one, r=sfackler
Browse files Browse the repository at this point in the history
std: Fix implementation of `Alloc::alloc_one`

This had an accidental `u8 as *mut T` where it was intended to have just a
normal pointer-to-pointer cast.

Closes rust-lang#42827
  • Loading branch information
Ariel Ben-Yehuda authored Jun 29, 2017
2 parents effd1e0 + d24d408 commit ea762f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/liballoc/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ pub unsafe trait Alloc {
{
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"))
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/run-pass/allocator-alloc-one.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit ea762f2

Please sign in to comment.