Skip to content

Commit 17aa0cb

Browse files
committed
Remove a const-if-hack in RawVec
1 parent c605199 commit 17aa0cb

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/liballoc/raw_vec.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "0")]
22
#![doc(hidden)]
33

4+
#![feature(const_if_match)]
5+
46
use core::cmp;
57
use core::mem;
68
use core::ops::Drop;
@@ -51,15 +53,24 @@ pub struct RawVec<T, A: Alloc = Global> {
5153
impl<T, A: Alloc> RawVec<T, A> {
5254
/// Like `new`, but parameterized over the choice of allocator for
5355
/// the returned `RawVec`.
56+
#[cfg(not(bootstrap))]
5457
pub const fn new_in(a: A) -> Self {
55-
// `!0` is `usize::MAX`. This branch should be stripped at compile time.
56-
// FIXME(mark-i-m): use this line when `if`s are allowed in `const`:
57-
//let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
58+
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
5859

5960
// `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
6061
RawVec {
6162
ptr: Unique::empty(),
62-
// FIXME(mark-i-m): use `cap` when ifs are allowed in const
63+
cap,
64+
a,
65+
}
66+
}
67+
68+
/// Like `new`, but parameterized over the choice of allocator for
69+
/// the returned `RawVec`.
70+
#[cfg(bootstrap)]
71+
pub const fn new_in(a: A) -> Self {
72+
RawVec {
73+
ptr: Unique::empty(),
6374
cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
6475
a,
6576
}
@@ -131,17 +142,30 @@ impl<T> RawVec<T, Global> {
131142
/// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
132143
/// `RawVec` with capacity `usize::MAX`. Useful for implementing
133144
/// delayed allocation.
145+
#[cfg(not(bootstrap))]
134146
pub const fn new() -> Self {
135147
// FIXME(Centril): Reintegrate this with `fn new_in` when we can.
136148

137-
// `!0` is `usize::MAX`. This branch should be stripped at compile time.
138-
// FIXME(mark-i-m): use this line when `if`s are allowed in `const`:
139-
//let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
149+
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
140150

141151
// `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
142152
RawVec {
143153
ptr: Unique::empty(),
144-
// FIXME(mark-i-m): use `cap` when ifs are allowed in const
154+
cap,
155+
a: Global,
156+
}
157+
}
158+
159+
/// Creates the biggest possible `RawVec` (on the system heap)
160+
/// without allocating. If `T` has positive size, then this makes a
161+
/// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
162+
/// `RawVec` with capacity `usize::MAX`. Useful for implementing
163+
/// delayed allocation.
164+
#[cfg(bootstrap)]
165+
pub const fn new() -> Self {
166+
// `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
167+
RawVec {
168+
ptr: Unique::empty(),
145169
cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
146170
a: Global,
147171
}

0 commit comments

Comments
 (0)