Make Vec::new const
This commit is contained in:
parent
25749ad66d
commit
256096da9e
4 changed files with 34 additions and 4 deletions
|
@ -68,6 +68,16 @@ impl<T, A: Alloc> RawVec<T, A> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
/// Like `with_capacity` but parameterized over the choice of
|
||||||
/// allocator for the returned RawVec.
|
/// allocator for the returned RawVec.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -124,6 +134,12 @@ impl<T> RawVec<T, Global> {
|
||||||
Self::new_in(Global)
|
Self::new_in(Global)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a `RawVec` with capcity 0 (on the system heap), regardless of `T`, without
|
||||||
|
/// allocating.
|
||||||
|
pub fn empty() -> Self {
|
||||||
|
Self::empty_in(Global)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a RawVec (on the system heap) with exactly the
|
/// Creates a RawVec (on the system heap) with exactly the
|
||||||
/// capacity and alignment requirements for a `[T; cap]`. This is
|
/// capacity and alignment requirements for a `[T; cap]`. This is
|
||||||
/// equivalent to calling RawVec::new when `cap` is 0 or T is
|
/// equivalent to calling RawVec::new when `cap` is 0 or T is
|
||||||
|
|
|
@ -324,7 +324,7 @@ impl<T> Vec<T> {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn new() -> Vec<T> {
|
pub fn new() -> Vec<T> {
|
||||||
Vec {
|
Vec {
|
||||||
buf: RawVec::new(),
|
buf: RawVec::empty(),
|
||||||
len: 0,
|
len: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2551,10 +2551,9 @@ impl<T: Sized> Unique<T> {
|
||||||
/// This is useful for initializing types which lazily allocate, like
|
/// This is useful for initializing types which lazily allocate, like
|
||||||
/// `Vec::new` does.
|
/// `Vec::new` does.
|
||||||
// FIXME: rename to dangling() to match NonNull?
|
// FIXME: rename to dangling() to match NonNull?
|
||||||
pub fn empty() -> Self {
|
pub const fn empty() -> Self {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr = mem::align_of::<T>() as *mut T;
|
Unique::new_unchecked(mem::align_of::<T>() as *mut T)
|
||||||
Unique::new_unchecked(ptr)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
15
src/test/run-pass/vec-const-new.rs
Normal file
15
src/test/run-pass/vec-const-new.rs
Normal file
|
@ -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() {}
|
Loading…
Add table
Add a link
Reference in a new issue