@@ -1789,17 +1789,20 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
17891789 /// #
17901790 /// # fn main() { test().unwrap(); }
17911791 /// # fn test() -> Option<()> {
1792- #[ doc = concat!( "assert_eq!(NonZero::<" , stringify!( $Int ) , ">::new(0b111)?.bit_width(), 3);" ) ]
1793- #[ doc = concat!( "assert_eq!(NonZero::<" , stringify!( $Int ) , ">::new(0b1110)?.bit_width(), 4);" ) ]
1792+ #[ doc = concat!( "assert_eq!(NonZero::<" , stringify!( $Int ) , ">::MIN.bit_width(), NonZero::new(1)?);" ) ]
1793+ #[ doc = concat!( "assert_eq!(NonZero::<" , stringify!( $Int ) , ">::new(0b111)?.bit_width(), NonZero::new(3)?);" ) ]
1794+ #[ doc = concat!( "assert_eq!(NonZero::<" , stringify!( $Int ) , ">::new(0b1110)?.bit_width(), NonZero::new(4)?);" ) ]
17941795 /// # Some(())
17951796 /// # }
17961797 /// ```
17971798 #[ unstable( feature = "uint_bit_width" , issue = "142326" ) ]
17981799 #[ must_use = "this returns the result of the operation, \
17991800 without modifying the original"]
18001801 #[ inline( always) ]
1801- pub const fn bit_width( self ) -> u32 {
1802- Self :: BITS - self . leading_zeros( )
1802+ pub const fn bit_width( self ) -> NonZero <u32 > {
1803+ // SAFETY: Since `self.leading_zeros()` is always less than
1804+ // `Self::BITS`, this subtraction can never be zero.
1805+ unsafe { NonZero :: new_unchecked( Self :: BITS - self . leading_zeros( ) ) }
18031806 }
18041807 } ;
18051808
0 commit comments