Skip to content

Commit 208f353

Browse files
authored
Rollup merge of rust-lang#66755 - mark-i-m:const-vec-new, r=ecstatic-morse
Remove a const-if-hack in RawVec r? @ecstatic-morse cc @Centril
2 parents 02206dc + 7d26811 commit 208f353

File tree

2 files changed

+4
-18
lines changed

2 files changed

+4
-18
lines changed

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#![feature(const_generic_impls_guard)]
8686
#![feature(const_generics)]
8787
#![feature(const_in_array_repeat_expressions)]
88+
#![feature(const_if_match)]
8889
#![feature(cow_is_borrowed)]
8990
#![feature(dispatch_from_dyn)]
9091
#![feature(core_intrinsics)]

src/liballoc/raw_vec.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,12 @@ impl<T, A: Alloc> RawVec<T, A> {
5252
/// Like `new`, but parameterized over the choice of allocator for
5353
/// the returned `RawVec`.
5454
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 };
55+
let cap = if mem::size_of::<T>() == 0 { core::usize::MAX } else { 0 };
5856

5957
// `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
6058
RawVec {
6159
ptr: Unique::empty(),
62-
// FIXME(mark-i-m): use `cap` when ifs are allowed in const
63-
cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
60+
cap,
6461
a,
6562
}
6663
}
@@ -132,19 +129,7 @@ impl<T> RawVec<T, Global> {
132129
/// `RawVec` with capacity `usize::MAX`. Useful for implementing
133130
/// delayed allocation.
134131
pub const fn new() -> Self {
135-
// FIXME(Centril): Reintegrate this with `fn new_in` when we can.
136-
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 };
140-
141-
// `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
142-
RawVec {
143-
ptr: Unique::empty(),
144-
// FIXME(mark-i-m): use `cap` when ifs are allowed in const
145-
cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
146-
a: Global,
147-
}
132+
Self::new_in(Global)
148133
}
149134

150135
/// Creates a `RawVec` (on the system heap) with exactly the

0 commit comments

Comments
 (0)