File tree 1 file changed +26
-0
lines changed
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -171,6 +171,32 @@ macro_rules! nonzero_integer {
171
171
}
172
172
}
173
173
174
+ /// Converts a primitive mutable reference to a non-zero mutable reference
175
+ /// if the referenced integer is not zero.
176
+ #[ unstable( feature = "nonzero_from_mut" , issue = "none" ) ]
177
+ #[ must_use]
178
+ #[ inline]
179
+ pub fn from_mut( n: & mut $Int) -> Option <& mut Self > {
180
+ // SAFETY: Self is repr(transparent), and the value is non-zero.
181
+ // As long as the returned reference is alive,
182
+ // the user cannot `*n = 0` directly.
183
+ ( * n != 0 ) . then( || unsafe { & mut * ( n as * mut $Int as * mut Self ) } )
184
+ }
185
+
186
+ /// Converts a primitive mutable reference to a non-zero mutable reference
187
+ /// without checking whether the referenced value is non-zero.
188
+ /// This results in undefined behavior if `*n` is zero.
189
+ ///
190
+ /// # Safety
191
+ /// The referenced value must not be currently zero.
192
+ #[ unstable( feature = "nonzero_from_mut" , issue = "none" ) ]
193
+ #[ must_use]
194
+ #[ inline]
195
+ pub unsafe fn from_mut_unchecked( n: & mut $Int) -> & mut Self {
196
+ // SAFETY: Self is repr(transparent), and the value is assumed to be non-zero.
197
+ unsafe { & mut * ( n as * mut $Int as * mut Self ) }
198
+ }
199
+
174
200
/// Returns the value as a primitive type.
175
201
#[ $stability]
176
202
#[ inline]
You can’t perform that action at this time.
0 commit comments