Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct RawVec<T, A: Alloc = Global> {
}

impl<T, A: Alloc> RawVec<T, A> {
// FIXME: this should be made `const` when `if` statements are allowed
/// Like `new` but parameterized over the choice of allocator for
/// the returned RawVec.
pub fn new_in(a: A) -> Self {
Expand All @@ -68,6 +69,17 @@ impl<T, A: Alloc> RawVec<T, A> {
}
}

// FIXME: this should removed when `new_in` can be made `const`
/// Like `empty` but parametrized over the choice of allocator for the returned `RawVec`.
pub const fn empty_in(a: A) -> Self {
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
RawVec {
ptr: Unique::empty(),
cap: 0,
a,
}
}

/// Like `with_capacity` but parameterized over the choice of
/// allocator for the returned RawVec.
#[inline]
Expand Down Expand Up @@ -124,6 +136,13 @@ impl<T> RawVec<T, Global> {
Self::new_in(Global)
}

// FIXME: this should removed when `new` can be made `const`
/// Create a `RawVec` with capcity 0 (on the system heap), regardless of `T`, without
/// allocating.
pub const fn empty() -> Self {
Self::empty_in(Global)
}

/// Creates a RawVec (on the system heap) with exactly the
/// capacity and alignment requirements for a `[T; cap]`. This is
/// equivalent to calling RawVec::new when `cap` is 0 or T is
Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ impl<T> Vec<T> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> Vec<T> {
pub const fn new() -> Vec<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mark this with #[rustc_const_unstable(feature = "const_vec_new")] so we don't need to insta-stable this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennytm Done.

Vec {
buf: RawVec::new(),
buf: RawVec::empty(),
len: 0,
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2551,10 +2551,9 @@ impl<T: Sized> Unique<T> {
/// This is useful for initializing types which lazily allocate, like
/// `Vec::new` does.
// FIXME: rename to dangling() to match NonNull?
pub fn empty() -> Self {
pub const fn empty() -> Self {
unsafe {
let ptr = mem::align_of::<T>() as *mut T;
Unique::new_unchecked(ptr)
Unique::new_unchecked(mem::align_of::<T>() as *mut T)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/run-pass/vec-const-new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2012 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.

// Test that Vec::new() can be used for constants

const MY_VEC: Vec<usize> = Vec::new();

pub fn main() {}