133
133
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
134
134
//! [`i32`]: ../../std/primitive.i32.html
135
135
136
- // ignore-tidy-undocumented-unsafe
137
-
138
136
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
139
137
140
138
use crate :: iter:: { FromIterator , FusedIterator , TrustedLen } ;
@@ -301,6 +299,8 @@ impl<T> Option<T> {
301
299
#[ inline]
302
300
#[ stable( feature = "pin" , since = "1.33.0" ) ]
303
301
pub fn as_pin_ref ( self : Pin < & Self > ) -> Option < Pin < & T > > {
302
+ // SAFETY: `x` is guaranteed to be pinned because it comes from `self`
303
+ // which is pinned.
304
304
unsafe { Pin :: get_ref ( self ) . as_ref ( ) . map ( |x| Pin :: new_unchecked ( x) ) }
305
305
}
306
306
@@ -310,6 +310,8 @@ impl<T> Option<T> {
310
310
#[ inline]
311
311
#[ stable( feature = "pin" , since = "1.33.0" ) ]
312
312
pub fn as_pin_mut ( self : Pin < & mut Self > ) -> Option < Pin < & mut T > > {
313
+ // SAFETY: `get_unchecked_mut` is never used to move the `Option` inside `self`.
314
+ // `x` is guaranteed to be pinned because it comes from `self` which is pinned.
313
315
unsafe { Pin :: get_unchecked_mut ( self ) . as_mut ( ) . map ( |x| Pin :: new_unchecked ( x) ) }
314
316
}
315
317
@@ -858,6 +860,8 @@ impl<T> Option<T> {
858
860
859
861
match * self {
860
862
Some ( ref mut v) => v,
863
+ // SAFETY: a `None` variant for `self` would have been replaced by a `Some`
864
+ // variant in the code above.
861
865
None => unsafe { hint:: unreachable_unchecked ( ) } ,
862
866
}
863
867
}
0 commit comments