A pain point I’ve always had with Rust is the lack of methods to convert between integer types. Yes there’s as
, but as
can fail when downcasting. Yes there’s TryFrom
/TryInto
, but try_from
and try_into
aren’t very descriptive since they don’t include the output type in the name and this makes code harder to read.
Can we just add the following inherent methods to the integer types?:
impl u8 {
fn to_u16(self) -> u16;
fn to_u32(self) -> u32;
fn to_u64(self) -> u64;
fn to_u128(self) -> u128;
fn to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn to_i16(self) -> i16;
fn to_i32(self) -> i32;
fn to_i64(self) -> i64;
fn to_i128(self) -> i128;
fn to_isize(self) -> isize;
}
impl u16 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn to_u32(self) -> u32;
fn to_u64(self) -> u64;
fn to_u128(self) -> u128;
fn to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn to_i32(self) -> i32;
fn to_i64(self) -> i64;
fn to_i128(self) -> i128;
fn to_isize(self) -> isize;
}
impl u32 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn to_u64(self) -> u64;
fn to_u128(self) -> u128;
fn to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn checked_to_i32(self) -> Option<i32>;
fn saturating_to_i32(self) -> i32;
fn wrapping_to_i32(self) -> i32;
fn to_i64(self) -> i64;
fn to_i128(self) -> i128;
fn checked_to_isize(self) -> Option<isize>;
fn saturating_to_isize(self) -> isize;
fn wrapping_to_isize(self) -> isize;
}
impl u64 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn to_u128(self) -> u128;
fn checked_to_usize(self) -> Option<usize>;
fn saturating_to_usize(self) -> usize;
fn wrapping_to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn checked_to_i32(self) -> Option<i32>;
fn saturating_to_i32(self) -> i32;
fn wrapping_to_i32(self) -> i32;
fn checked_to_i64(self) -> Option<i64>;
fn saturating_to_i64(self) -> i64;
fn wrapping_to_i64(self) -> i64;
fn to_i128(self) -> i128;
fn checked_to_isize(self) -> Option<isize>;
fn saturating_to_isize(self) -> isize;
fn wrapping_to_isize(self) -> isize;
}
impl u128 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn checked_to_u64(self) -> Option<u64>;
fn saturating_to_u64(self) -> u64;
fn wrapping_to_u64(self) -> u64;
fn checked_to_usize(self) -> Option<usize>;
fn saturating_to_usize(self) -> usize;
fn wrapping_to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn checked_to_i32(self) -> Option<i32>;
fn saturating_to_i32(self) -> i32;
fn wrapping_to_i32(self) -> i32;
fn checked_to_i64(self) -> Option<i64>;
fn saturating_to_i64(self) -> i64;
fn wrapping_to_i64(self) -> i64;
fn checked_to_i128(self) -> Option<i128>;
fn saturating_to_i128(self) -> i128;
fn wrapping_to_i128(self) -> i128;
fn checked_to_isize(self) -> Option<isize>;
fn saturating_to_isize(self) -> isize;
fn wrapping_to_isize(self) -> isize;
}
impl usize {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn to_u64(self) -> u64;
fn to_u128(self) -> u128;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn checked_to_i32(self) -> Option<i32>;
fn saturating_to_i32(self) -> i32;
fn wrapping_to_i32(self) -> i32;
fn checked_to_i64(self) -> Option<i64>;
fn saturating_to_i64(self) -> i64;
fn wrapping_to_i64(self) -> i64;
fn to_i128(self) -> i128;
fn checked_to_isize(self) -> Option<isize>;
fn saturating_to_isize(self) -> isize;
fn wrapping_to_isize(self) -> isize;
}
impl i8 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn checked_to_u64(self) -> Option<u64>;
fn saturating_to_u64(self) -> u64;
fn wrapping_to_u64(self) -> u64;
fn checked_to_u128(self) -> Option<u128>;
fn saturating_to_u128(self) -> u128;
fn wrapping_to_u128(self) -> u128;
fn checked_to_usize(self) -> Option<usize>;
fn saturating_to_usize(self) -> usize;
fn wrapping_to_usize(self) -> usize;
fn to_i16(self) -> i16;
fn to_i32(self) -> i32;
fn to_i64(self) -> i64;
fn to_i128(self) -> i128;
fn to_isize(self) -> isize
}
impl i16 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn checked_to_u64(self) -> Option<u64>;
fn saturating_to_u64(self) -> u64;
fn wrapping_to_u64(self) -> u64;
fn checked_to_u128(self) -> Option<u128>;
fn saturating_to_u128(self) -> u128;
fn wrapping_to_u128(self) -> u128;
fn checked_to_usize(self) -> Option<usize>;
fn saturating_to_usize(self) -> usize;
fn wrapping_to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn to_i32(self) -> i32;
fn to_i64(self) -> i64;
fn to_i128(self) -> i128;
fn to_isize(self) -> isize
}
impl i32 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn checked_to_u64(self) -> Option<u64>;
fn saturating_to_u64(self) -> u64;
fn wrapping_to_u64(self) -> u64;
fn checked_to_u128(self) -> Option<u128>;
fn saturating_to_u128(self) -> u128;
fn wrapping_to_u128(self) -> u128;
fn checked_to_usize(self) -> Option<usize>;
fn saturating_to_usize(self) -> usize;
fn wrapping_to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn to_i64(self) -> i64;
fn to_i128(self) -> i128;
fn to_isize(self) -> isize
}
impl i64 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn checked_to_u64(self) -> Option<u64>;
fn saturating_to_u64(self) -> u64;
fn wrapping_to_u64(self) -> u64;
fn checked_to_u128(self) -> Option<u128>;
fn saturating_to_u128(self) -> u128;
fn wrapping_to_u128(self) -> u128;
fn checked_to_usize(self) -> Option<usize>;
fn saturating_to_usize(self) -> usize;
fn wrapping_to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn checked_to_i32(self) -> Option<i32>;
fn saturating_to_i32(self) -> i32;
fn wrapping_to_i32(self) -> i32;
fn to_i128(self) -> i128;
fn checked_to_isize(self) -> Option<isize>;
fn saturating_to_isize(self) -> isize;
fn wrapping_to_isize(self) -> isize;
}
impl i128 {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn checked_to_u64(self) -> Option<u64>;
fn saturating_to_u64(self) -> u64;
fn wrapping_to_u64(self) -> u64;
fn checked_to_u128(self) -> Option<u128>;
fn saturating_to_u128(self) -> u128;
fn wrapping_to_u128(self) -> u128;
fn checked_to_usize(self) -> Option<usize>;
fn saturating_to_usize(self) -> usize;
fn wrapping_to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn checked_to_i32(self) -> Option<i32>;
fn saturating_to_i32(self) -> i32;
fn wrapping_to_i32(self) -> i32;
fn checked_to_i64(self) -> Option<i64>;
fn saturating_to_i64(self) -> i64;
fn wrapping_to_i64(self) -> i64;
fn checked_to_isize(self) -> Option<isize>;
fn saturating_to_isize(self) -> isize;
fn wrapping_to_isize(self) -> isize;
}
impl isize {
fn checked_to_u8(self) -> Option<u8>;
fn saturating_to_u8(self) -> u8;
fn wrapping_to_u8(self) -> u8;
fn checked_to_u16(self) -> Option<u16>;
fn saturating_to_u16(self) -> u16;
fn wrapping_to_u16(self) -> u16;
fn checked_to_u32(self) -> Option<u32>;
fn saturating_to_u32(self) -> u32;
fn wrapping_to_u32(self) -> u32;
fn checked_to_u64(self) -> Option<u64>;
fn saturating_to_u64(self) -> u64;
fn wrapping_to_u64(self) -> u64;
fn checked_to_u128(self) -> Option<u128>;
fn saturating_to_u128(self) -> u128;
fn wrapping_to_u128(self) -> u128;
fn checked_to_usize(self) -> Option<usize>;
fn saturating_to_usize(self) -> usize;
fn wrapping_to_usize(self) -> usize;
fn checked_to_i8(self) -> Option<i8>;
fn saturating_to_i8(self) -> i8;
fn wrapping_to_i8(self) -> i8;
fn checked_to_i16(self) -> Option<i16>;
fn saturating_to_i16(self) -> i16;
fn wrapping_to_i16(self) -> i16;
fn checked_to_i32(self) -> Option<i32>;
fn saturating_to_i32(self) -> i32;
fn wrapping_to_i32(self) -> i32;
fn to_i64(self) -> i64;
fn to_i128(self) -> i128;
}