]> git.lizzy.rs Git - rust.git/commitdiff
Make Vec::new const
authorMark Mansi <markm@cs.wisc.edu>
Wed, 25 Apr 2018 21:33:02 +0000 (16:33 -0500)
committerMark Mansi <markm@cs.wisc.edu>
Wed, 25 Apr 2018 21:33:02 +0000 (16:33 -0500)
src/liballoc/raw_vec.rs
src/liballoc/vec.rs
src/libcore/ptr.rs
src/test/run-pass/vec-const-new.rs [new file with mode: 0644]

index 7ef0a27fc7258876768fb3abfaac3396e9ff8da4..dc8ad9ee06169279fafa461a96769a32593f04c7 100644 (file)
@@ -68,6 +68,16 @@ pub fn new_in(a: A) -> Self {
         }
     }
 
+    /// Like `empty` but parametrized over the choice of allocator for the returned `RawVec`.
+    pub const fn empty_in(a: A) -> Self {
+        // Unique::empty() doubles as "unallocated" and "zero-sized allocation"
+        RawVec {
+            ptr: Unique::empty(),
+            cap: 0,
+            a,
+        }
+    }
+
     /// Like `with_capacity` but parameterized over the choice of
     /// allocator for the returned RawVec.
     #[inline]
@@ -124,6 +134,12 @@ pub fn new() -> Self {
         Self::new_in(Global)
     }
 
+    /// Create a `RawVec` with capcity 0 (on the system heap), regardless of `T`, without
+    /// allocating.
+    pub fn empty() -> Self {
+        Self::empty_in(Global)
+    }
+
     /// Creates a RawVec (on the system heap) with exactly the
     /// capacity and alignment requirements for a `[T; cap]`. This is
     /// equivalent to calling RawVec::new when `cap` is 0 or T is
index b184404c15bfd6c050b99252db3c15e41b2d00a2..757606607bbcfc3f9184b72eb88dc337d48a693a 100644 (file)
@@ -324,7 +324,7 @@ impl<T> Vec<T> {
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn new() -> Vec<T> {
         Vec {
-            buf: RawVec::new(),
+            buf: RawVec::empty(),
             len: 0,
         }
     }
index 74bb264cc679cc31c310cefade5b196cbd6881b8..b612a278a34a759bcb110208860756bd920ed033 100644 (file)
@@ -2551,10 +2551,9 @@ impl<T: Sized> Unique<T> {
     /// This is useful for initializing types which lazily allocate, like
     /// `Vec::new` does.
     // FIXME: rename to dangling() to match NonNull?
-    pub fn empty() -> Self {
+    pub const fn empty() -> Self {
         unsafe {
-            let ptr = mem::align_of::<T>() as *mut T;
-            Unique::new_unchecked(ptr)
+            Unique::new_unchecked(mem::align_of::<T>() as *mut T)
         }
     }
 }
diff --git a/src/test/run-pass/vec-const-new.rs b/src/test/run-pass/vec-const-new.rs
new file mode 100644 (file)
index 0000000..02d8cfd
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Test that Vec::new() can be used for constants
+
+const MY_VEC: Vec<usize> = Vec::new();
+
+pub fn main() {}