num_conv/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
//! `num_conv` is a crate to convert between integer types without using `as` casts. This provides
//! better certainty when refactoring, makes the exact behavior of code more explicit, and allows
//! using turbofish syntax.
#![no_std]
/// Anonymously import all extension traits.
///
/// This allows you to use the methods without worrying about polluting the namespace or importing
/// them individually.
///
/// ```rust
/// use num_conv::prelude::*;
/// ```
pub mod prelude {
pub use crate::{CastSigned as _, CastUnsigned as _, Extend as _, Truncate as _};
}
mod sealed {
pub trait Integer {}
macro_rules! impl_integer {
($($t:ty)*) => {$(
impl Integer for $t {}
)*};
}
impl_integer! {
u8 u16 u32 u64 u128 usize
i8 i16 i32 i64 i128 isize
}
pub trait ExtendTargetSealed<T> {
fn extend(self) -> T;
}
pub trait TruncateTargetSealed<T> {
fn truncate(self) -> T;
}
}
/// Cast to a signed integer of the same size.
///
/// This trait is implemented for all integers. Unsigned to signed casts are equivalent to
/// `0.wrapping_add_signed(value)`, while signed to signed casts are an identity conversion.
///
/// ```rust
/// # use num_conv::CastSigned;
/// assert_eq!(u8::MAX.cast_signed(), -1_i8);
/// assert_eq!(u16::MAX.cast_signed(), -1_i16);
/// assert_eq!(u32::MAX.cast_signed(), -1_i32);
/// assert_eq!(u64::MAX.cast_signed(), -1_i64);
/// assert_eq!(u128::MAX.cast_signed(), -1_i128);
/// assert_eq!(usize::MAX.cast_signed(), -1_isize);
/// ```
///
/// ```rust
/// # use num_conv::CastSigned;
/// assert_eq!(0_i8.cast_signed(), 0_i8);
/// assert_eq!(0_i16.cast_signed(), 0_i16);
/// assert_eq!(0_i32.cast_signed(), 0_i32);
/// assert_eq!(0_i64.cast_signed(), 0_i64);
/// assert_eq!(0_i128.cast_signed(), 0_i128);
/// assert_eq!(0_isize.cast_signed(), 0_isize);
/// ```
pub trait CastSigned: sealed::Integer {
/// The signed integer type with the same size as `Self`.
type Signed;
/// Cast an integer to the signed integer of the same size.
fn cast_signed(self) -> Self::Signed;
}
/// Cast to an unsigned integer of the same size.
///
/// This trait is implemented for all integers. Signed to unsigned casts are equivalent to
/// `0.wrapping_add_unsigned(value)`, while unsigned to unsigned casts are an identity conversion.
///
/// ```rust
/// # use num_conv::CastUnsigned;
/// assert_eq!((-1_i8).cast_unsigned(), u8::MAX);
/// assert_eq!((-1_i16).cast_unsigned(), u16::MAX);
/// assert_eq!((-1_i32).cast_unsigned(), u32::MAX);
/// assert_eq!((-1_i64).cast_unsigned(), u64::MAX);
/// assert_eq!((-1_i128).cast_unsigned(), u128::MAX);
/// assert_eq!((-1_isize).cast_unsigned(), usize::MAX);
/// ```
///
/// ```rust
/// # use num_conv::CastUnsigned;
/// assert_eq!(0_u8.cast_unsigned(), 0_u8);
/// assert_eq!(0_u16.cast_unsigned(), 0_u16);
/// assert_eq!(0_u32.cast_unsigned(), 0_u32);
/// assert_eq!(0_u64.cast_unsigned(), 0_u64);
/// assert_eq!(0_u128.cast_unsigned(), 0_u128);
/// assert_eq!(0_usize.cast_unsigned(), 0_usize);
/// ```
pub trait CastUnsigned: sealed::Integer {
/// The unsigned integer type with the same size as `Self`.
type Unsigned;
/// Cast an integer to the unsigned integer of the same size.
fn cast_unsigned(self) -> Self::Unsigned;
}
/// A type that can be used with turbofish syntax in [`Extend::extend`].
///
/// It is unlikely that you will want to use this trait directly. You are probably looking for the
/// [`Extend`] trait.
pub trait ExtendTarget<T>: sealed::ExtendTargetSealed<T> {}
/// A type that can be used with turbofish syntax in [`Truncate::truncate`].
///
/// It is unlikely that you will want to use this trait directly. You are probably looking for the
/// [`Truncate`] trait.
pub trait TruncateTarget<T>: sealed::TruncateTargetSealed<T> {}
/// Extend to an integer of the same size or larger, preserving its value.
///
/// ```rust
/// # use num_conv::Extend;
/// assert_eq!(0_u8.extend::<u16>(), 0_u16);
/// assert_eq!(0_u16.extend::<u32>(), 0_u32);
/// assert_eq!(0_u32.extend::<u64>(), 0_u64);
/// assert_eq!(0_u64.extend::<u128>(), 0_u128);
/// ```
///
/// ```rust
/// # use num_conv::Extend;
/// assert_eq!((-1_i8).extend::<i16>(), -1_i16);
/// assert_eq!((-1_i16).extend::<i32>(), -1_i32);
/// assert_eq!((-1_i32).extend::<i64>(), -1_i64);
/// assert_eq!((-1_i64).extend::<i128>(), -1_i128);
/// ```
pub trait Extend: sealed::Integer {
/// Extend an integer to an integer of the same size or larger, preserving its value.
fn extend<T>(self) -> T
where
Self: ExtendTarget<T>;
}
impl<T: sealed::Integer> Extend for T {
fn extend<U>(self) -> U
where
T: ExtendTarget<U>,
{
sealed::ExtendTargetSealed::extend(self)
}
}
/// Truncate to an integer of the same size or smaller, preserving the least significant bits.
///
/// ```rust
/// # use num_conv::Truncate;
/// assert_eq!(u16::MAX.truncate::<u8>(), u8::MAX);
/// assert_eq!(u32::MAX.truncate::<u16>(), u16::MAX);
/// assert_eq!(u64::MAX.truncate::<u32>(), u32::MAX);
/// assert_eq!(u128::MAX.truncate::<u64>(), u64::MAX);
/// ```
///
/// ```rust
/// # use num_conv::Truncate;
/// assert_eq!((-1_i16).truncate::<i8>(), -1_i8);
/// assert_eq!((-1_i32).truncate::<i16>(), -1_i16);
/// assert_eq!((-1_i64).truncate::<i32>(), -1_i32);
/// assert_eq!((-1_i128).truncate::<i64>(), -1_i64);
/// ```
pub trait Truncate: sealed::Integer {
/// Truncate an integer to an integer of the same size or smaller, preserving the least
/// significant bits.
fn truncate<T>(self) -> T
where
Self: TruncateTarget<T>;
}
impl<T: sealed::Integer> Truncate for T {
fn truncate<U>(self) -> U
where
T: TruncateTarget<U>,
{
sealed::TruncateTargetSealed::truncate(self)
}
}
macro_rules! impl_cast_signed {
($($($from:ty),+ => $to:ty;)*) => {$($(
const _: () = assert!(
core::mem::size_of::<$from>() == core::mem::size_of::<$to>(),
concat!(
"cannot cast ",
stringify!($from),
" to ",
stringify!($to),
" because they are different sizes"
)
);
impl CastSigned for $from {
type Signed = $to;
fn cast_signed(self) -> Self::Signed {
self as _
}
}
)+)*};
}
macro_rules! impl_cast_unsigned {
($($($from:ty),+ => $to:ty;)*) => {$($(
const _: () = assert!(
core::mem::size_of::<$from>() == core::mem::size_of::<$to>(),
concat!(
"cannot cast ",
stringify!($from),
" to ",
stringify!($to),
" because they are different sizes"
)
);
impl CastUnsigned for $from {
type Unsigned = $to;
fn cast_unsigned(self) -> Self::Unsigned {
self as _
}
}
)+)*};
}
macro_rules! impl_extend {
($($from:ty => $($to:ty),+;)*) => {$($(
const _: () = assert!(
core::mem::size_of::<$from>() <= core::mem::size_of::<$to>(),
concat!(
"cannot extend ",
stringify!($from),
" to ",
stringify!($to),
" because ",
stringify!($from),
" is larger than ",
stringify!($to)
)
);
impl sealed::ExtendTargetSealed<$to> for $from {
fn extend(self) -> $to {
self as _
}
}
impl ExtendTarget<$to> for $from {}
)+)*};
}
macro_rules! impl_truncate {
($($($from:ty),+ => $to:ty;)*) => {$($(
const _: () = assert!(
core::mem::size_of::<$from>() >= core::mem::size_of::<$to>(),
concat!(
"cannot truncate ",
stringify!($from),
" to ",
stringify!($to),
" because ",
stringify!($from),
" is smaller than ",
stringify!($to)
)
);
impl sealed::TruncateTargetSealed<$to> for $from {
fn truncate(self) -> $to {
self as _
}
}
impl TruncateTarget<$to> for $from {}
)+)*};
}
impl_cast_signed! {
u8, i8 => i8;
u16, i16 => i16;
u32, i32 => i32;
u64, i64 => i64;
u128, i128 => i128;
usize, isize => isize;
}
impl_cast_unsigned! {
u8, i8 => u8;
u16, i16 => u16;
u32, i32 => u32;
u64, i64 => u64;
u128, i128 => u128;
usize, isize => usize;
}
impl_extend! {
u8 => u8, u16, u32, u64, u128, usize;
u16 => u16, u32, u64, u128, usize;
u32 => u32, u64, u128;
u64 => u64, u128;
u128 => u128;
usize => usize;
i8 => i8, i16, i32, i64, i128, isize;
i16 => i16, i32, i64, i128, isize;
i32 => i32, i64, i128;
i64 => i64, i128;
i128 => i128;
isize => isize;
}
impl_truncate! {
u8, u16, u32, u64, u128, usize => u8;
u16, u32, u64, u128, usize => u16;
u32, u64, u128 => u32;
u64, u128 => u64;
u128 => u128;
usize => usize;
i8, i16, i32, i64, i128, isize => i8;
i16, i32, i64, i128, isize => i16;
i32, i64, i128 => i32;
i64, i128 => i64;
i128 => i128;
isize => isize;
}