File tree 5 files changed +28
-8
lines changed
5 files changed +28
-8
lines changed Original file line number Diff line number Diff line change 124
124
#![ feature( pointer_methods) ]
125
125
#![ feature( inclusive_range_fields) ]
126
126
#![ cfg_attr( stage0, feature( generic_param_attrs) ) ]
127
+ #![ feature( rustc_const_unstable) ]
127
128
128
129
#![ cfg_attr( not( test) , feature( fn_traits, i128 ) ) ]
129
130
#![ cfg_attr( test, feature( test) ) ]
Original file line number Diff line number Diff line change @@ -56,14 +56,16 @@ pub struct RawVec<T, A: Alloc = Global> {
56
56
impl < T , A : Alloc > RawVec < T , A > {
57
57
/// Like `new` but parameterized over the choice of allocator for
58
58
/// the returned RawVec.
59
- pub fn new_in ( a : A ) -> Self {
59
+ pub const fn new_in ( a : A ) -> Self {
60
60
// !0 is usize::MAX. This branch should be stripped at compile time.
61
- let cap = if mem:: size_of :: < T > ( ) == 0 { !0 } else { 0 } ;
61
+ // FIXME(mark-i-m): use this line when `if`s are allowed in `const`
62
+ //let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
62
63
63
64
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
64
65
RawVec {
65
66
ptr : Unique :: empty ( ) ,
66
- cap,
67
+ // FIXME(mark-i-m): use `cap` when ifs are allowed in const
68
+ cap : [ 0 , !0 ] [ ( mem:: size_of :: < T > ( ) == 0 ) as usize ] ,
67
69
a,
68
70
}
69
71
}
@@ -120,7 +122,7 @@ impl<T> RawVec<T, Global> {
120
122
/// RawVec with capacity 0. If T has 0 size, then it makes a
121
123
/// RawVec with capacity `usize::MAX`. Useful for implementing
122
124
/// delayed allocation.
123
- pub fn new ( ) -> Self {
125
+ pub const fn new ( ) -> Self {
124
126
Self :: new_in ( Global )
125
127
}
126
128
Original file line number Diff line number Diff line change @@ -322,7 +322,8 @@ impl<T> Vec<T> {
322
322
/// ```
323
323
#[ inline]
324
324
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
325
- pub fn new ( ) -> Vec < T > {
325
+ #[ rustc_const_unstable( feature = "const_vec_new" ) ]
326
+ pub const fn new ( ) -> Vec < T > {
326
327
Vec {
327
328
buf : RawVec :: new ( ) ,
328
329
len : 0 ,
Original file line number Diff line number Diff line change @@ -2552,10 +2552,9 @@ impl<T: Sized> Unique<T> {
2552
2552
/// This is useful for initializing types which lazily allocate, like
2553
2553
/// `Vec::new` does.
2554
2554
// FIXME: rename to dangling() to match NonNull?
2555
- pub fn empty ( ) -> Self {
2555
+ pub const fn empty ( ) -> Self {
2556
2556
unsafe {
2557
- let ptr = mem:: align_of :: < T > ( ) as * mut T ;
2558
- Unique :: new_unchecked ( ptr)
2557
+ Unique :: new_unchecked ( mem:: align_of :: < T > ( ) as * mut T )
2559
2558
}
2560
2559
}
2561
2560
}
Original file line number Diff line number Diff line change
1
+ // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2
+ // file at the top-level directory of this distribution and at
3
+ // http://rust-lang.org/COPYRIGHT.
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8
+ // option. This file may not be copied, modified, or distributed
9
+ // except according to those terms.
10
+
11
+ // Test that Vec::new() can be used for constants
12
+
13
+ #![ feature( const_vec_new) ]
14
+
15
+ const MY_VEC : Vec < usize > = Vec :: new ( ) ;
16
+
17
+ pub fn main ( ) { }
You can’t perform that action at this time.
0 commit comments