@@ -3944,5 +3944,89 @@ macro_rules! int_impl {
39443944 self
39453945 }
39463946 }
3947+
3948+ /// Truncate an integer to an integer of the same size or smaller, preserving the least
3949+ /// significant bits.
3950+ ///
3951+ /// # Examples
3952+ ///
3953+ /// ```
3954+ /// #![feature(integer_extend_truncate)]
3955+ #[ doc = concat!( "assert_eq!(120i8, 120" , stringify!( $SelfT) , ".truncate());" ) ]
3956+ #[ doc = concat!( "assert_eq!(-120i8, (-120" , stringify!( $SelfT) , ").truncate());" ) ]
3957+ /// assert_eq!(120i8, 376i32.truncate());
3958+ /// ```
3959+ #[ must_use = "this returns the truncated value and does not modify the original" ]
3960+ #[ unstable( feature = "integer_extend_truncate" , issue = "154330" ) ]
3961+ #[ rustc_const_unstable( feature = "integer_truncate_extend" , issue = "154330" ) ]
3962+ #[ inline]
3963+ pub const fn truncate<Target >( self ) -> Target
3964+ where Self : [ const ] traits:: TruncateTarget <Target >
3965+ {
3966+ traits:: TruncateTarget :: internal_truncate( self )
3967+ }
3968+
3969+ /// Truncate an integer to an integer of the same size or smaller, saturating at numeric bounds
3970+ /// instead of truncating.
3971+ ///
3972+ /// # Examples
3973+ ///
3974+ /// ```
3975+ /// #![feature(integer_extend_truncate)]
3976+ #[ doc = concat!( "assert_eq!(120i8, 120" , stringify!( $SelfT) , ".saturating_truncate());" ) ]
3977+ #[ doc = concat!( "assert_eq!(-120i8, (-120" , stringify!( $SelfT) , ").saturating_truncate());" ) ]
3978+ /// assert_eq!(127i8, 376i32.saturating_truncate());
3979+ /// assert_eq!(-128i8, (-1000i32).saturating_truncate());
3980+ /// ```
3981+ #[ must_use = "this returns the truncated value and does not modify the original" ]
3982+ #[ unstable( feature = "integer_extend_truncate" , issue = "154330" ) ]
3983+ #[ rustc_const_unstable( feature = "integer_truncate_extend" , issue = "154330" ) ]
3984+ #[ inline]
3985+ pub const fn saturating_truncate<Target >( self ) -> Target
3986+ where Self : [ const ] traits:: TruncateTarget <Target >
3987+ {
3988+ traits:: TruncateTarget :: internal_saturating_truncate( self )
3989+ }
3990+
3991+ /// Truncate an integer to an integer of the same size or smaller, returning `None` if the value
3992+ /// is outside the bounds of the smaller type.
3993+ ///
3994+ /// # Examples
3995+ ///
3996+ /// ```
3997+ /// #![feature(integer_extend_truncate)]
3998+ #[ doc = concat!( "assert_eq!(Some(120i8), 120" , stringify!( $SelfT) , ".checked_truncate());" ) ]
3999+ #[ doc = concat!( "assert_eq!(Some(-120i8), (-120" , stringify!( $SelfT) , ").checked_truncate());" ) ]
4000+ /// assert_eq!(None, 376i32.checked_truncate::<i8>());
4001+ /// assert_eq!(None, (-1000i32).checked_truncate::<i8>());
4002+ /// ```
4003+ #[ must_use = "this returns the truncated value and does not modify the original" ]
4004+ #[ unstable( feature = "integer_extend_truncate" , issue = "154330" ) ]
4005+ #[ rustc_const_unstable( feature = "integer_truncate_extend" , issue = "154330" ) ]
4006+ #[ inline]
4007+ pub const fn checked_truncate<Target >( self ) -> Option <Target >
4008+ where Self : [ const ] traits:: TruncateTarget <Target >
4009+ {
4010+ traits:: TruncateTarget :: internal_checked_truncate( self )
4011+ }
4012+
4013+ /// Extend to an integer of the same size or larger, preserving its value.
4014+ ///
4015+ /// # Examples
4016+ ///
4017+ /// ```
4018+ /// #![feature(integer_extend_truncate)]
4019+ #[ doc = concat!( "assert_eq!(120i128, 120i8.extend());" ) ]
4020+ #[ doc = concat!( "assert_eq!(-120i128, (-120i8).extend());" ) ]
4021+ /// ```
4022+ #[ must_use = "this returns the extended value and does not modify the original" ]
4023+ #[ unstable( feature = "integer_extend_truncate" , issue = "154330" ) ]
4024+ #[ rustc_const_unstable( feature = "integer_truncate_extend" , issue = "154330" ) ]
4025+ #[ inline]
4026+ pub const fn extend<Target >( self ) -> Target
4027+ where Self : [ const ] traits:: ExtendTarget <Target >
4028+ {
4029+ traits:: ExtendTarget :: internal_extend( self )
4030+ }
39474031 }
39484032}
0 commit comments