@@ -946,7 +946,8 @@ impl<T, A: Allocator> Vec<T, A> {
946
946
/// ```
947
947
#[ inline]
948
948
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
949
- pub fn capacity ( & self ) -> usize {
949
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
950
+ pub const fn capacity ( & self ) -> usize {
950
951
self . buf . capacity ( )
951
952
}
952
953
@@ -1253,8 +1254,9 @@ impl<T, A: Allocator> Vec<T, A> {
1253
1254
/// ```
1254
1255
#[ inline]
1255
1256
#[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
1256
- pub fn as_slice ( & self ) -> & [ T ] {
1257
- self
1257
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1258
+ pub const fn as_slice ( & self ) -> & [ T ] {
1259
+ unsafe { slice:: from_raw_parts ( self . as_ptr ( ) , self . len ) }
1258
1260
}
1259
1261
1260
1262
/// Extracts a mutable slice of the entire vector.
@@ -1271,7 +1273,7 @@ impl<T, A: Allocator> Vec<T, A> {
1271
1273
#[ inline]
1272
1274
#[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
1273
1275
pub fn as_mut_slice ( & mut self ) -> & mut [ T ] {
1274
- self
1276
+ unsafe { slice :: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
1275
1277
}
1276
1278
1277
1279
/// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
@@ -1326,9 +1328,10 @@ impl<T, A: Allocator> Vec<T, A> {
1326
1328
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1327
1329
/// [`as_ptr`]: Vec::as_ptr
1328
1330
#[ stable( feature = "vec_as_ptr" , since = "1.37.0" ) ]
1331
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1329
1332
#[ rustc_never_returns_null_ptr]
1330
1333
#[ inline]
1331
- pub fn as_ptr ( & self ) -> * const T {
1334
+ pub const fn as_ptr ( & self ) -> * const T {
1332
1335
// We shadow the slice method of the same name to avoid going through
1333
1336
// `deref`, which creates an intermediate reference.
1334
1337
self . buf . ptr ( )
@@ -2262,8 +2265,9 @@ impl<T, A: Allocator> Vec<T, A> {
2262
2265
/// ```
2263
2266
#[ inline]
2264
2267
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2268
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
2265
2269
#[ rustc_confusables( "length" , "size" ) ]
2266
- pub fn len ( & self ) -> usize {
2270
+ pub const fn len ( & self ) -> usize {
2267
2271
self . len
2268
2272
}
2269
2273
@@ -2279,7 +2283,8 @@ impl<T, A: Allocator> Vec<T, A> {
2279
2283
/// assert!(!v.is_empty());
2280
2284
/// ```
2281
2285
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2282
- pub fn is_empty ( & self ) -> bool {
2286
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
2287
+ pub const fn is_empty ( & self ) -> bool {
2283
2288
self . len ( ) == 0
2284
2289
}
2285
2290
@@ -2828,15 +2833,15 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> {
2828
2833
2829
2834
#[ inline]
2830
2835
fn deref ( & self ) -> & [ T ] {
2831
- unsafe { slice :: from_raw_parts ( self . as_ptr ( ) , self . len ) }
2836
+ self . as_slice ( )
2832
2837
}
2833
2838
}
2834
2839
2835
2840
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2836
2841
impl < T , A : Allocator > ops:: DerefMut for Vec < T , A > {
2837
2842
#[ inline]
2838
2843
fn deref_mut ( & mut self ) -> & mut [ T ] {
2839
- unsafe { slice :: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
2844
+ self . as_mut_slice ( )
2840
2845
}
2841
2846
}
2842
2847
0 commit comments