@@ -366,3 +366,84 @@ mod ptr_try_from_impls {
366366 rev ! ( try_from_unbounded, isize , i32 , i64 ) ;
367367 rev ! ( try_from_both_bounded, isize , i128 ) ;
368368}
369+
370+ // Conversion traits for non-zero integer types
371+ use crate :: num:: NonZeroI128 ;
372+ use crate :: num:: NonZeroI16 ;
373+ use crate :: num:: NonZeroI32 ;
374+ use crate :: num:: NonZeroI64 ;
375+ use crate :: num:: NonZeroI8 ;
376+ use crate :: num:: NonZeroIsize ;
377+ use crate :: num:: NonZeroU128 ;
378+ use crate :: num:: NonZeroU16 ;
379+ use crate :: num:: NonZeroU32 ;
380+ use crate :: num:: NonZeroU64 ;
381+ use crate :: num:: NonZeroU8 ;
382+ use crate :: num:: NonZeroUsize ;
383+
384+ macro_rules! nzint_impl_from {
385+ ( $Small: ty, $Large: ty, #[ $attr: meta] , $doc: expr) => {
386+ #[ $attr]
387+ #[ doc = $doc]
388+ impl From <$Small> for $Large {
389+ #[ inline]
390+ fn from( small: $Small) -> $Large {
391+ // SAFETY: input type guarantees the value is non-zero
392+ unsafe {
393+ <$Large>:: new_unchecked( small. get( ) . into( ) )
394+ }
395+ }
396+ }
397+ } ;
398+ ( $Small: ty, $Large: ty, #[ $attr: meta] ) => {
399+ nzint_impl_from!( $Small,
400+ $Large,
401+ #[ $attr] ,
402+ concat!( "Converts `" ,
403+ stringify!( $Small) ,
404+ "` to `" ,
405+ stringify!( $Large) ,
406+ "` losslessly." ) ) ;
407+ }
408+ }
409+
410+ // Non-zero Unsigned -> Non-zero Unsigned
411+ nzint_impl_from ! { NonZeroU8 , NonZeroU16 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
412+ nzint_impl_from ! { NonZeroU8 , NonZeroU32 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
413+ nzint_impl_from ! { NonZeroU8 , NonZeroU64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
414+ nzint_impl_from ! { NonZeroU8 , NonZeroU128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
415+ nzint_impl_from ! { NonZeroU8 , NonZeroUsize , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
416+ nzint_impl_from ! { NonZeroU16 , NonZeroU32 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
417+ nzint_impl_from ! { NonZeroU16 , NonZeroU64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
418+ nzint_impl_from ! { NonZeroU16 , NonZeroU128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
419+ nzint_impl_from ! { NonZeroU16 , NonZeroUsize , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
420+ nzint_impl_from ! { NonZeroU32 , NonZeroU64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
421+ nzint_impl_from ! { NonZeroU32 , NonZeroU128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
422+ nzint_impl_from ! { NonZeroU64 , NonZeroU128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
423+
424+ // Non-zero Signed -> Non-zero Signed
425+ nzint_impl_from ! { NonZeroI8 , NonZeroI16 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
426+ nzint_impl_from ! { NonZeroI8 , NonZeroI32 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
427+ nzint_impl_from ! { NonZeroI8 , NonZeroI64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
428+ nzint_impl_from ! { NonZeroI8 , NonZeroI128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
429+ nzint_impl_from ! { NonZeroI8 , NonZeroIsize , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
430+ nzint_impl_from ! { NonZeroI16 , NonZeroI32 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
431+ nzint_impl_from ! { NonZeroI16 , NonZeroI64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
432+ nzint_impl_from ! { NonZeroI16 , NonZeroI128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
433+ nzint_impl_from ! { NonZeroI16 , NonZeroIsize , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
434+ nzint_impl_from ! { NonZeroI32 , NonZeroI64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
435+ nzint_impl_from ! { NonZeroI32 , NonZeroI128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
436+ nzint_impl_from ! { NonZeroI64 , NonZeroI128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
437+
438+ // NonZero UnSigned -> Non-zero Signed
439+ nzint_impl_from ! { NonZeroU8 , NonZeroI16 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
440+ nzint_impl_from ! { NonZeroU8 , NonZeroI32 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
441+ nzint_impl_from ! { NonZeroU8 , NonZeroI64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
442+ nzint_impl_from ! { NonZeroU8 , NonZeroI128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
443+ nzint_impl_from ! { NonZeroU8 , NonZeroIsize , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
444+ nzint_impl_from ! { NonZeroU16 , NonZeroI32 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
445+ nzint_impl_from ! { NonZeroU16 , NonZeroI64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
446+ nzint_impl_from ! { NonZeroU16 , NonZeroI128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
447+ nzint_impl_from ! { NonZeroU32 , NonZeroI64 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
448+ nzint_impl_from ! { NonZeroU32 , NonZeroI128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
449+ nzint_impl_from ! { NonZeroU64 , NonZeroI128 , #[ stable( feature = "nz_int_conv" , since = "1.41.0" ) ] }
0 commit comments