@@ -17,7 +17,7 @@ pub struct PeekMut<
1717 T ,
1818 #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1919> {
20- vec : & ' a mut Vec < T , A > ,
20+ pub ( super ) vec : & ' a mut Vec < T , A > ,
2121}
2222
2323#[ unstable( feature = "vec_peek_mut" , issue = "122742" ) ]
@@ -28,10 +28,6 @@ impl<T: fmt::Debug> fmt::Debug for PeekMut<'_, T> {
2828}
2929
3030impl < ' a , T > PeekMut < ' a , T > {
31- pub ( crate ) fn new ( vec : & ' a mut Vec < T > ) -> Option < Self > {
32- if vec. is_empty ( ) { None } else { Some ( Self { vec } ) }
33- }
34-
3531 /// Removes the peeked value from the vector and returns it.
3632 #[ unstable( feature = "vec_peek_mut" , issue = "122742" ) ]
3733 pub fn pop ( this : Self ) -> T {
@@ -45,8 +41,9 @@ impl<'a, T> Deref for PeekMut<'a, T> {
4541 type Target = T ;
4642
4743 fn deref ( & self ) -> & Self :: Target {
44+ let idx = self . vec . len ( ) - 1 ;
4845 // SAFETY: PeekMut is only constructed if the vec is non-empty
49- unsafe { self . vec . get_unchecked ( self . vec . len ( ) - 1 ) }
46+ unsafe { self . vec . get_unchecked ( idx ) }
5047 }
5148}
5249
0 commit comments