@@ -860,8 +860,15 @@ impl<T> Vec<T> {
860
860
/// This is generally not recommended, use with caution!
861
861
/// Calling this method with an out-of-allocation index is *[undefined behavior]*
862
862
/// even if the resulting reference is not used.
863
+ /// When you asign [`MaybeUninit::uninit()`] to the index in bounds, it is
864
+ /// *[undefined behavior]* to call other methods or drop the vector before using [`set_len`]
865
+ /// to make the index out of bounds.
866
+ /// You can have a memory leak if you forget to use [`set_len`] to make the index in bounds
867
+ /// after asigning value to the reference out of bounds.
863
868
///
864
869
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
870
+ /// [`MaybeUninit::uninit()`]: ../mem/union.MaybeUninit.html#method.uninit
871
+ /// [`set_len`]: #method.set_len
865
872
///
866
873
/// # Examples
867
874
///
@@ -882,12 +889,14 @@ impl<T> Vec<T> {
882
889
/// assert_eq!(&*x, &[0, 1, 2, 3]);
883
890
/// ```
884
891
#[ unstable( feature = "vec_get_uninit_unchecked" , issue = "none" ) ]
885
- pub fn get_uninit_unchecked ( & mut self , index : usize ) -> & mut MaybeUninit < T > {
886
- if cfg ! ( debug_assertions) && index >= self . capacity ( ) {
887
- panic ! ( "Out of allocation access" )
888
- } else {
889
- unsafe { & mut * ( self . as_mut_ptr ( ) . add ( index) as * mut MaybeUninit < T > ) }
890
- }
892
+ pub unsafe fn get_uninit_unchecked ( & mut self , index : usize ) -> & mut MaybeUninit < T > {
893
+ debug_assert ! (
894
+ index < self . capacity( ) ,
895
+ "index out of allocation: the capacity is {} but the index is {}" ,
896
+ self . capacity( ) ,
897
+ index
898
+ ) ;
899
+ & mut * ( self . as_mut_ptr ( ) . add ( index) as * mut MaybeUninit < T > )
891
900
}
892
901
893
902
/// Forces the length of the vector to `new_len`.
0 commit comments