Skip to content

Commit 256096d

Browse files
committed
Make Vec::new const
1 parent 25749ad commit 256096d

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/liballoc/raw_vec.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ impl<T, A: Alloc> RawVec<T, A> {
6868
}
6969
}
7070

71+
/// Like `empty` but parametrized over the choice of allocator for the returned `RawVec`.
72+
pub const fn empty_in(a: A) -> Self {
73+
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
74+
RawVec {
75+
ptr: Unique::empty(),
76+
cap: 0,
77+
a,
78+
}
79+
}
80+
7181
/// Like `with_capacity` but parameterized over the choice of
7282
/// allocator for the returned RawVec.
7383
#[inline]
@@ -124,6 +134,12 @@ impl<T> RawVec<T, Global> {
124134
Self::new_in(Global)
125135
}
126136

137+
/// Create a `RawVec` with capcity 0 (on the system heap), regardless of `T`, without
138+
/// allocating.
139+
pub fn empty() -> Self {
140+
Self::empty_in(Global)
141+
}
142+
127143
/// Creates a RawVec (on the system heap) with exactly the
128144
/// capacity and alignment requirements for a `[T; cap]`. This is
129145
/// equivalent to calling RawVec::new when `cap` is 0 or T is

src/liballoc/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl<T> Vec<T> {
324324
#[stable(feature = "rust1", since = "1.0.0")]
325325
pub fn new() -> Vec<T> {
326326
Vec {
327-
buf: RawVec::new(),
327+
buf: RawVec::empty(),
328328
len: 0,
329329
}
330330
}

src/libcore/ptr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,10 +2551,9 @@ impl<T: Sized> Unique<T> {
25512551
/// This is useful for initializing types which lazily allocate, like
25522552
/// `Vec::new` does.
25532553
// FIXME: rename to dangling() to match NonNull?
2554-
pub fn empty() -> Self {
2554+
pub const fn empty() -> Self {
25552555
unsafe {
2556-
let ptr = mem::align_of::<T>() as *mut T;
2557-
Unique::new_unchecked(ptr)
2556+
Unique::new_unchecked(mem::align_of::<T>() as *mut T)
25582557
}
25592558
}
25602559
}

src/test/run-pass/vec-const-new.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2012 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+
// Test that Vec::new() can be used for constants
12+
13+
const MY_VEC: Vec<usize> = Vec::new();
14+
15+
pub fn main() {}

0 commit comments

Comments
 (0)