about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorMark Mansi <markm@cs.wisc.edu>2019-11-25 14:44:19 -0600
committerMark Mansi <markm@cs.wisc.edu>2019-12-18 20:19:05 -0600
commit17aa0cb2ca73ad789e718bf9162a740af02a829f (patch)
tree598f7bf295befe800fe02a59941e4e0f206ffbf4 /src/liballoc
parentc605199e89572e586a5f37bc698c48b6a10896fb (diff)
downloadrust-17aa0cb2ca73ad789e718bf9162a740af02a829f.tar.gz
rust-17aa0cb2ca73ad789e718bf9162a740af02a829f.zip
Remove a const-if-hack in RawVec
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/raw_vec.rs40
1 files changed, 32 insertions, 8 deletions
diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs
index ee75fc288fe..dec990a117b 100644
--- a/src/liballoc/raw_vec.rs
+++ b/src/liballoc/raw_vec.rs
@@ -1,6 +1,8 @@
 #![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "0")]
 #![doc(hidden)]
 
+#![feature(const_if_match)]
+
 use core::cmp;
 use core::mem;
 use core::ops::Drop;
@@ -51,15 +53,24 @@ pub struct RawVec<T, A: Alloc = Global> {
 impl<T, A: Alloc> RawVec<T, A> {
     /// Like `new`, but parameterized over the choice of allocator for
     /// the returned `RawVec`.
+    #[cfg(not(bootstrap))]
     pub const fn new_in(a: A) -> Self {
-        // `!0` is `usize::MAX`. This branch should be stripped at compile time.
-        // FIXME(mark-i-m): use this line when `if`s are allowed in `const`:
-        //let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
+        let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
 
         // `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
         RawVec {
             ptr: Unique::empty(),
-            // FIXME(mark-i-m): use `cap` when ifs are allowed in const
+            cap,
+            a,
+        }
+    }
+
+    /// Like `new`, but parameterized over the choice of allocator for
+    /// the returned `RawVec`.
+    #[cfg(bootstrap)]
+    pub const fn new_in(a: A) -> Self {
+        RawVec {
+            ptr: Unique::empty(),
             cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
             a,
         }
@@ -131,17 +142,30 @@ impl<T> RawVec<T, Global> {
     /// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
     /// `RawVec` with capacity `usize::MAX`. Useful for implementing
     /// delayed allocation.
+    #[cfg(not(bootstrap))]
     pub const fn new() -> Self {
         // FIXME(Centril): Reintegrate this with `fn new_in` when we can.
 
-        // `!0` is `usize::MAX`. This branch should be stripped at compile time.
-        // FIXME(mark-i-m): use this line when `if`s are allowed in `const`:
-        //let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
+        let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
 
         // `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
         RawVec {
             ptr: Unique::empty(),
-            // FIXME(mark-i-m): use `cap` when ifs are allowed in const
+            cap,
+            a: Global,
+        }
+    }
+
+    /// Creates the biggest possible `RawVec` (on the system heap)
+    /// without allocating. If `T` has positive size, then this makes a
+    /// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
+    /// `RawVec` with capacity `usize::MAX`. Useful for implementing
+    /// delayed allocation.
+    #[cfg(bootstrap)]
+    pub const fn new() -> Self {
+        // `Unique::empty()` doubles as "unallocated" and "zero-sized allocation".
+        RawVec {
+            ptr: Unique::empty(),
             cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
             a: Global,
         }