@@ -1238,7 +1238,8 @@ impl<T, A: Allocator> Vec<T, A> {
1238
1238
/// ```
1239
1239
#[ inline]
1240
1240
#[ 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 {
1242
1243
self . buf . capacity ( )
1243
1244
}
1244
1245
@@ -1545,8 +1546,9 @@ impl<T, A: Allocator> Vec<T, A> {
1545
1546
/// ```
1546
1547
#[ inline]
1547
1548
#[ 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 ) }
1550
1552
}
1551
1553
1552
1554
/// Extracts a mutable slice of the entire vector.
@@ -1563,7 +1565,7 @@ impl<T, A: Allocator> Vec<T, A> {
1563
1565
#[ inline]
1564
1566
#[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
1565
1567
pub fn as_mut_slice ( & mut self ) -> & mut [ T ] {
1566
- self
1568
+ unsafe { slice :: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
1567
1569
}
1568
1570
1569
1571
/// 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> {
1618
1620
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1619
1621
/// [`as_ptr`]: Vec::as_ptr
1620
1622
#[ stable( feature = "vec_as_ptr" , since = "1.37.0" ) ]
1623
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1621
1624
#[ rustc_never_returns_null_ptr]
1622
1625
#[ inline]
1623
- pub fn as_ptr ( & self ) -> * const T {
1626
+ pub const fn as_ptr ( & self ) -> * const T {
1624
1627
// We shadow the slice method of the same name to avoid going through
1625
1628
// `deref`, which creates an intermediate reference.
1626
1629
self . buf . ptr ( )
@@ -2556,8 +2559,9 @@ impl<T, A: Allocator> Vec<T, A> {
2556
2559
/// ```
2557
2560
#[ inline]
2558
2561
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2562
+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
2559
2563
#[ rustc_confusables( "length" , "size" ) ]
2560
- pub fn len ( & self ) -> usize {
2564
+ pub const fn len ( & self ) -> usize {
2561
2565
self . len
2562
2566
}
2563
2567
@@ -2573,7 +2577,8 @@ impl<T, A: Allocator> Vec<T, A> {
2573
2577
/// assert!(!v.is_empty());
2574
2578
/// ```
2575
2579
#[ 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 {
2577
2582
self . len ( ) == 0
2578
2583
}
2579
2584
@@ -3123,15 +3128,15 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> {
3123
3128
3124
3129
#[ inline]
3125
3130
fn deref ( & self ) -> & [ T ] {
3126
- unsafe { slice :: from_raw_parts ( self . as_ptr ( ) , self . len ) }
3131
+ self . as_slice ( )
3127
3132
}
3128
3133
}
3129
3134
3130
3135
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3131
3136
impl < T , A : Allocator > ops:: DerefMut for Vec < T , A > {
3132
3137
#[ inline]
3133
3138
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 ( )
3135
3140
}
3136
3141
}
3137
3142
0 commit comments