@@ -78,18 +78,6 @@ macro_rules! nonzero_integers {
78
78
}
79
79
}
80
80
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
81
/// Converts a primitive mutable reference to a non-zero mutable reference
94
82
/// without checking whether the referenced value is non-zero.
95
83
/// This results in undefined behavior if `*n` is zero.
@@ -101,7 +89,26 @@ macro_rules! nonzero_integers {
101
89
#[ inline]
102
90
pub unsafe fn from_mut_unchecked( n: & mut $Int) -> & mut Self {
103
91
// SAFETY: Self is repr(transparent), and the value is assumed to be non-zero.
104
- unsafe { & mut * ( n as * mut $Int as * mut Self ) }
92
+ unsafe {
93
+ let n_alias = & mut * n;
94
+ core:: intrinsics:: assert_unsafe_precondition!(
95
+ concat!( stringify!( $Ty) , "::from_mut_unchecked requires the argument to dereference as non-zero" ) ,
96
+ ( n_alias: & mut $Int) => * n_alias != 0
97
+ ) ;
98
+ & mut * ( n as * mut $Int as * mut Self )
99
+ }
100
+ }
101
+
102
+ /// Converts a primitive mutable reference to a non-zero mutable reference
103
+ /// if the referenced integer is not zero.
104
+ #[ unstable( feature = "nonzero_from_mut" , issue = "none" ) ]
105
+ #[ must_use]
106
+ #[ inline]
107
+ pub fn from_mut( n: & mut $Int) -> Option <& mut Self > {
108
+ // SAFETY: Self is repr(transparent), and the value is non-zero.
109
+ // As long as the returned reference is alive,
110
+ // the user cannot `*n = 0` directly.
111
+ ( * n != 0 ) . then( || unsafe { & mut * ( n as * mut $Int as * mut Self ) } )
105
112
}
106
113
107
114
0 commit comments