File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -78,6 +78,33 @@ macro_rules! nonzero_integers {
7878 }
7979 }
8080
81+ /// Converts a primitive mutable reference to a non-zero mutable reference
82+ /// if the referenced integer is not zero.
83+ #[ unstable( feature = "nonzero_from_mut" , issue = "none" ) ]
84+ #[ must_use]
85+ #[ inline]
86+ pub fn from_mut( n: & mut $Int) -> Option <& mut Self > {
87+ // SAFETY: Self is repr(transparent), and the value is non-zero.
88+ // As long as the returned reference is alive,
89+ // the user cannot `*n = 0` directly.
90+ ( * n != 0 ) . then( || unsafe { & mut * ( n as * mut $Int as * mut Self ) } )
91+ }
92+
93+ /// Converts a primitive mutable reference to a non-zero mutable reference
94+ /// without checking whether the referenced value is non-zero.
95+ /// This results in undefined behavior if `*n` is zero.
96+ ///
97+ /// # Safety
98+ /// The referenced value must not be currently zero.
99+ #[ unstable( feature = "nonzero_from_mut" , issue = "none" ) ]
100+ #[ must_use]
101+ #[ inline]
102+ pub unsafe fn from_mut_unchecked( n: & mut $Int) -> & mut Self {
103+ // SAFETY: Self is repr(transparent), and the value is assumed to be non-zero.
104+ unsafe { & mut * ( n as * mut $Int as * mut Self ) }
105+ }
106+
107+
81108 /// Returns the value as a primitive type.
82109 #[ $stability]
83110 #[ inline]
You can’t perform that action at this time.
0 commit comments