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
//! truncate-integer: integer truncation for Rust
//!
//! There are several ways one might want to do integer truncation in Rust:
//!
//! - Unchecked: truncation may result in a changed value. You only get
//! the low-order N bits.
//! - Checked: if truncation would result in a changed value, return `None`,
//! otherwise `Some(value)`.
//! - Panicking: if truncation would result in a changed value, 'panic!'
//! This is equivalent to checked truncation with `.unwrap()`, but with a nicer
//! panic message.
//! - Saturating: if truncation would result in a changed value, return
//! the maximum value that would fit in the target type.
//!
//! It's possible to get all of these in Rust without importing additional
//! crates or writing much code, for example:
//!
//! ```rust
//! # use std::convert::TryFrom;
//! let x = 257u16;
//!
//! let unchecked = x as u8;
//! assert_eq!(unchecked, 1u8);
//!
//! let checked = u8::try_from(x);
//! assert!(checked.is_err());
//!
//! // This would panic
//! // let value = x.try_from().unwrap();
//!
//! let saturating = u8::try_from(x).unwrap_or(u8::MAX);
//! assert_eq!(saturating, 255);
//! ```
//!
//! If those are good enough for you, then you don't need this crate.
//! However, if you would prefer to call a function to communicate your
//! intent, then you might find this crate useful.
//!
//! It provides a trait that implements each of the truncation forms listed above:
//!
//! - [`TruncateUnchecked`] performs unchecked truncation.
//! - [`TryTruncate`] performs checked truncation.
//! - [`Chop`] performs panicking truncation.
//! - [`Shrink`] performs saturating truncation.
//!
//! It's sometimes desirable to invert this logic, e.g. in trait bounds,
//! so there is an inverse of each of the above:
//!
//! - [`TruncateFromUnchecked`]
//! - [`TryTruncateFrom`]
//! - [`ChopFrom`]
//! - [`ShrinkFrom`]
//!
//! All of the truncations are implemented for both signed and unsigned
//! integers (including signed-to-unsigned and vice versa), except
//! `TruncateFromUnchecked`, because it's not immediately clear what the
//! correct output would be when then input is outside the output bounds.
pub trait TryTruncate<T> {
/// Try to truncate an integer to fit into a smaller type.
///
/// If the value fits into the target type, return `Ok(value)`
/// Otherwise, return `None`.
fn try_truncate(self) -> Option<T>;
}
pub trait TryTruncateFrom<T>: Sized {
/// Try to truncate an integer to fit into a smaller type.
///
/// If the value fits into the `Self` type, return `Ok(value)`
/// Otherwise, return `None`.
fn try_truncate_from(value: T) -> Option<Self>;
}
impl<Source, Dest> TryTruncateFrom<Source> for Dest
where
Source: TryTruncate<Dest>,
{
fn try_truncate_from(x: Source) -> Option<Self> {
x.try_truncate()
}
}
pub trait Chop<T> {
/// Perform panicking truncation
///
/// If the value fits into the target type, return that value.
/// Otherwise, panic.
fn chop(self) -> T;
}
pub trait ChopFrom<T> {
/// Perform panicking truncation
///
/// If the value fits into the `Self` type, return that value.
/// Otherwise, panic.
fn chop_from(value: T) -> Self;
}
impl<Source, Dest> ChopFrom<Source> for Dest
where
Source: Chop<Dest>,
{
fn chop_from(x: Source) -> Self {
x.chop()
}
}
pub trait TruncateUnchecked<T> {
/// Perform unchecked bitwise truncation
///
/// If the value fits into the target type, return that value.
/// Otherwise, return the low-order bits that do fit.
///
/// This has the same result as using `as` to truncate (e.g. `foo as u8`).
fn truncate_unchecked(self) -> T;
}
pub trait TruncateFromUnchecked<T> {
/// Perform unchecked bitwise truncation
///
/// If the value fits into the `Self` type, return that value.
/// Otherwise, return the low-order bits that do fit.
///
/// This has the same result as using `as` to truncate (e.g. `foo as u8`).
fn truncate_from_unchecked(value: T) -> Self;
}
impl<Source, Dest> TruncateFromUnchecked<Source> for Dest
where
Source: TruncateUnchecked<Dest>,
{
fn truncate_from_unchecked(x: Source) -> Self {
x.truncate_unchecked()
}
}
/// Perform saturating truncation.
pub trait Shrink<T> {
/// Perform saturating truncation.
///
/// If the value fits into the target type, return that value.
/// Otherwise, return the closest value that does fit.
fn shrink(self) -> T;
}
/// Perform saturating truncation.
pub trait ShrinkFrom<T> {
/// Perform saturating truncation.
///
/// If the value fits into the `Self` type, return that value.
/// Otherwise, return the closest value that does fit.
fn shrink_from(value: T) -> Self;
}
impl<Source, Dest> ShrinkFrom<Source> for Dest
where
Source: Shrink<Dest>,
{
fn shrink_from(x: Source) -> Self {
x.shrink()
}
}
macro_rules! make_truncate {
($Source: ty, $Dest:ty) => {
impl TryTruncate<$Dest> for $Source {
#[track_caller]
#[inline]
fn try_truncate(self) -> Option<$Dest> {
use ::core::convert::TryFrom;
<$Dest>::try_from(self).ok()
}
}
impl Chop<$Dest> for $Source {
#[track_caller]
#[inline]
fn chop(self) -> $Dest {
use ::core::convert::TryFrom;
match <$Dest>::try_from(self) {
Ok(val) => val,
Err(_) => panic!("chop overflow"),
}
}
}
impl Shrink<$Dest> for $Source {
#[track_caller]
#[inline]
fn shrink(self) -> $Dest {
use ::core::convert::TryFrom;
match <$Dest>::try_from(self) {
Ok(val) => val,
Err(_) => {
if self < (<$Dest>::MIN) as $Source {
<$Dest>::MIN
} else {
<$Dest>::MAX
}
}
}
}
}
};
}
macro_rules! make_truncate_all {
($Source: ty, $Dest:ty) => {
// FIXME: don't implement this for negative numbers!
impl TruncateUnchecked<$Dest> for $Source {
#[track_caller]
#[inline]
fn truncate_unchecked(self) -> $Dest {
self as $Dest
}
}
make_truncate!($Source, $Dest);
}
}
make_truncate_all!(usize, u8);
make_truncate_all!(usize, u16);
make_truncate_all!(usize, u32);
make_truncate_all!(u128, u8);
make_truncate_all!(u128, u16);
make_truncate_all!(u128, u32);
make_truncate_all!(u128, u64);
make_truncate_all!(u64, u8);
make_truncate_all!(u64, u16);
make_truncate_all!(u64, u32);
make_truncate_all!(u32, u8);
make_truncate_all!(u32, u16);
make_truncate_all!(u16, u8);
make_truncate_all!(u128, i8);
make_truncate_all!(u128, i16);
make_truncate_all!(u128, i32);
make_truncate_all!(u128, i64);
make_truncate_all!(u64, i8);
make_truncate_all!(u64, i16);
make_truncate_all!(u64, i32);
make_truncate_all!(u32, i8);
make_truncate_all!(u32, i16);
make_truncate_all!(u16, i8);
make_truncate!(i128, i64);
make_truncate!(i128, i32);
make_truncate!(i128, i16);
make_truncate!(i128, i8);
make_truncate!(i64, i8);
make_truncate!(i64, i16);
make_truncate!(i64, i32);
make_truncate!(i32, i8);
make_truncate!(i32, i16);
make_truncate!(i16, i8);
make_truncate!(i128, u64);
make_truncate!(i128, u32);
make_truncate!(i128, u16);
make_truncate!(i128, u8);
make_truncate!(i64, u8);
make_truncate!(i64, u16);
make_truncate!(i64, u32);
make_truncate!(i32, u8);
make_truncate!(i32, u16);
make_truncate!(i16, u8);