@@ -353,6 +353,98 @@ macro_rules! uint_impl {
353353 self as $SignedT
354354 }
355355
356+ /// Saturating conversion of `self` to a signed integer of the same size.
357+ ///
358+ /// The signed integer's maximum value is returned if `self` is larger
359+ /// than the maximum positive value representable by the signed integer.
360+ ///
361+ /// For other kinds of signed integer casts, see
362+ /// [`cast_signed`](Self::cast_signed),
363+ /// [`checked_cast_signed`](Self::checked_cast_signed),
364+ /// or [`strict_cast_signed`](Self::strict_cast_signed).
365+ ///
366+ /// # Examples
367+ ///
368+ /// ```
369+ /// #![feature(integer_cast_extras)]
370+ #[ doc = concat!( "let n = " , stringify!( $SelfT) , "::MAX;" ) ]
371+ ///
372+ #[ doc = concat!( "assert_eq!(n.saturating_cast_signed(), " , stringify!( $SignedT) , "::MAX);" ) ]
373+ #[ doc = concat!( "assert_eq!(64" , stringify!( $SelfT) , ".saturating_cast_signed(), 64" , stringify!( $SignedT) , ");" ) ]
374+ /// ```
375+ #[ rustc_const_unstable( feature = "integer_cast_extras" , issue = "154650" ) ]
376+ #[ unstable( feature = "integer_cast_extras" , issue = "154650" ) ]
377+ #[ must_use = "this returns the result of the operation, \
378+ without modifying the original"]
379+ #[ inline( always) ]
380+ pub const fn saturating_cast_signed( self ) -> $SignedT {
381+ // Clamp to the signed integer max size, which is ActualT::MAX >> 1.
382+ if self <= <$SignedT>:: MAX . cast_unsigned( ) {
383+ self . cast_signed( )
384+ } else {
385+ <$SignedT>:: MAX
386+ }
387+ }
388+
389+ /// Checked conversion of `self` to a signed integer of the same size,
390+ /// returning `None` if `self` is larger than the signed integer's
391+ /// maximum value.
392+ ///
393+ /// For other kinds of signed integer casts, see
394+ /// [`cast_signed`](Self::cast_signed),
395+ /// [`saturating_cast_signed`](Self::saturating_cast_signed),
396+ /// or [`strict_cast_signed`](Self::strict_cast_signed).
397+ ///
398+ /// # Examples
399+ ///
400+ /// ```
401+ /// #![feature(integer_cast_extras)]
402+ #[ doc = concat!( "let n = " , stringify!( $SelfT) , "::MAX;" ) ]
403+ ///
404+ #[ doc = concat!( "assert_eq!(n.checked_cast_signed(), None);" ) ]
405+ #[ doc = concat!( "assert_eq!(64" , stringify!( $SelfT) , ".checked_cast_signed(), Some(64" , stringify!( $SignedT) , "));" ) ]
406+ /// ```
407+ #[ rustc_const_unstable( feature = "integer_cast_extras" , issue = "154650" ) ]
408+ #[ unstable( feature = "integer_cast_extras" , issue = "154650" ) ]
409+ #[ must_use = "this returns the result of the operation, \
410+ without modifying the original"]
411+ #[ inline( always) ]
412+ pub const fn checked_cast_signed( self ) -> Option <$SignedT> {
413+ if self <= <$SignedT>:: MAX . cast_unsigned( ) {
414+ Some ( self . cast_signed( ) )
415+ } else {
416+ None
417+ }
418+ }
419+
420+ /// Strict conversion of `self` to a signed integer of the same size,
421+ /// which panics if `self` is larger than the signed integer's maximum
422+ /// value.
423+ ///
424+ /// For other kinds of signed integer casts, see
425+ /// [`cast_signed`](Self::cast_signed),
426+ /// [`checked_cast_signed`](Self::checked_cast_signed),
427+ /// or [`saturating_cast_signed`](Self::saturating_cast_signed).
428+ ///
429+ /// # Examples
430+ ///
431+ /// ```should_panic
432+ /// #![feature(integer_cast_extras)]
433+ #[ doc = concat!( "let _ = " , stringify!( $SelfT) , "::MAX.strict_cast_signed();" ) ]
434+ /// ```
435+ #[ rustc_const_unstable( feature = "integer_cast_extras" , issue = "154650" ) ]
436+ #[ unstable( feature = "integer_cast_extras" , issue = "154650" ) ]
437+ #[ must_use = "this returns the result of the operation, \
438+ without modifying the original"]
439+ #[ inline]
440+ #[ track_caller]
441+ pub const fn strict_cast_signed( self ) -> $SignedT {
442+ match self . checked_cast_signed( ) {
443+ Some ( n) => n,
444+ None => imp:: overflow_panic:: cast_integer( ) ,
445+ }
446+ }
447+
356448 /// Shifts the bits to the left by a specified amount, `n`,
357449 /// wrapping the truncated bits to the end of the resulting integer.
358450 ///
0 commit comments