Skip to content

Commit e5280e4

Browse files
committed
use const trick
1 parent 0212e02 commit e5280e4

File tree

2 files changed

+7
-24
lines changed

2 files changed

+7
-24
lines changed

src/liballoc/raw_vec.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,18 @@ pub struct RawVec<T, A: Alloc = Global> {
5454
}
5555

5656
impl<T, A: Alloc> RawVec<T, A> {
57-
// FIXME: this should be made `const` when `if` statements are allowed
5857
/// Like `new` but parameterized over the choice of allocator for
5958
/// the returned RawVec.
60-
pub fn new_in(a: A) -> Self {
59+
pub const fn new_in(a: A) -> Self {
6160
// !0 is usize::MAX. This branch should be stripped at compile time.
62-
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
61+
// FIXME(mark-i-m): use this line when `if`s are allowed in `const`
62+
//let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
6363

6464
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
6565
RawVec {
6666
ptr: Unique::empty(),
67-
cap,
68-
a,
69-
}
70-
}
71-
72-
// FIXME: this should removed when `new_in` can be made `const`
73-
/// Like `empty` but parametrized over the choice of allocator for the returned `RawVec`.
74-
pub const fn empty_in(a: A) -> Self {
75-
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
76-
RawVec {
77-
ptr: Unique::empty(),
78-
cap: 0,
67+
// FIXME(mark-i-m): use `cap` when ifs are allowed in const
68+
cap: [0, !0][(mem::size_of::<T>() != 0) as usize],
7969
a,
8070
}
8171
}
@@ -132,17 +122,10 @@ impl<T> RawVec<T, Global> {
132122
/// RawVec with capacity 0. If T has 0 size, then it makes a
133123
/// RawVec with capacity `usize::MAX`. Useful for implementing
134124
/// delayed allocation.
135-
pub fn new() -> Self {
125+
pub const fn new() -> Self {
136126
Self::new_in(Global)
137127
}
138128

139-
// FIXME: this should removed when `new` can be made `const`
140-
/// Create a `RawVec` with capcity 0 (on the system heap), regardless of `T`, without
141-
/// allocating.
142-
pub const fn empty() -> Self {
143-
Self::empty_in(Global)
144-
}
145-
146129
/// Creates a RawVec (on the system heap) with exactly the
147130
/// capacity and alignment requirements for a `[T; cap]`. This is
148131
/// equivalent to calling RawVec::new when `cap` is 0 or T is

src/liballoc/vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<T> Vec<T> {
325325
#[rustc_const_unstable(feature = "const_vec_new")]
326326
pub const fn new() -> Vec<T> {
327327
Vec {
328-
buf: RawVec::empty(),
328+
buf: RawVec::new(),
329329
len: 0,
330330
}
331331
}

0 commit comments

Comments
 (0)