Skip to content

Commit 110fda6

Browse files
authored
Rollup merge of rust-lang#42901 - alexcrichton:alloc-one, r=sfackler
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
2 parents 48303df + d24d408 commit 110fda6

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/liballoc/allocator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ pub unsafe trait Alloc {
873873
{
874874
let k = Layout::new::<T>();
875875
if k.size() > 0 {
876-
unsafe { self.alloc(k).map(|p|Unique::new(*p as *mut T)) }
876+
unsafe { self.alloc(k).map(|p| Unique::new(p as *mut T)) }
877877
} else {
878878
Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
879879
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(alloc, allocator_api, heap_api, unique)]
12+
13+
extern crate alloc;
14+
15+
use alloc::heap::HeapAlloc;
16+
use alloc::allocator::Alloc;
17+
18+
fn main() {
19+
unsafe {
20+
let ptr = HeapAlloc.alloc_one::<i32>().unwrap_or_else(|e| {
21+
HeapAlloc.oom(e)
22+
});
23+
*ptr.as_ptr() = 4;
24+
assert_eq!(*ptr.as_ptr(), 4);
25+
HeapAlloc.dealloc_one(ptr);
26+
}
27+
}

0 commit comments

Comments
 (0)