@@ -1551,6 +1551,19 @@ impl<T, A: Allocator> Vec<T, A> {
1551
1551
#[ cfg_attr( not( test) , rustc_diagnostic_item = "vec_as_slice" ) ]
1552
1552
#[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1553
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.
1554
1567
unsafe { slice:: from_raw_parts ( self . as_ptr ( ) , self . len ) }
1555
1568
}
1556
1569
@@ -1570,6 +1583,19 @@ impl<T, A: Allocator> Vec<T, A> {
1570
1583
#[ cfg_attr( not( test) , rustc_diagnostic_item = "vec_as_mut_slice" ) ]
1571
1584
#[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
1572
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.
1573
1599
unsafe { slice:: from_raw_parts_mut ( self . as_mut_ptr ( ) , self . len ) }
1574
1600
}
1575
1601
0 commit comments