You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a tracking issue for the addition of saturating, wrapping, checked, unchecked, and strict casts between integer types.
Public API
For each built-in integer type T the following methods are added:
implT{/// Converts `self` to the target integer type, saturating at the nearest edge/// of the target type's domain if the value does not lie in within it.fnsaturating_cast<T:BoundedCastFromInt<Self>>(self) -> T;/// Converts `self` to the target integer type, wrapping around at the/// boundary of the target type.fnwrapping_cast<T:BoundedCastFromInt<Self>>(self) -> T;/// Converts `self` to the target integer type, returning `None` if the value/// does not lie in the target type's domain.fnchecked_cast<T:CheckedCastFromInt<Self>>(self) -> Option<T>;/// Equivalent to `self.checked_cast::<Int>().unwrap()`.fnstrict_cast<T:CheckedCastFromInt<Self>>(self) -> T;/// Equivalent to `self.checked_cast::<Int>().unwrap_unchecked()`.unsafefnunchecked_cast<T:CheckedCastFromInt<Self>>(self) -> T;}
The BoundedCastFromInt trait would need to be split into a saturating and wrapping variants for NonZero<uint> to ever be supported by the saturating cast since you can't implement the wrapping cast for it (except for converting between signed and unsigned nonzeros of the same size but cast_signed and cast_unsigned already exist).
@Apersoma I would never implement a wrapping variant for NonZero as it's unclear what it means. Saturating somewhat makes sense for NonZero<uint> but makes no sense for NonZero<int>.
Personally I think it's better if we implement neither operation with NonZero as the output type.
NonZero is fine as source type for BoundedCastFromInt, and always fine for CheckedCastFromInt both as source and target.
asking if we can extend support to booleans so we dont still need to do u8::from(val).strict_cast::<i32>() instead of val as i32, so we just have val.strict_cast::<i32>() where val is a bool
dont know why i didn't think we had this im silly, still feel like for consistency it would be nice to have the applicable cast functions for bool but idk.
Activity
orlp commentedon Jun 3, 2026
I can't assign myself but I will try to port the implementation from the
int-castcrate to std this week.Rollup merge of rust-lang#157402 - orlp:int-cast, r=jhpratt
Rollup merge of rust-lang#157402 - orlp:int-cast, r=jhpratt
Auto merge of #157402 - orlp:int-cast, r=jhpratt
Auto merge of #157402 - orlp:int-cast, r=jhpratt
Auto merge of #157402 - orlp:int-cast, r=jhpratt
Rollup merge of rust-lang#157402 - orlp:int-cast, r=jhpratt
Unrolled build for #157402
Apersoma commentedon Jun 9, 2026
The
BoundedCastFromInttrait would need to be split into a saturating and wrapping variants forNonZero<uint>to ever be supported by the saturating cast since you can't implement the wrapping cast for it (except for converting between signed and unsigned nonzeros of the same size butcast_signedandcast_unsignedalready exist).orlp commentedon Jun 9, 2026
@Apersoma I would never implement a wrapping variant for
NonZeroas it's unclear what it means. Saturating somewhat makes sense forNonZero<uint>but makes no sense forNonZero<int>.Personally I think it's better if we implement neither operation with
NonZeroas the output type.NonZerois fine as source type forBoundedCastFromInt, and always fine forCheckedCastFromIntboth as source and target.bgkillas commentedon Jun 11, 2026
asking if we can extend support to booleans so we dont still need to do
u8::from(val).strict_cast::<i32>()instead ofval as i32, so we just haveval.strict_cast::<i32>()wherevalis a boolorlp commentedon Jun 11, 2026
@bgkillas
i32::from(val)?bgkillas commentedon Jun 11, 2026
dont know why i didn't think we had this im silly, still feel like for consistency it would be nice to have the applicable cast functions for bool but idk.
Rollup merge of #157402 - orlp:int-cast, r=jhpratt
orlp commentedon Jun 11, 2026
Rollup merge of #157402 - orlp:int-cast, r=jhpratt