@@ -1240,7 +1240,8 @@ impl<T, A: Allocator> Vec<T, A> {
1240
1240
/// ```
1241
1241
#[ inline]
1242
1242
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1243
- pub fn capacity ( & self ) -> usize {
1243
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1244
+ pub const fn capacity ( & self ) -> usize {
1244
1245
self . buf . capacity ( )
1245
1246
}
1246
1247
@@ -1548,8 +1549,9 @@ impl<T, A: Allocator> Vec<T, A> {
1548
1549
#[ inline]
1549
1550
#[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
1550
1551
#[ cfg_attr( not( test) , rustc_diagnostic_item = "vec_as_slice" ) ]
1551
- pub fn as_slice ( & self ) -> & [ T ] {
1552
- self
1552
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1553
+ pub const fn as_slice ( & self ) -> & [ T ] {
1554
+ unsafe { slice:: from_raw_parts ( self . as_ptr ( ) , self . len ) }
1553
1555
}
1554
1556
1555
1557
/// Extracts a mutable slice of the entire vector.
@@ -1567,7 +1569,7 @@ impl<T, A: Allocator> Vec<T, A> {
1567
1569
#[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
1568
1570
#[ cfg_attr( not( test) , rustc_diagnostic_item = "vec_as_mut_slice" ) ]
1569
1571
pub fn as_mut_slice ( & mut self ) -> & mut [ T ] {
1570
- self
1572
+ unsafe { slice :: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
1571
1573
}
1572
1574
1573
1575
/// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
@@ -1622,9 +1624,10 @@ impl<T, A: Allocator> Vec<T, A> {
1622
1624
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1623
1625
/// [`as_ptr`]: Vec::as_ptr
1624
1626
#[ stable( feature = "vec_as_ptr" , since = "1.37.0" ) ]
1627
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1625
1628
#[ rustc_never_returns_null_ptr]
1626
1629
#[ inline]
1627
- pub fn as_ptr ( & self ) -> * const T {
1630
+ pub const fn as_ptr ( & self ) -> * const T {
1628
1631
// We shadow the slice method of the same name to avoid going through
1629
1632
// `deref`, which creates an intermediate reference.
1630
1633
self . buf . ptr ( )
@@ -2561,8 +2564,9 @@ impl<T, A: Allocator> Vec<T, A> {
2561
2564
/// ```
2562
2565
#[ inline]
2563
2566
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2567
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
2564
2568
#[ rustc_confusables( "length" , "size" ) ]
2565
- pub fn len ( & self ) -> usize {
2569
+ pub const fn len ( & self ) -> usize {
2566
2570
self . len
2567
2571
}
2568
2572
@@ -2579,7 +2583,8 @@ impl<T, A: Allocator> Vec<T, A> {
2579
2583
/// ```
2580
2584
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2581
2585
#[ cfg_attr( not( test) , rustc_diagnostic_item = "vec_is_empty" ) ]
2582
- pub fn is_empty ( & self ) -> bool {
2586
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
2587
+ pub const fn is_empty ( & self ) -> bool {
2583
2588
self . len ( ) == 0
2584
2589
}
2585
2590
@@ -3130,15 +3135,15 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> {
3130
3135
3131
3136
#[ inline]
3132
3137
fn deref ( & self ) -> & [ T ] {
3133
- unsafe { slice :: from_raw_parts ( self . as_ptr ( ) , self . len ) }
3138
+ self . as_slice ( )
3134
3139
}
3135
3140
}
3136
3141
3137
3142
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3138
3143
impl < T , A : Allocator > ops:: DerefMut for Vec < T , A > {
3139
3144
#[ inline]
3140
3145
fn deref_mut ( & mut self ) -> & mut [ T ] {
3141
- unsafe { slice :: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
3146
+ self . as_mut_slice ( )
3142
3147
}
3143
3148
}
3144
3149
0 commit comments