@@ -1240,7 +1240,8 @@ impl<T, A: Allocator> Vec<T, A> {
12401240 /// ```
12411241 #[ inline]
12421242 #[ 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 {
12441245 self . buf . capacity ( )
12451246 }
12461247
@@ -1548,8 +1549,22 @@ impl<T, A: Allocator> Vec<T, A> {
15481549 #[ inline]
15491550 #[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
15501551 #[ 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+ // SAFETY: `slice::from_raw_parts` requires pointee is a contiguous, aligned buffer of size
1555+ // `len` containing properly-initialized `T`s. Data must not be mutated for the returned
1556+ // lifetime. Further, `len * mem::size_of::<T>` <= `ISIZE::MAX`, and allocation does not
1557+ // "wrap" through overflowing memory addresses.
1558+ //
1559+ // * Vec API guarantees that self.buf:
1560+ // * contains only properly-initialized items within 0..len
1561+ // * is aligned, contiguous, and valid for `len` reads
1562+ // * obeys size and address-wrapping constraints
1563+ //
1564+ // * We only construct `&mut` references to `self.buf` through `&mut self` methods; borrow-
1565+ // check ensures that it is not possible to mutably alias `self.buf` within the
1566+ // returned lifetime.
1567+ unsafe { slice:: from_raw_parts ( self . as_ptr ( ) , self . len ) }
15531568 }
15541569
15551570 /// Extracts a mutable slice of the entire vector.
@@ -1566,8 +1581,22 @@ impl<T, A: Allocator> Vec<T, A> {
15661581 #[ inline]
15671582 #[ stable( feature = "vec_as_slice" , since = "1.7.0" ) ]
15681583 #[ cfg_attr( not( test) , rustc_diagnostic_item = "vec_as_mut_slice" ) ]
1569- pub fn as_mut_slice ( & mut self ) -> & mut [ T ] {
1570- self
1584+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1585+ pub const fn as_mut_slice ( & mut self ) -> & mut [ T ] {
1586+ // SAFETY: `slice::from_raw_parts_mut` requires pointee is a contiguous, aligned buffer of
1587+ // size `len` containing properly-initialized `T`s. Data must not be accessed through any
1588+ // other pointer for the returned lifetime. Further, `len * mem::size_of::<T>` <=
1589+ // `ISIZE::MAX` and allocation does not "wrap" through overflowing memory addresses.
1590+ //
1591+ // * Vec API guarantees that self.buf:
1592+ // * contains only properly-initialized items within 0..len
1593+ // * is aligned, contiguous, and valid for `len` reads
1594+ // * obeys size and address-wrapping constraints
1595+ //
1596+ // * We only construct references to `self.buf` through `&self` and `&mut self` methods;
1597+ // borrow-check ensures that it is not possible to construct a reference to `self.buf`
1598+ // within the returned lifetime.
1599+ unsafe { slice:: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
15711600 }
15721601
15731602 /// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
@@ -1622,9 +1651,10 @@ impl<T, A: Allocator> Vec<T, A> {
16221651 /// [`as_mut_ptr`]: Vec::as_mut_ptr
16231652 /// [`as_ptr`]: Vec::as_ptr
16241653 #[ stable( feature = "vec_as_ptr" , since = "1.37.0" ) ]
1654+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
16251655 #[ rustc_never_returns_null_ptr]
16261656 #[ inline]
1627- pub fn as_ptr ( & self ) -> * const T {
1657+ pub const fn as_ptr ( & self ) -> * const T {
16281658 // We shadow the slice method of the same name to avoid going through
16291659 // `deref`, which creates an intermediate reference.
16301660 self . buf . ptr ( )
@@ -1681,9 +1711,10 @@ impl<T, A: Allocator> Vec<T, A> {
16811711 /// [`as_mut_ptr`]: Vec::as_mut_ptr
16821712 /// [`as_ptr`]: Vec::as_ptr
16831713 #[ stable( feature = "vec_as_ptr" , since = "1.37.0" ) ]
1714+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
16841715 #[ rustc_never_returns_null_ptr]
16851716 #[ inline]
1686- pub fn as_mut_ptr ( & mut self ) -> * mut T {
1717+ pub const fn as_mut_ptr ( & mut self ) -> * mut T {
16871718 // We shadow the slice method of the same name to avoid going through
16881719 // `deref_mut`, which creates an intermediate reference.
16891720 self . buf . ptr ( )
@@ -2561,8 +2592,9 @@ impl<T, A: Allocator> Vec<T, A> {
25612592 /// ```
25622593 #[ inline]
25632594 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2595+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
25642596 #[ rustc_confusables( "length" , "size" ) ]
2565- pub fn len ( & self ) -> usize {
2597+ pub const fn len ( & self ) -> usize {
25662598 self . len
25672599 }
25682600
@@ -2579,7 +2611,8 @@ impl<T, A: Allocator> Vec<T, A> {
25792611 /// ```
25802612 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
25812613 #[ cfg_attr( not( test) , rustc_diagnostic_item = "vec_is_empty" ) ]
2582- pub fn is_empty ( & self ) -> bool {
2614+ #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
2615+ pub const fn is_empty ( & self ) -> bool {
25832616 self . len ( ) == 0
25842617 }
25852618
@@ -3130,15 +3163,15 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> {
31303163
31313164 #[ inline]
31323165 fn deref ( & self ) -> & [ T ] {
3133- unsafe { slice :: from_raw_parts ( self . as_ptr ( ) , self . len ) }
3166+ self . as_slice ( )
31343167 }
31353168}
31363169
31373170#[ stable( feature = "rust1" , since = "1.0.0" ) ]
31383171impl < T , A : Allocator > ops:: DerefMut for Vec < T , A > {
31393172 #[ inline]
31403173 fn deref_mut ( & mut self ) -> & mut [ T ] {
3141- unsafe { slice :: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
3174+ self . as_mut_slice ( )
31423175 }
31433176}
31443177
0 commit comments