|
1 | 1 | #![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "0")]
|
2 | 2 | #![doc(hidden)]
|
3 | 3 |
|
| 4 | +#![feature(const_if_match)] |
| 5 | + |
4 | 6 | use core::cmp;
|
5 | 7 | use core::mem;
|
6 | 8 | use core::ops::Drop;
|
@@ -51,15 +53,24 @@ pub struct RawVec<T, A: Alloc = Global> {
|
51 | 53 | impl<T, A: Alloc> RawVec<T, A> {
|
52 | 54 | /// Like `new`, but parameterized over the choice of allocator for
|
53 | 55 | /// the returned `RawVec`.
|
| 56 | + #[cfg(not(bootstrap))] |
54 | 57 | 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 }; |
58 | 59 |
|
59 | 60 | // `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
|
60 | 61 | RawVec {
|
61 | 62 | 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(), |
63 | 74 | cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
|
64 | 75 | a,
|
65 | 76 | }
|
@@ -131,17 +142,30 @@ impl<T> RawVec<T, Global> {
|
131 | 142 | /// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
|
132 | 143 | /// `RawVec` with capacity `usize::MAX`. Useful for implementing
|
133 | 144 | /// delayed allocation.
|
| 145 | + #[cfg(not(bootstrap))] |
134 | 146 | pub const fn new() -> Self {
|
135 | 147 | // FIXME(Centril): Reintegrate this with `fn new_in` when we can.
|
136 | 148 |
|
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 }; |
140 | 150 |
|
141 | 151 | // `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
|
142 | 152 | RawVec {
|
143 | 153 | 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(), |
145 | 169 | cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
|
146 | 170 | a: Global,
|
147 | 171 | }
|
|
0 commit comments