@@ -1238,7 +1238,8 @@ impl<T, A: Allocator> Vec<T, A> {
12381238 /// ```
12391239 #[ inline]
12401240 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1241- pub fn capacity ( & self ) -> usize {
1241+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1242+ pub const fn capacity ( & self ) -> usize {
12421243 self . buf . capacity ( )
12431244 }
12441245
@@ -1545,8 +1546,9 @@ impl<T, A: Allocator> Vec<T, A> {
15451546 /// ```
15461547 #[ inline]
15471548 #[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
1548- pub fn as_slice ( & self ) -> & [ T ] {
1549- self
1549+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1550+ pub const fn as_slice ( & self ) -> & [ T ] {
1551+ unsafe { slice:: from_raw_parts ( self . as_ptr ( ) , self . len ) }
15501552 }
15511553
15521554 /// Extracts a mutable slice of the entire vector.
@@ -1563,7 +1565,7 @@ impl<T, A: Allocator> Vec<T, A> {
15631565 #[ inline]
15641566 #[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
15651567 pub fn as_mut_slice ( & mut self ) -> & mut [ T ] {
1566- self
1568+ unsafe { slice :: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
15671569 }
15681570
15691571 /// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
@@ -1618,9 +1620,10 @@ impl<T, A: Allocator> Vec<T, A> {
16181620 /// [`as_mut_ptr`]: Vec::as_mut_ptr
16191621 /// [`as_ptr`]: Vec::as_ptr
16201622 #[ stable( feature = "vec_as_ptr" , since = "1.37.0" ) ]
1623+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
16211624 #[ rustc_never_returns_null_ptr]
16221625 #[ inline]
1623- pub fn as_ptr ( & self ) -> * const T {
1626+ pub const fn as_ptr ( & self ) -> * const T {
16241627 // We shadow the slice method of the same name to avoid going through
16251628 // `deref`, which creates an intermediate reference.
16261629 self . buf . ptr ( )
@@ -2556,8 +2559,9 @@ impl<T, A: Allocator> Vec<T, A> {
25562559 /// ```
25572560 #[ inline]
25582561 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2562+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
25592563 #[ rustc_confusables( "length" , "size" ) ]
2560- pub fn len ( & self ) -> usize {
2564+ pub const fn len ( & self ) -> usize {
25612565 self . len
25622566 }
25632567
@@ -2573,7 +2577,8 @@ impl<T, A: Allocator> Vec<T, A> {
25732577 /// assert!(!v.is_empty());
25742578 /// ```
25752579 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2576- pub fn is_empty ( & self ) -> bool {
2580+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
2581+ pub const fn is_empty ( & self ) -> bool {
25772582 self . len ( ) == 0
25782583 }
25792584
@@ -3123,15 +3128,15 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> {
31233128
31243129 #[ inline]
31253130 fn deref ( & self ) -> & [ T ] {
3126- unsafe { slice :: from_raw_parts ( self . as_ptr ( ) , self . len ) }
3131+ self . as_slice ( )
31273132 }
31283133}
31293134
31303135#[ stable( feature = "rust1" , since = "1.0.0" ) ]
31313136impl < T , A : Allocator > ops:: DerefMut for Vec < T , A > {
31323137 #[ inline]
31333138 fn deref_mut ( & mut self ) -> & mut [ T ] {
3134- unsafe { slice :: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
3139+ self . as_mut_slice ( )
31353140 }
31363141}
31373142
0 commit comments