| 1 | /*! `fun`damental `ty`pes |
| 2 | |
| 3 | This crate provides trait unification of the Rust fundamental items, allowing |
| 4 | users to declare the behavior they want from a number without committing to a |
| 5 | single particular numeric type. |
| 6 | |
| 7 | The number types can be categorized along two axes: behavior and width. Traits |
| 8 | for each axis and group on that axis are provided: |
| 9 | |
| 10 | ## Numeric Categories |
| 11 | |
| 12 | The most general category is represented by the trait [`Numeric`]. It is |
| 13 | implemented by all the numeric fundamentals, and includes only the traits that |
| 14 | they all implement. This is an already-large amount: basic memory management, |
| 15 | comparison, rendering, and numeric arithmetic. |
| 16 | |
| 17 | The numbers are then split into [`Floating`] and [`Integral`]. The former fills |
| 18 | out the API of `f32` and `f64`, while the latter covers all of the `iN` and `uN` |
| 19 | numbers. |
| 20 | |
| 21 | Lastly, [`Integral`] splits further, into [`Signed`] and [`Unsigned`]. These |
| 22 | provide the last specializations unique to the differences between `iN` and |
| 23 | `uN`. |
| 24 | |
| 25 | ## Width Categories |
| 26 | |
| 27 | Every number implements the trait `IsN` for the `N` of its bit width. `isize` |
| 28 | and `usize` implement the trait that matches their width on the target platform. |
| 29 | |
| 30 | In addition, the trait groups `AtLeastN` and `AtMostN` enable clamping the range |
| 31 | of acceptable widths to lower or upper bounds. These traits are equivalent to |
| 32 | `mem::size_of::<T>() >= N` and `mem::size_of::<T>() <= N`, respectively. |
| 33 | |
| 34 | [`Floating`]: trait.Floating.html |
| 35 | [`Integral`]: trait.Integral.html |
| 36 | [`Numeric`]: trait.Numeric.html |
| 37 | [`Signed`]: trait.Signed.html |
| 38 | [`Unsigned`]: trait.Unsigned.html |
| 39 | !*/ |
| 40 | |
| 41 | #![cfg_attr(not(feature = "std"), no_std)] |
| 42 | #![deny(unconditional_recursion)] |
| 43 | |
| 44 | use core::{ |
| 45 | convert::{ |
| 46 | TryFrom, |
| 47 | TryInto, |
| 48 | }, |
| 49 | fmt::{ |
| 50 | Binary, |
| 51 | Debug, |
| 52 | Display, |
| 53 | LowerExp, |
| 54 | LowerHex, |
| 55 | Octal, |
| 56 | UpperExp, |
| 57 | UpperHex, |
| 58 | }, |
| 59 | hash::Hash, |
| 60 | iter::{ |
| 61 | Product, |
| 62 | Sum, |
| 63 | }, |
| 64 | num::{ |
| 65 | FpCategory, |
| 66 | ParseIntError, |
| 67 | }, |
| 68 | ops::{ |
| 69 | Add, |
| 70 | AddAssign, |
| 71 | BitAnd, |
| 72 | BitAndAssign, |
| 73 | BitOr, |
| 74 | BitOrAssign, |
| 75 | BitXor, |
| 76 | BitXorAssign, |
| 77 | Div, |
| 78 | DivAssign, |
| 79 | Mul, |
| 80 | MulAssign, |
| 81 | Neg, |
| 82 | Not, |
| 83 | Rem, |
| 84 | RemAssign, |
| 85 | Shl, |
| 86 | ShlAssign, |
| 87 | Shr, |
| 88 | ShrAssign, |
| 89 | Sub, |
| 90 | SubAssign, |
| 91 | }, |
| 92 | str::FromStr, |
| 93 | }; |
| 94 | |
| 95 | /// Declare that a type is one of the language fundamental types. |
| 96 | pub trait Fundamental: |
| 97 | 'static |
| 98 | + Sized |
| 99 | + Send |
| 100 | + Sync |
| 101 | + Unpin |
| 102 | + Clone |
| 103 | + Copy |
| 104 | + Default |
| 105 | + FromStr |
| 106 | // cmp |
| 107 | + PartialEq<Self> |
| 108 | + PartialOrd<Self> |
| 109 | // fmt |
| 110 | + Debug |
| 111 | + Display |
| 112 | { |
| 113 | /// Tests `self != 0`. |
| 114 | fn as_bool(self) -> bool; |
| 115 | |
| 116 | /// Represents `self` as a Unicode Scalar Value, if possible. |
| 117 | fn as_char(self) -> Option<char>; |
| 118 | |
| 119 | /// Performs `self as i8`. |
| 120 | fn as_i8(self) -> i8; |
| 121 | |
| 122 | /// Performs `self as i16`. |
| 123 | fn as_i16(self) -> i16; |
| 124 | |
| 125 | /// Performs `self as i32`. |
| 126 | fn as_i32(self) -> i32; |
| 127 | |
| 128 | /// Performs `self as i64`. |
| 129 | fn as_i64(self) -> i64; |
| 130 | |
| 131 | /// Performs `self as i128`. |
| 132 | fn as_i128(self) -> i128; |
| 133 | |
| 134 | /// Performs `self as isize`. |
| 135 | fn as_isize(self) -> isize; |
| 136 | |
| 137 | /// Performs `self as u8`. |
| 138 | fn as_u8(self) -> u8; |
| 139 | |
| 140 | /// Performs `self as u16`. |
| 141 | fn as_u16(self) -> u16; |
| 142 | |
| 143 | /// Performs `self as u32`. |
| 144 | fn as_u32(self) -> u32; |
| 145 | |
| 146 | /// Performs `self as u64`. |
| 147 | fn as_u64(self) -> u64; |
| 148 | |
| 149 | /// Performs `self as u128`. |
| 150 | fn as_u128(self) -> u128; |
| 151 | |
| 152 | /// Performs `self as usize`. |
| 153 | fn as_usize(self) -> usize; |
| 154 | |
| 155 | /// Performs `self as f32`. |
| 156 | fn as_f32(self) -> f32; |
| 157 | |
| 158 | /// Performs `self as f64`. |
| 159 | fn as_f64(self) -> f64; |
| 160 | } |
| 161 | |
| 162 | /// Declare that a type is an abstract number. |
| 163 | /// |
| 164 | /// This unifies all of the signed-integer, unsigned-integer, and floating-point |
| 165 | /// types. |
| 166 | pub trait Numeric: |
| 167 | Fundamental |
| 168 | // iter |
| 169 | + Product<Self> |
| 170 | + for<'a> Product<&'a Self> |
| 171 | + Sum<Self> |
| 172 | + for<'a> Sum<&'a Self> |
| 173 | // numeric ops |
| 174 | + Add<Self, Output = Self> |
| 175 | + for<'a> Add<&'a Self, Output = Self> |
| 176 | + AddAssign<Self> |
| 177 | + for<'a> AddAssign<&'a Self> |
| 178 | + Sub<Self, Output = Self> |
| 179 | + for<'a> Sub<&'a Self, Output = Self> |
| 180 | + SubAssign<Self> |
| 181 | + for<'a> SubAssign<&'a Self> |
| 182 | + Mul<Self, Output = Self> |
| 183 | + for<'a> Mul<&'a Self, Output = Self> |
| 184 | + MulAssign<Self> |
| 185 | + for<'a> MulAssign<&'a Self> |
| 186 | + Div<Self, Output = Self> |
| 187 | + for<'a> Div<&'a Self, Output = Self> |
| 188 | + DivAssign<Self> |
| 189 | + for<'a> DivAssign<&'a Self> |
| 190 | + Rem<Self, Output = Self> |
| 191 | + for<'a> Rem<&'a Self, Output = Self> |
| 192 | + RemAssign<Self> |
| 193 | + for<'a> RemAssign<&'a Self> |
| 194 | { |
| 195 | /// The `[u8; N]` byte array that stores values of `Self`. |
| 196 | type Bytes; |
| 197 | |
| 198 | /// Return the memory representation of this number as a byte array in |
| 199 | /// big-endian (network) byte order. |
| 200 | fn to_be_bytes(self) -> Self::Bytes; |
| 201 | |
| 202 | /// Return the memory representation of this number as a byte array in |
| 203 | /// little-endian byte order. |
| 204 | fn to_le_bytes(self) -> Self::Bytes; |
| 205 | |
| 206 | /// Return the memory representation of this number as a byte array in |
| 207 | /// native byte order. |
| 208 | fn to_ne_bytes(self) -> Self::Bytes; |
| 209 | |
| 210 | /// Create a numeric value from its representation as a byte array in big |
| 211 | /// endian. |
| 212 | fn from_be_bytes(bytes: Self::Bytes) -> Self; |
| 213 | |
| 214 | /// Create a numeric value from its representation as a byte array in little |
| 215 | /// endian. |
| 216 | fn from_le_bytes(bytes: Self::Bytes) -> Self; |
| 217 | |
| 218 | /// Create a numeric value from its memory representation as a byte array in |
| 219 | /// native endianness. |
| 220 | fn from_ne_bytes(bytes: Self::Bytes) -> Self; |
| 221 | } |
| 222 | |
| 223 | /// Declare that a type is a fixed-point integer. |
| 224 | /// |
| 225 | /// This unifies all of the signed and unsigned integral types. |
| 226 | pub trait Integral: |
| 227 | Numeric |
| 228 | + Hash |
| 229 | + Eq |
| 230 | + Ord |
| 231 | + Binary |
| 232 | + LowerHex |
| 233 | + UpperHex |
| 234 | + Octal |
| 235 | + BitAnd<Self, Output = Self> |
| 236 | + for<'a> BitAnd<&'a Self, Output = Self> |
| 237 | + BitAndAssign<Self> |
| 238 | + for<'a> BitAndAssign<&'a Self> |
| 239 | + BitOr<Self, Output = Self> |
| 240 | + for<'a> BitOr<&'a Self, Output = Self> |
| 241 | + BitOrAssign<Self> |
| 242 | + for<'a> BitOrAssign<&'a Self> |
| 243 | + BitXor<Self, Output = Self> |
| 244 | + for<'a> BitXor<&'a Self, Output = Self> |
| 245 | + BitXorAssign<Self> |
| 246 | + for<'a> BitXorAssign<&'a Self> |
| 247 | + Not<Output = Self> |
| 248 | + TryFrom<i8> |
| 249 | + TryFrom<u8> |
| 250 | + TryFrom<i16> |
| 251 | + TryFrom<u16> |
| 252 | + TryFrom<i32> |
| 253 | + TryFrom<u32> |
| 254 | + TryFrom<i64> |
| 255 | + TryFrom<u64> |
| 256 | + TryFrom<i128> |
| 257 | + TryFrom<u128> |
| 258 | + TryFrom<isize> |
| 259 | + TryFrom<usize> |
| 260 | + TryInto<i8> |
| 261 | + TryInto<u8> |
| 262 | + TryInto<i16> |
| 263 | + TryInto<u16> |
| 264 | + TryInto<i32> |
| 265 | + TryInto<u32> |
| 266 | + TryInto<i64> |
| 267 | + TryInto<u64> |
| 268 | + TryInto<i128> |
| 269 | + TryInto<u128> |
| 270 | + TryInto<isize> |
| 271 | + TryInto<usize> |
| 272 | + Shl<Self, Output = Self> |
| 273 | + for<'a> Shl<&'a Self, Output = Self> |
| 274 | + ShlAssign<Self> |
| 275 | + for<'a> ShlAssign<&'a Self> |
| 276 | + Shr<Self, Output = Self> |
| 277 | + for<'a> Shr<&'a Self, Output = Self> |
| 278 | + ShrAssign<Self> |
| 279 | + for<'a> ShrAssign<&'a Self> |
| 280 | + Shl<i8, Output = Self> |
| 281 | + for<'a> Shl<&'a i8, Output = Self> |
| 282 | + ShlAssign<i8> |
| 283 | + for<'a> ShlAssign<&'a i8> |
| 284 | + Shr<i8, Output = Self> |
| 285 | + for<'a> Shr<&'a i8, Output = Self> |
| 286 | + ShrAssign<i8> |
| 287 | + for<'a> ShrAssign<&'a i8> |
| 288 | + Shl<u8, Output = Self> |
| 289 | + for<'a> Shl<&'a u8, Output = Self> |
| 290 | + ShlAssign<u8> |
| 291 | + for<'a> ShlAssign<&'a u8> |
| 292 | + Shr<u8, Output = Self> |
| 293 | + for<'a> Shr<&'a u8, Output = Self> |
| 294 | + ShrAssign<u8> |
| 295 | + for<'a> ShrAssign<&'a u8> |
| 296 | + Shl<i16, Output = Self> |
| 297 | + for<'a> Shl<&'a i16, Output = Self> |
| 298 | + ShlAssign<i16> |
| 299 | + for<'a> ShlAssign<&'a i16> |
| 300 | + Shr<i16, Output = Self> |
| 301 | + for<'a> Shr<&'a i16, Output = Self> |
| 302 | + ShrAssign<i16> |
| 303 | + for<'a> ShrAssign<&'a i16> |
| 304 | + Shl<u16, Output = Self> |
| 305 | + for<'a> Shl<&'a u16, Output = Self> |
| 306 | + ShlAssign<u16> |
| 307 | + for<'a> ShlAssign<&'a u16> |
| 308 | + Shr<u16, Output = Self> |
| 309 | + for<'a> Shr<&'a u16, Output = Self> |
| 310 | + ShrAssign<u16> |
| 311 | + for<'a> ShrAssign<&'a u16> |
| 312 | + Shl<i32, Output = Self> |
| 313 | + for<'a> Shl<&'a i32, Output = Self> |
| 314 | + ShlAssign<i32> |
| 315 | + for<'a> ShlAssign<&'a i32> |
| 316 | + Shr<i32, Output = Self> |
| 317 | + for<'a> Shr<&'a i32, Output = Self> |
| 318 | + ShrAssign<i32> |
| 319 | + for<'a> ShrAssign<&'a i32> |
| 320 | + Shl<u32, Output = Self> |
| 321 | + for<'a> Shl<&'a u32, Output = Self> |
| 322 | + ShlAssign<u32> |
| 323 | + for<'a> ShlAssign<&'a u32> |
| 324 | + Shr<u32, Output = Self> |
| 325 | + for<'a> Shr<&'a u32, Output = Self> |
| 326 | + ShrAssign<u32> |
| 327 | + for<'a> ShrAssign<&'a u32> |
| 328 | + Shl<i64, Output = Self> |
| 329 | + for<'a> Shl<&'a i64, Output = Self> |
| 330 | + ShlAssign<i64> |
| 331 | + for<'a> ShlAssign<&'a i64> |
| 332 | + Shr<i64, Output = Self> |
| 333 | + for<'a> Shr<&'a i64, Output = Self> |
| 334 | + ShrAssign<i64> |
| 335 | + for<'a> ShrAssign<&'a i64> |
| 336 | + Shl<u64, Output = Self> |
| 337 | + for<'a> Shl<&'a u64, Output = Self> |
| 338 | + ShlAssign<u64> |
| 339 | + for<'a> ShlAssign<&'a u64> |
| 340 | + Shr<u64, Output = Self> |
| 341 | + for<'a> Shr<&'a u64, Output = Self> |
| 342 | + ShrAssign<u64> |
| 343 | + for<'a> ShrAssign<&'a u64> |
| 344 | + Shl<i128, Output = Self> |
| 345 | + for<'a> Shl<&'a i128, Output = Self> |
| 346 | + ShlAssign<i128> |
| 347 | + for<'a> ShlAssign<&'a i128> |
| 348 | + Shr<i128, Output = Self> |
| 349 | + for<'a> Shr<&'a i128, Output = Self> |
| 350 | + ShrAssign<i128> |
| 351 | + for<'a> ShrAssign<&'a i128> |
| 352 | + Shl<u128, Output = Self> |
| 353 | + for<'a> Shl<&'a u128, Output = Self> |
| 354 | + ShlAssign<u128> |
| 355 | + for<'a> ShlAssign<&'a u128> |
| 356 | + Shr<u128, Output = Self> |
| 357 | + for<'a> Shr<&'a u128, Output = Self> |
| 358 | + ShrAssign<u128> |
| 359 | + for<'a> ShrAssign<&'a u128> |
| 360 | + Shl<isize, Output = Self> |
| 361 | + for<'a> Shl<&'a isize, Output = Self> |
| 362 | + ShlAssign<isize> |
| 363 | + for<'a> ShlAssign<&'a isize> |
| 364 | + Shr<isize, Output = Self> |
| 365 | + for<'a> Shr<&'a isize, Output = Self> |
| 366 | + ShrAssign<isize> |
| 367 | + for<'a> ShrAssign<&'a isize> |
| 368 | + Shl<usize, Output = Self> |
| 369 | + for<'a> Shl<&'a usize, Output = Self> |
| 370 | + ShlAssign<usize> |
| 371 | + for<'a> ShlAssign<&'a usize> |
| 372 | + Shr<usize, Output = Self> |
| 373 | + for<'a> Shr<&'a usize, Output = Self> |
| 374 | + ShrAssign<usize> |
| 375 | + for<'a> ShrAssign<&'a usize> |
| 376 | { |
| 377 | /// The type’s zero value. |
| 378 | const ZERO: Self; |
| 379 | |
| 380 | /// The type’s step value. |
| 381 | const ONE: Self; |
| 382 | |
| 383 | /// The type’s minimum value. This is zero for unsigned integers. |
| 384 | const MIN: Self; |
| 385 | |
| 386 | /// The type’s maximum value. |
| 387 | const MAX: Self; |
| 388 | |
| 389 | /// The size of this type in bits. |
| 390 | const BITS: u32; |
| 391 | |
| 392 | /// Returns the smallest value that can be represented by this integer type. |
| 393 | fn min_value() -> Self; |
| 394 | |
| 395 | /// Returns the largest value that can be represented by this integer type. |
| 396 | fn max_value() -> Self; |
| 397 | |
| 398 | /// Converts a string slice in a given base to an integer. |
| 399 | /// |
| 400 | /// The string is expected to be an optional `+` or `-` sign followed by |
| 401 | /// digits. Leading and trailing whitespace represent an error. Digits are a |
| 402 | /// subset of these characters, depending on `radix`: |
| 403 | /// |
| 404 | /// - `0-9` |
| 405 | /// - `a-z` |
| 406 | /// - `A-Z` |
| 407 | /// |
| 408 | /// # Panics |
| 409 | /// |
| 410 | /// This function panics if `radix` is not in the range from 2 to 36. |
| 411 | fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>; |
| 412 | |
| 413 | /// Returns the number of ones in the binary representation of `self`. |
| 414 | fn count_ones(self) -> u32; |
| 415 | |
| 416 | /// Returns the number of zeros in the binary representation of `self`. |
| 417 | fn count_zeros(self) -> u32; |
| 418 | |
| 419 | /// Returns the number of leading zeros in the binary representation of |
| 420 | /// `self`. |
| 421 | fn leading_zeros(self) -> u32; |
| 422 | |
| 423 | /// Returns the number of trailing zeros in the binary representation of |
| 424 | /// `self`. |
| 425 | fn trailing_zeros(self) -> u32; |
| 426 | |
| 427 | /// Returns the number of leading ones in the binary representation of |
| 428 | /// `self`. |
| 429 | fn leading_ones(self) -> u32; |
| 430 | |
| 431 | /// Returns the number of trailing ones in the binary representation of |
| 432 | /// `self`. |
| 433 | fn trailing_ones(self) -> u32; |
| 434 | |
| 435 | /// Shifts the bits to the left by a specified amount, `n`, wrapping the |
| 436 | /// truncated bits to the end of the resulting integer. |
| 437 | /// |
| 438 | /// Please note this isn’t the same operation as the `<<` shifting operator! |
| 439 | fn rotate_left(self, n: u32) -> Self; |
| 440 | |
| 441 | /// Shifts the bits to the right by a specified amount, `n`, wrapping the |
| 442 | /// truncated bits to the beginning of the resulting integer. |
| 443 | /// |
| 444 | /// Please note this isn’t the same operation as the `>>` shifting operator! |
| 445 | fn rotate_right(self, n: u32) -> Self; |
| 446 | |
| 447 | /// Reverses the byte order of the integer. |
| 448 | fn swap_bytes(self) -> Self; |
| 449 | |
| 450 | /// Reverses the bit pattern of the integer. |
| 451 | fn reverse_bits(self) -> Self; |
| 452 | |
| 453 | /// Converts an integer from big endian to the target’s endianness. |
| 454 | /// |
| 455 | /// On big endian this is a no-op. On little endian the bytes are swapped. |
| 456 | #[allow(clippy::wrong_self_convention)] |
| 457 | fn from_be(self) -> Self; |
| 458 | |
| 459 | /// Converts an integer frm little endian to the target’s endianness. |
| 460 | /// |
| 461 | /// On little endian this is a no-op. On big endian the bytes are swapped. |
| 462 | #[allow(clippy::wrong_self_convention)] |
| 463 | fn from_le(self) -> Self; |
| 464 | |
| 465 | /// Converts `self` to big endian from the target’s endianness. |
| 466 | /// |
| 467 | /// On big endian this is a no-op. On little endian the bytes are swapped. |
| 468 | fn to_be(self) -> Self; |
| 469 | |
| 470 | /// Converts `self` to little endian from the target’s endianness. |
| 471 | /// |
| 472 | /// On little endian this is a no-op. On big endian the bytes are swapped. |
| 473 | fn to_le(self) -> Self; |
| 474 | |
| 475 | /// Checked integer addition. Computes `self + rhs`, returning `None` if |
| 476 | /// overflow occurred. |
| 477 | fn checked_add(self, rhs: Self) -> Option<Self>; |
| 478 | |
| 479 | /// Checked integer subtraction. Computes `self - rhs`, returning `None` if |
| 480 | /// overflow occurred. |
| 481 | fn checked_sub(self, rhs: Self) -> Option<Self>; |
| 482 | |
| 483 | /// Checked integer multiplication. Computes `self * rhs`, returning `None` |
| 484 | /// if overflow occurred. |
| 485 | fn checked_mul(self, rhs: Self) -> Option<Self>; |
| 486 | |
| 487 | /// Checked integer division. Computes `self / rhs`, returning `None` if |
| 488 | /// `rhs == 0` or the division results in overflow. |
| 489 | fn checked_div(self, rhs: Self) -> Option<Self>; |
| 490 | |
| 491 | /// Checked Euclidean division. Computes `self.div_euclid(rhs)`, returning |
| 492 | /// `None` if `rhs == 0` or the division results in overflow. |
| 493 | fn checked_div_euclid(self, rhs: Self) -> Option<Self>; |
| 494 | |
| 495 | /// Checked integer remainder. Computes `self % rhs`, returning `None` if |
| 496 | /// `rhs == 0` or the division results in overflow. |
| 497 | fn checked_rem(self, rhs: Self) -> Option<Self>; |
| 498 | |
| 499 | /// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning |
| 500 | /// `None` if `rhs == 0` or the division results in overflow. |
| 501 | fn checked_rem_euclid(self, rhs: Self) -> Option<Self>; |
| 502 | |
| 503 | /// Checked negation. Computes `-self`, returning `None` if `self == MIN`. |
| 504 | /// |
| 505 | /// Note that negating any positive integer will overflow. |
| 506 | fn checked_neg(self) -> Option<Self>; |
| 507 | |
| 508 | /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is |
| 509 | /// larger than or equal to the number of bits in `self`. |
| 510 | fn checked_shl(self, rhs: u32) -> Option<Self>; |
| 511 | |
| 512 | /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` |
| 513 | /// is larger than or equal to the number of bits in `self`. |
| 514 | fn checked_shr(self, rhs: u32) -> Option<Self>; |
| 515 | |
| 516 | /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if |
| 517 | /// overflow occurred. |
| 518 | fn checked_pow(self, rhs: u32) -> Option<Self>; |
| 519 | |
| 520 | /// Saturating integer addition. Computes `self + rhs`, saturating at the |
| 521 | /// numeric bounds instead of overflowing. |
| 522 | fn saturating_add(self, rhs: Self) -> Self; |
| 523 | |
| 524 | /// Saturating integer subtraction. Computes `self - rhs`, saturating at the |
| 525 | /// numeric bounds instead of overflowing. |
| 526 | fn saturating_sub(self, rhs: Self) -> Self; |
| 527 | |
| 528 | /// Saturating integer multiplication. Computes `self * rhs`, saturating at |
| 529 | /// the numeric bounds instead of overflowing. |
| 530 | fn saturating_mul(self, rhs: Self) -> Self; |
| 531 | |
| 532 | /// Saturating integer exponentiation. Computes `self.pow(exp)`, saturating |
| 533 | /// at the numeric bounds instead of overflowing. |
| 534 | fn saturating_pow(self, rhs: u32) -> Self; |
| 535 | |
| 536 | /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at |
| 537 | /// the boundary of the type. |
| 538 | fn wrapping_add(self, rhs: Self) -> Self; |
| 539 | |
| 540 | /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around |
| 541 | /// at the boundary of the type. |
| 542 | fn wrapping_sub(self, rhs: Self) -> Self; |
| 543 | |
| 544 | /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping |
| 545 | /// around at the boundary of the type. |
| 546 | fn wrapping_mul(self, rhs: Self) -> Self; |
| 547 | |
| 548 | /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at |
| 549 | /// the boundary of the type. |
| 550 | /// |
| 551 | /// # Signed Integers |
| 552 | /// |
| 553 | /// The only case where such wrapping can occur is when one divides |
| 554 | /// `MIN / -1` on a signed type (where `MIN` is the negative minimal value |
| 555 | /// for the type); this is equivalent to `-MIN`, a positive value that is |
| 556 | /// too large to represent in the type. In such a case, this function |
| 557 | /// returns `MIN` itself. |
| 558 | /// |
| 559 | /// # Unsigned Integers |
| 560 | /// |
| 561 | /// Wrapping (modular) division. Computes `self / rhs`. Wrapped division on |
| 562 | /// unsigned types is just normal division. There’s no way wrapping could |
| 563 | /// ever happen. This function exists, so that all operations are accounted |
| 564 | /// for in the wrapping operations. |
| 565 | /// |
| 566 | /// # Panics |
| 567 | /// |
| 568 | /// This function will panic if `rhs` is 0. |
| 569 | fn wrapping_div(self, rhs: Self) -> Self; |
| 570 | |
| 571 | /// Wrapping Euclidean division. Computes `self.div_euclid(rhs)`, wrapping |
| 572 | /// around at the boundary of the type. |
| 573 | /// |
| 574 | /// # Signed Types |
| 575 | /// |
| 576 | /// Wrapping will only occur in `MIN / -1` on a signed type (where `MIN` is |
| 577 | /// the negative minimal value for the type). This is equivalent to `-MIN`, |
| 578 | /// a positive value that is too large to represent in the type. In this |
| 579 | /// case, this method returns `MIN` itself. |
| 580 | /// |
| 581 | /// # Unsigned Types |
| 582 | /// |
| 583 | /// Wrapped division on unsigned types is just normal division. There’s no |
| 584 | /// way wrapping could ever happen. This function exists, so that all |
| 585 | /// operations are accounted for in the wrapping operations. Since, for the |
| 586 | /// positive integers, all common definitions of division are equal, this is |
| 587 | /// exactly equal to `self.wrapping_div(rhs)`. |
| 588 | /// |
| 589 | /// # Panics |
| 590 | /// |
| 591 | /// This function will panic if `rhs` is 0. |
| 592 | fn wrapping_div_euclid(self, rhs: Self) -> Self; |
| 593 | |
| 594 | /// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at |
| 595 | /// the boundary of the type. |
| 596 | /// |
| 597 | /// # Signed Integers |
| 598 | /// |
| 599 | /// Such wrap-around never actually occurs mathematically; implementation |
| 600 | /// artifacts make `x % y` invalid for `MIN / -1` on a signed type (where |
| 601 | /// `MIN` is the negative minimal value). In such a case, this function |
| 602 | /// returns `0`. |
| 603 | /// |
| 604 | /// # Unsigned Integers |
| 605 | /// |
| 606 | /// Wrapped remainder calculation on unsigned types is just the regular |
| 607 | /// remainder calculation. There’s no way wrapping could ever happen. This |
| 608 | /// function exists, so that all operations are accounted for in the |
| 609 | /// wrapping operations. |
| 610 | /// |
| 611 | /// # Panics |
| 612 | /// |
| 613 | /// This function will panic if `rhs` is 0. |
| 614 | fn wrapping_rem(self, rhs: Self) -> Self; |
| 615 | |
| 616 | /// Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping |
| 617 | /// around at the boundary of the type. |
| 618 | /// |
| 619 | /// # Signed Integers |
| 620 | /// |
| 621 | /// Wrapping will only occur in `MIN % -1` on a signed type (where `MIN` is |
| 622 | /// the negative minimal value for the type). In this case, this method |
| 623 | /// returns 0. |
| 624 | /// |
| 625 | /// # Unsigned Integers |
| 626 | /// |
| 627 | /// Wrapped modulo calculation on unsigned types is just the regular |
| 628 | /// remainder calculation. There’s no way wrapping could ever happen. This |
| 629 | /// function exists, so that all operations are accounted for in the |
| 630 | /// wrapping operations. Since, for the positive integers, all common |
| 631 | /// definitions of division are equal, this is exactly equal to |
| 632 | /// `self.wrapping_rem(rhs)`. |
| 633 | /// |
| 634 | /// # Panics |
| 635 | /// |
| 636 | /// This function will panic if `rhs` is 0. |
| 637 | fn wrapping_rem_euclid(self, rhs: Self) -> Self; |
| 638 | |
| 639 | /// Wrapping (modular) negation. Computes `-self`, wrapping around at the |
| 640 | /// boundary of the type. |
| 641 | /// |
| 642 | /// # Signed Integers |
| 643 | /// |
| 644 | /// The only case where such wrapping can occur is when one negates `MIN` |
| 645 | /// on a signed type (where `MIN` is the negative minimal value for the |
| 646 | /// type); this is a positive value that is too large to represent in the |
| 647 | /// type. In such a case, this function returns `MIN` itself. |
| 648 | /// |
| 649 | /// # Unsigned Integers |
| 650 | /// |
| 651 | /// Since unsigned types do not have negative equivalents all applications |
| 652 | /// of this function will wrap (except for `-0`). For values smaller than |
| 653 | /// the corresponding signed type’s maximum the result is the same as |
| 654 | /// casting the corresponding signed value. Any larger values are equivalent |
| 655 | /// to `MAX + 1 - (val - MAX - 1)` where `MAX` is the corresponding signed |
| 656 | /// type’s maximum. |
| 657 | fn wrapping_neg(self) -> Self; |
| 658 | |
| 659 | /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` |
| 660 | /// removes any high-order bits of `rhs` that would cause the shift to |
| 661 | /// exceed the bit-width of the type. |
| 662 | /// |
| 663 | /// Note that this is not the same as a rotate-left; the RHS of a wrapping |
| 664 | /// shift-left is restricted to the range of the type, rather than the bits |
| 665 | /// shifted out of the LHS being returned to the other end. The primitive |
| 666 | /// integer types all implement a `rotate_left` function, which may be what |
| 667 | /// you want instead. |
| 668 | fn wrapping_shl(self, rhs: u32) -> Self; |
| 669 | |
| 670 | /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where `mask` |
| 671 | /// removes any high-order bits of `rhs` that would cause the shift to |
| 672 | /// exceed the bit-width of the type. |
| 673 | /// |
| 674 | /// Note that this is not the same as a rotate-right; the RHS of a wrapping |
| 675 | /// shift-right is restricted to the range of the type, rather than the bits |
| 676 | /// shifted out of the LHS being returned to the other end. The primitive |
| 677 | /// integer types all implement a `rotate_right` function, which may be what |
| 678 | /// you want instead. |
| 679 | fn wrapping_shr(self, rhs: u32) -> Self; |
| 680 | |
| 681 | /// Wrapping (modular) exponentiation. Computes `self.pow(exp)`, wrapping |
| 682 | /// around at the boundary of the type. |
| 683 | fn wrapping_pow(self, rhs: u32) -> Self; |
| 684 | |
| 685 | /// Calculates `self + rhs` |
| 686 | /// |
| 687 | /// Returns a tuple of the addition along with a boolean indicating whether |
| 688 | /// an arithmetic overflow would occur. If an overflow would have occurred |
| 689 | /// then the wrapped value is returned. |
| 690 | fn overflowing_add(self, rhs: Self) -> (Self, bool); |
| 691 | |
| 692 | /// Calculates `self - rhs` |
| 693 | /// |
| 694 | /// Returns a tuple of the subtraction along with a boolean indicating |
| 695 | /// whether an arithmetic overflow would occur. If an overflow would have |
| 696 | /// occurred then the wrapped value is returned. |
| 697 | fn overflowing_sub(self, rhs: Self) -> (Self, bool); |
| 698 | |
| 699 | /// Calculates the multiplication of `self` and `rhs`. |
| 700 | /// |
| 701 | /// Returns a tuple of the multiplication along with a boolean indicating |
| 702 | /// whether an arithmetic overflow would occur. If an overflow would have |
| 703 | /// occurred then the wrapped value is returned. |
| 704 | fn overflowing_mul(self, rhs: Self) -> (Self, bool); |
| 705 | |
| 706 | /// Calculates the divisor when `self` is divided by `rhs`. |
| 707 | /// |
| 708 | /// Returns a tuple of the divisor along with a boolean indicating whether |
| 709 | /// an arithmetic overflow would occur. If an overflow would occur then self |
| 710 | /// is returned. |
| 711 | /// |
| 712 | /// # Panics |
| 713 | /// |
| 714 | /// This function will panic if `rhs` is 0. |
| 715 | fn overflowing_div(self, rhs: Self) -> (Self, bool); |
| 716 | |
| 717 | /// Calculates the quotient of Euclidean division `self.div_euclid(rhs)`. |
| 718 | /// |
| 719 | /// Returns a tuple of the divisor along with a boolean indicating whether |
| 720 | /// an arithmetic overflow would occur. If an overflow would occur then self |
| 721 | /// is returned. |
| 722 | /// |
| 723 | /// # Panics |
| 724 | /// |
| 725 | /// This function will panic if `rhs` is 0. |
| 726 | fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool); |
| 727 | |
| 728 | /// Calculates the remainder when `self` is divided by `rhs`. |
| 729 | /// |
| 730 | /// Returns a tuple of the remainder after dividing along with a boolean |
| 731 | /// indicating whether an arithmetic overflow would occur. If an overflow |
| 732 | /// would occur then 0 is returned. |
| 733 | /// |
| 734 | /// # Panics |
| 735 | /// |
| 736 | /// This function will panic if `rhs` is 0. |
| 737 | fn overflowing_rem(self, rhs: Self) -> (Self, bool); |
| 738 | |
| 739 | /// Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`. |
| 740 | /// |
| 741 | /// Returns a tuple of the remainder after dividing along with a boolean |
| 742 | /// indicating whether an arithmetic overflow would occur. If an overflow |
| 743 | /// would occur then 0 is returned. |
| 744 | /// |
| 745 | /// # Panics |
| 746 | /// |
| 747 | /// This function will panic if rhs is 0. |
| 748 | fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool); |
| 749 | |
| 750 | /// Negates self, overflowing if this is equal to the minimum value. |
| 751 | /// |
| 752 | /// Returns a tuple of the negated version of self along with a boolean |
| 753 | /// indicating whether an overflow happened. If `self` is the minimum value |
| 754 | /// (e.g., `i32::MIN` for values of type `i32`), then the minimum value will |
| 755 | /// be returned again and `true` will be returned for an overflow happening. |
| 756 | fn overflowing_neg(self) -> (Self, bool); |
| 757 | |
| 758 | /// Shifts self left by `rhs` bits. |
| 759 | /// |
| 760 | /// Returns a tuple of the shifted version of self along with a boolean |
| 761 | /// indicating whether the shift value was larger than or equal to the |
| 762 | /// number of bits. If the shift value is too large, then value is masked |
| 763 | /// (N-1) where N is the number of bits, and this value is then used to |
| 764 | /// perform the shift. |
| 765 | fn overflowing_shl(self, rhs: u32) -> (Self, bool); |
| 766 | |
| 767 | /// Shifts self right by `rhs` bits. |
| 768 | /// |
| 769 | /// Returns a tuple of the shifted version of self along with a boolean |
| 770 | /// indicating whether the shift value was larger than or equal to the |
| 771 | /// number of bits. If the shift value is too large, then value is masked |
| 772 | /// (N-1) where N is the number of bits, and this value is then used to |
| 773 | /// perform the shift. |
| 774 | fn overflowing_shr(self, rhs: u32) -> (Self, bool); |
| 775 | |
| 776 | /// Raises self to the power of `exp`, using exponentiation by squaring. |
| 777 | /// |
| 778 | /// Returns a tuple of the exponentiation along with a bool indicating |
| 779 | /// whether an overflow happened. |
| 780 | fn overflowing_pow(self, rhs: u32) -> (Self, bool); |
| 781 | |
| 782 | /// Raises self to the power of `exp`, using exponentiation by squaring. |
| 783 | fn pow(self, rhs: u32) -> Self; |
| 784 | |
| 785 | /// Calculates the quotient of Euclidean division of self by rhs. |
| 786 | /// |
| 787 | /// This computes the integer `n` such that |
| 788 | /// `self = n * rhs + self.rem_euclid(rhs)`, with |
| 789 | /// `0 <= self.rem_euclid(rhs) < rhs`. |
| 790 | /// |
| 791 | /// In other words, the result is `self / rhs` rounded to the integer `n` |
| 792 | /// such that `self >= n * rhs`. If `self > 0`, this is equal to round |
| 793 | /// towards zero (the default in Rust); if `self < 0`, this is equal to |
| 794 | /// round towards +/- infinity. |
| 795 | /// |
| 796 | /// # Panics |
| 797 | /// |
| 798 | /// This function will panic if `rhs` is 0 or the division results in |
| 799 | /// overflow. |
| 800 | fn div_euclid(self, rhs: Self) -> Self; |
| 801 | |
| 802 | /// Calculates the least nonnegative remainder of `self (mod rhs)`. |
| 803 | /// |
| 804 | /// This is done as if by the Euclidean division algorithm -- given |
| 805 | /// `r = self.rem_euclid(rhs)`, `self = rhs * self.div_euclid(rhs) + r`, and |
| 806 | /// `0 <= r < abs(rhs)`. |
| 807 | /// |
| 808 | /// # Panics |
| 809 | /// |
| 810 | /// This function will panic if `rhs` is 0 or the division results in |
| 811 | /// overflow. |
| 812 | fn rem_euclid(self, rhs: Self) -> Self; |
| 813 | } |
| 814 | |
| 815 | /// Declare that a type is a signed integer. |
| 816 | pub trait Signed: Integral + Neg { |
| 817 | /// Checked absolute value. Computes `self.abs()`, returning `None` if |
| 818 | /// `self == MIN`. |
| 819 | fn checked_abs(self) -> Option<Self>; |
| 820 | |
| 821 | /// Wrapping (modular) absolute value. Computes `self.abs()`, wrapping |
| 822 | /// around at the boundary of the type. |
| 823 | /// |
| 824 | /// The only case where such wrapping can occur is when one takes the |
| 825 | /// absolute value of the negative minimal value for the type this is a |
| 826 | /// positive value that is too large to represent in the type. In such a |
| 827 | /// case, this function returns `MIN` itself. |
| 828 | fn wrapping_abs(self) -> Self; |
| 829 | |
| 830 | /// Computes the absolute value of `self`. |
| 831 | /// |
| 832 | /// Returns a tuple of the absolute version of self along with a boolean |
| 833 | /// indicating whether an overflow happened. If self is the minimum value |
| 834 | /// (e.g., iN::MIN for values of type iN), then the minimum value will be |
| 835 | /// returned again and true will be returned for an overflow happening. |
| 836 | fn overflowing_abs(self) -> (Self, bool); |
| 837 | |
| 838 | //// Computes the absolute value of self. |
| 839 | /// |
| 840 | /// # Overflow behavior |
| 841 | /// |
| 842 | /// The absolute value of `iN::min_value()` cannot be represented as an |
| 843 | /// `iN`, and attempting to calculate it will cause an overflow. This means |
| 844 | /// that code in debug mode will trigger a panic on this case and optimized |
| 845 | /// code will return `iN::min_value()` without a panic. |
| 846 | fn abs(self) -> Self; |
| 847 | |
| 848 | /// Returns a number representing sign of `self`. |
| 849 | /// |
| 850 | /// - `0` if the number is zero |
| 851 | /// - `1` if the number is positive |
| 852 | /// - `-1` if the number is negative |
| 853 | fn signum(self) -> Self; |
| 854 | |
| 855 | /// Returns `true` if `self` is positive and `false` if the number is zero |
| 856 | /// or negative. |
| 857 | fn is_positive(self) -> bool; |
| 858 | |
| 859 | /// Returns `true` if `self` is negative and `false` if the number is zero |
| 860 | /// or positive. |
| 861 | fn is_negative(self) -> bool; |
| 862 | } |
| 863 | |
| 864 | /// Declare that a type is an unsigned integer. |
| 865 | pub trait Unsigned: Integral { |
| 866 | /// Returns `true` if and only if `self == 2^k` for some `k`. |
| 867 | fn is_power_of_two(self) -> bool; |
| 868 | |
| 869 | /// Returns the smallest power of two greater than or equal to `self`. |
| 870 | /// |
| 871 | /// When return value overflows (i.e., `self > (1 << (N-1))` for type `uN`), |
| 872 | /// it panics in debug mode and return value is wrapped to 0 in release mode |
| 873 | /// (the only situation in which method can return 0). |
| 874 | fn next_power_of_two(self) -> Self; |
| 875 | |
| 876 | /// Returns the smallest power of two greater than or equal to `n`. If the |
| 877 | /// next power of two is greater than the type’s maximum value, `None` is |
| 878 | /// returned, otherwise the power of two is wrapped in `Some`. |
| 879 | fn checked_next_power_of_two(self) -> Option<Self>; |
| 880 | } |
| 881 | |
| 882 | /// Declare that a type is a floating-point number. |
| 883 | pub trait Floating: |
| 884 | Numeric |
| 885 | + LowerExp |
| 886 | + UpperExp |
| 887 | + Neg |
| 888 | + From<f32> |
| 889 | + From<i8> |
| 890 | + From<i16> |
| 891 | + From<u8> |
| 892 | + From<u16> |
| 893 | { |
| 894 | /// The unsigned integer type of the same width as `Self`. |
| 895 | type Raw: Unsigned; |
| 896 | |
| 897 | /// The radix or base of the internal representation of `f32`. |
| 898 | const RADIX: u32; |
| 899 | |
| 900 | /// Number of significant digits in base 2. |
| 901 | const MANTISSA_DIGITS: u32; |
| 902 | |
| 903 | /// Approximate number of significant digits in base 10. |
| 904 | const DIGITS: u32; |
| 905 | |
| 906 | /// [Machine epsilon] value for `f32`. |
| 907 | /// |
| 908 | /// This is the difference between `1.0` and the next larger representable |
| 909 | /// number. |
| 910 | /// |
| 911 | /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon |
| 912 | const EPSILON: Self; |
| 913 | |
| 914 | /// Smallest finite `f32` value. |
| 915 | const MIN: Self; |
| 916 | |
| 917 | /// Smallest positive normal `f32` value. |
| 918 | const MIN_POSITIVE: Self; |
| 919 | |
| 920 | /// Largest finite `f32` value. |
| 921 | const MAX: Self; |
| 922 | |
| 923 | /// One greater than the minimum possible normal power of 2 exponent. |
| 924 | const MIN_EXP: i32; |
| 925 | |
| 926 | /// Maximum possible power of 2 exponent. |
| 927 | const MAX_EXP: i32; |
| 928 | |
| 929 | /// Minimum possible normal power of 10 exponent. |
| 930 | const MIN_10_EXP: i32; |
| 931 | |
| 932 | /// Maximum possible power of 10 exponent. |
| 933 | const MAX_10_EXP: i32; |
| 934 | |
| 935 | /// Not a Number (NaN). |
| 936 | const NAN: Self; |
| 937 | |
| 938 | /// Infinity (∞). |
| 939 | const INFINITY: Self; |
| 940 | |
| 941 | /// Negative infinity (−∞). |
| 942 | const NEG_INFINITY: Self; |
| 943 | |
| 944 | /// Archimedes' constant (Ï€) |
| 945 | const PI: Self; |
| 946 | |
| 947 | /// π/2 |
| 948 | const FRAC_PI_2: Self; |
| 949 | |
| 950 | /// π/3 |
| 951 | const FRAC_PI_3: Self; |
| 952 | |
| 953 | /// π/4 |
| 954 | const FRAC_PI_4: Self; |
| 955 | |
| 956 | /// π/6 |
| 957 | const FRAC_PI_6: Self; |
| 958 | |
| 959 | /// π/8 |
| 960 | const FRAC_PI_8: Self; |
| 961 | |
| 962 | /// 1/Ï€ |
| 963 | const FRAC_1_PI: Self; |
| 964 | |
| 965 | /// 2/Ï€ |
| 966 | const FRAC_2_PI: Self; |
| 967 | |
| 968 | /// 2/sqrt(Ï€) |
| 969 | const FRAC_2_SQRT_PI: Self; |
| 970 | |
| 971 | /// sqrt(2) |
| 972 | const SQRT_2: Self; |
| 973 | |
| 974 | /// 1/sqrt(2) |
| 975 | const FRAC_1_SQRT_2: Self; |
| 976 | |
| 977 | /// Euler’s number (e) |
| 978 | const E: Self; |
| 979 | |
| 980 | /// log<sub>2</sub>(e) |
| 981 | const LOG2_E: Self; |
| 982 | |
| 983 | /// log<sub>10</sub>(e) |
| 984 | const LOG10_E: Self; |
| 985 | |
| 986 | /// ln(2) |
| 987 | const LN_2: Self; |
| 988 | |
| 989 | /// ln(10) |
| 990 | const LN_10: Self; |
| 991 | |
| 992 | // These functions are only available in `std`, because they rely on the |
| 993 | // system math library `libm` which is not provided by `core`. |
| 994 | |
| 995 | /// Returns the largest integer less than or equal to a number. |
| 996 | #[cfg(feature = "std")] |
| 997 | fn floor(self) -> Self; |
| 998 | |
| 999 | /// Returns the smallest integer greater than or equal to a number. |
| 1000 | #[cfg(feature = "std")] |
| 1001 | fn ceil(self) -> Self; |
| 1002 | |
| 1003 | /// Returns the nearest integer to a number. Round half-way cases away from |
| 1004 | /// `0.0`. |
| 1005 | #[cfg(feature = "std")] |
| 1006 | fn round(self) -> Self; |
| 1007 | |
| 1008 | /// Returns the integer part of a number. |
| 1009 | #[cfg(feature = "std")] |
| 1010 | fn trunc(self) -> Self; |
| 1011 | |
| 1012 | /// Returns the fractional part of a number. |
| 1013 | #[cfg(feature = "std")] |
| 1014 | fn fract(self) -> Self; |
| 1015 | |
| 1016 | /// Computes the absolute value of `self`. Returns `NAN` if the |
| 1017 | /// number is `NAN`. |
| 1018 | #[cfg(feature = "std")] |
| 1019 | fn abs(self) -> Self; |
| 1020 | |
| 1021 | /// Returns a number that represents the sign of `self`. |
| 1022 | /// |
| 1023 | /// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 1024 | /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 1025 | /// - `NAN` if the number is `NAN` |
| 1026 | #[cfg(feature = "std")] |
| 1027 | fn signum(self) -> Self; |
| 1028 | |
| 1029 | /// Returns a number composed of the magnitude of `self` and the sign of |
| 1030 | /// `sign`. |
| 1031 | /// |
| 1032 | /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise |
| 1033 | /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of |
| 1034 | /// `sign` is returned. |
| 1035 | #[cfg(feature = "std")] |
| 1036 | fn copysign(self, sign: Self) -> Self; |
| 1037 | |
| 1038 | /// Fused multiply-add. Computes `(self * a) + b` with only one rounding |
| 1039 | /// error, yielding a more accurate result than an un-fused multiply-add. |
| 1040 | /// |
| 1041 | /// Using `mul_add` can be more performant than an un-fused multiply-add if |
| 1042 | /// the target architecture has a dedicated `fma` CPU instruction. |
| 1043 | #[cfg(feature = "std")] |
| 1044 | fn mul_add(self, a: Self, b: Self) -> Self; |
| 1045 | |
| 1046 | /// Calculates Euclidean division, the matching method for `rem_euclid`. |
| 1047 | /// |
| 1048 | /// This computes the integer `n` such that |
| 1049 | /// `self = n * rhs + self.rem_euclid(rhs)`. |
| 1050 | /// In other words, the result is `self / rhs` rounded to the integer `n` |
| 1051 | /// such that `self >= n * rhs`. |
| 1052 | #[cfg(feature = "std")] |
| 1053 | fn div_euclid(self, rhs: Self) -> Self; |
| 1054 | |
| 1055 | /// Calculates the least nonnegative remainder of `self (mod rhs)`. |
| 1056 | /// |
| 1057 | /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in |
| 1058 | /// most cases. However, due to a floating point round-off error it can |
| 1059 | /// result in `r == rhs.abs()`, violating the mathematical definition, if |
| 1060 | /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`. |
| 1061 | /// This result is not an element of the function's codomain, but it is the |
| 1062 | /// closest floating point number in the real numbers and thus fulfills the |
| 1063 | /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)` |
| 1064 | /// approximatively. |
| 1065 | #[cfg(feature = "std")] |
| 1066 | fn rem_euclid(self, rhs: Self) -> Self; |
| 1067 | |
| 1068 | /// Raises a number to an integer power. |
| 1069 | /// |
| 1070 | /// Using this function is generally faster than using `powf` |
| 1071 | #[cfg(feature = "std")] |
| 1072 | fn powi(self, n: i32) -> Self; |
| 1073 | |
| 1074 | /// Raises a number to a floating point power. |
| 1075 | #[cfg(feature = "std")] |
| 1076 | fn powf(self, n: Self) -> Self; |
| 1077 | |
| 1078 | /// Returns the square root of a number. |
| 1079 | /// |
| 1080 | /// Returns NaN if `self` is a negative number. |
| 1081 | #[cfg(feature = "std")] |
| 1082 | fn sqrt(self) -> Self; |
| 1083 | |
| 1084 | /// Returns `e^(self)`, (the exponential function). |
| 1085 | #[cfg(feature = "std")] |
| 1086 | fn exp(self) -> Self; |
| 1087 | |
| 1088 | /// Returns `2^(self)`. |
| 1089 | #[cfg(feature = "std")] |
| 1090 | fn exp2(self) -> Self; |
| 1091 | |
| 1092 | /// Returns the natural logarithm of the number. |
| 1093 | #[cfg(feature = "std")] |
| 1094 | fn ln(self) -> Self; |
| 1095 | |
| 1096 | /// Returns the logarithm of the number with respect to an arbitrary base. |
| 1097 | /// |
| 1098 | /// The result may not be correctly rounded owing to implementation details; |
| 1099 | /// `self.log2()` can produce more accurate results for base 2, and |
| 1100 | /// `self.log10()` can produce more accurate results for base 10. |
| 1101 | #[cfg(feature = "std")] |
| 1102 | fn log(self, base: Self) -> Self; |
| 1103 | |
| 1104 | /// Returns the base 2 logarithm of the number. |
| 1105 | #[cfg(feature = "std")] |
| 1106 | fn log2(self) -> Self; |
| 1107 | |
| 1108 | /// Returns the base 10 logarithm of the number. |
| 1109 | #[cfg(feature = "std")] |
| 1110 | fn log10(self) -> Self; |
| 1111 | |
| 1112 | /// Returns the cubic root of a number. |
| 1113 | #[cfg(feature = "std")] |
| 1114 | fn cbrt(self) -> Self; |
| 1115 | |
| 1116 | /// Computes the sine of a number (in radians). |
| 1117 | #[cfg(feature = "std")] |
| 1118 | fn hypot(self, other: Self) -> Self; |
| 1119 | |
| 1120 | /// Computes the sine of a number (in radians). |
| 1121 | #[cfg(feature = "std")] |
| 1122 | fn sin(self) -> Self; |
| 1123 | |
| 1124 | /// Computes the cosine of a number (in radians). |
| 1125 | #[cfg(feature = "std")] |
| 1126 | fn cos(self) -> Self; |
| 1127 | |
| 1128 | /// Computes the tangent of a number (in radians). |
| 1129 | #[cfg(feature = "std")] |
| 1130 | fn tan(self) -> Self; |
| 1131 | |
| 1132 | /// Computes the arcsine of a number. Return value is in radians in the |
| 1133 | /// range [-pi/2, pi/2] or NaN if the number is outside the range [-1, 1]. |
| 1134 | #[cfg(feature = "std")] |
| 1135 | fn asin(self) -> Self; |
| 1136 | |
| 1137 | /// Computes the arccosine of a number. Return value is in radians in the |
| 1138 | /// range [0, pi] or NaN if the number is outside the range [-1, 1]. |
| 1139 | #[cfg(feature = "std")] |
| 1140 | fn acos(self) -> Self; |
| 1141 | |
| 1142 | /// Computes the arctangent of a number. Return value is in radians in the |
| 1143 | /// range [-pi/2, pi/2]; |
| 1144 | #[cfg(feature = "std")] |
| 1145 | fn atan(self) -> Self; |
| 1146 | |
| 1147 | /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) |
| 1148 | /// in radians. |
| 1149 | /// |
| 1150 | /// - `x = 0`, `y = 0`: `0` |
| 1151 | /// - `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]` |
| 1152 | /// - `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]` |
| 1153 | /// - `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)` |
| 1154 | #[cfg(feature = "std")] |
| 1155 | fn atan2(self, other: Self) -> Self; |
| 1156 | |
| 1157 | /// Simultaneously computes the sine and cosine of the number, `x`. Returns |
| 1158 | /// `(sin(x), cos(x))`. |
| 1159 | #[cfg(feature = "std")] |
| 1160 | fn sin_cos(self) -> (Self, Self); |
| 1161 | |
| 1162 | /// Returns `e^(self) - 1` in a way that is accurate even if the number is |
| 1163 | /// close to zero. |
| 1164 | #[cfg(feature = "std")] |
| 1165 | fn exp_m1(self) -> Self; |
| 1166 | |
| 1167 | /// Returns `ln(1+n)` (natural logarithm) more accurately than if the |
| 1168 | /// operations were performed separately. |
| 1169 | #[cfg(feature = "std")] |
| 1170 | fn ln_1p(self) -> Self; |
| 1171 | |
| 1172 | /// Hyperbolic sine function. |
| 1173 | #[cfg(feature = "std")] |
| 1174 | fn sinh(self) -> Self; |
| 1175 | |
| 1176 | /// Hyperbolic cosine function. |
| 1177 | #[cfg(feature = "std")] |
| 1178 | fn cosh(self) -> Self; |
| 1179 | |
| 1180 | /// Hyperbolic tangent function. |
| 1181 | #[cfg(feature = "std")] |
| 1182 | fn tanh(self) -> Self; |
| 1183 | |
| 1184 | /// Inverse hyperbolic sine function. |
| 1185 | #[cfg(feature = "std")] |
| 1186 | fn asinh(self) -> Self; |
| 1187 | |
| 1188 | /// Inverse hyperbolic cosine function. |
| 1189 | #[cfg(feature = "std")] |
| 1190 | fn acosh(self) -> Self; |
| 1191 | |
| 1192 | /// Inverse hyperbolic tangent function. |
| 1193 | #[cfg(feature = "std")] |
| 1194 | fn atanh(self) -> Self; |
| 1195 | |
| 1196 | /// Returns `true` if this value is `NaN`. |
| 1197 | fn is_nan(self) -> bool; |
| 1198 | |
| 1199 | /// Returns `true` if this value is positive infinity or negative infinity, |
| 1200 | /// and `false` otherwise. |
| 1201 | fn is_infinite(self) -> bool; |
| 1202 | |
| 1203 | /// Returns `true` if this number is neither infinite nor `NaN`. |
| 1204 | fn is_finite(self) -> bool; |
| 1205 | |
| 1206 | /// Returns `true` if the number is neither zero, infinite, [subnormal], or |
| 1207 | /// `NaN`. |
| 1208 | /// |
| 1209 | /// [subnormal]: https://en.wixipedia.org/wiki/Denormal_number |
| 1210 | fn is_normal(self) -> bool; |
| 1211 | |
| 1212 | /// Returns the floating point category of the number. If only one property |
| 1213 | /// is going to be tested, it is generally faster to use the specific |
| 1214 | /// predicate instead. |
| 1215 | fn classify(self) -> FpCategory; |
| 1216 | |
| 1217 | /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s |
| 1218 | /// with positive sign bit and positive infinity. |
| 1219 | fn is_sign_positive(self) -> bool; |
| 1220 | |
| 1221 | /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s |
| 1222 | /// with negative sign bit and negative infinity. |
| 1223 | fn is_sign_negative(self) -> bool; |
| 1224 | |
| 1225 | /// Takes the reciprocal (inverse) of a number, `1/x`. |
| 1226 | fn recip(self) -> Self; |
| 1227 | |
| 1228 | /// Converts radians to degrees. |
| 1229 | fn to_degrees(self) -> Self; |
| 1230 | |
| 1231 | /// Converts degrees to radians. |
| 1232 | fn to_radians(self) -> Self; |
| 1233 | |
| 1234 | /// Returns the maximum of the two numbers. |
| 1235 | fn max(self, other: Self) -> Self; |
| 1236 | |
| 1237 | /// Returns the minimum of the two numbers. |
| 1238 | fn min(self, other: Self) -> Self; |
| 1239 | |
| 1240 | /// Raw transmutation to `u32`. |
| 1241 | /// |
| 1242 | /// This is currently identical to `transmute::<f32, u32>(self)` on all |
| 1243 | /// platforms. |
| 1244 | /// |
| 1245 | /// See `from_bits` for some discussion of the portability of this operation |
| 1246 | /// (there are almost no issues). |
| 1247 | /// |
| 1248 | /// Note that this function is distinct from `as` casting, which attempts to |
| 1249 | /// preserve the *numeric* value, and not the bitwise value. |
| 1250 | fn to_bits(self) -> Self::Raw; |
| 1251 | |
| 1252 | /// Raw transmutation from `u32`. |
| 1253 | /// |
| 1254 | /// This is currently identical to `transmute::<u32, f32>(v)` on all |
| 1255 | /// platforms. It turns out this is incredibly portable, for two reasons: |
| 1256 | /// |
| 1257 | /// - Floats and Ints have the same endianness on all supported platforms. |
| 1258 | /// - IEEE-754 very precisely specifies the bit layout of floats. |
| 1259 | /// |
| 1260 | /// However there is one caveat: prior to the 2008 version of IEEE-754, how |
| 1261 | /// to interpret the NaN signaling bit wasn't actually specified. Most |
| 1262 | /// platforms (notably x86 and ARM) picked the interpretation that was |
| 1263 | /// ultimately standardized in 2008, but some didn't (notably MIPS). As a |
| 1264 | /// result, all signaling NaNs on MIPS are quiet NaNs on x86, and |
| 1265 | /// vice-versa. |
| 1266 | /// |
| 1267 | /// Rather than trying to preserve signaling-ness cross-platform, this |
| 1268 | /// implementation favors preserving the exact bits. This means that |
| 1269 | /// any payloads encoded in NaNs will be preserved even if the result of |
| 1270 | /// this method is sent over the network from an x86 machine to a MIPS one. |
| 1271 | /// |
| 1272 | /// If the results of this method are only manipulated by the same |
| 1273 | /// architecture that produced them, then there is no portability concern. |
| 1274 | /// |
| 1275 | /// If the input isn't NaN, then there is no portability concern. |
| 1276 | /// |
| 1277 | /// If you don't care about signalingness (very likely), then there is no |
| 1278 | /// portability concern. |
| 1279 | /// |
| 1280 | /// Note that this function is distinct from `as` casting, which attempts to |
| 1281 | /// preserve the *numeric* value, and not the bitwise value. |
| 1282 | fn from_bits(bits: Self::Raw) -> Self; |
| 1283 | } |
| 1284 | |
| 1285 | /// Declare that a type is exactly eight bits wide. |
| 1286 | pub trait Is8: Numeric {} |
| 1287 | |
| 1288 | /// Declare that a type is exactly sixteen bits wide. |
| 1289 | pub trait Is16: Numeric {} |
| 1290 | |
| 1291 | /// Declare that a type is exactly thirty-two bits wide. |
| 1292 | pub trait Is32: Numeric {} |
| 1293 | |
| 1294 | /// Declare that a type is exactly sixty-four bits wide. |
| 1295 | pub trait Is64: Numeric {} |
| 1296 | |
| 1297 | /// Declare that a type is exactly one hundred twenty-eight bits wide. |
| 1298 | pub trait Is128: Numeric {} |
| 1299 | |
| 1300 | /// Declare that a type is eight or more bits wide. |
| 1301 | pub trait AtLeast8: Numeric {} |
| 1302 | |
| 1303 | /// Declare that a type is sixteen or more bits wide. |
| 1304 | pub trait AtLeast16: Numeric {} |
| 1305 | |
| 1306 | /// Declare that a type is thirty-two or more bits wide. |
| 1307 | pub trait AtLeast32: Numeric {} |
| 1308 | |
| 1309 | /// Declare that a type is sixty-four or more bits wide. |
| 1310 | pub trait AtLeast64: Numeric {} |
| 1311 | |
| 1312 | /// Declare that a type is one hundred twenty-eight or more bits wide. |
| 1313 | pub trait AtLeast128: Numeric {} |
| 1314 | |
| 1315 | /// Declare that a type is eight or fewer bits wide. |
| 1316 | pub trait AtMost8: Numeric {} |
| 1317 | |
| 1318 | /// Declare that a type is sixteen or fewer bits wide. |
| 1319 | pub trait AtMost16: Numeric {} |
| 1320 | |
| 1321 | /// Declare that a type is thirty-two or fewer bits wide. |
| 1322 | pub trait AtMost32: Numeric {} |
| 1323 | |
| 1324 | /// Declare that a type is sixty-four or fewer bits wide. |
| 1325 | pub trait AtMost64: Numeric {} |
| 1326 | |
| 1327 | /// Declare that a type is one hundred twenty-eight or fewer bits wide. |
| 1328 | pub trait AtMost128: Numeric {} |
| 1329 | |
| 1330 | /// Creates new wrapper functions that forward to inherent items of the same |
| 1331 | /// name and signature. |
| 1332 | macro_rules! func { |
| 1333 | ( |
| 1334 | $(@$std:literal)? |
| 1335 | $name:ident (self$(, $arg:ident: $t:ty)*) $(-> $ret:ty)?; |
| 1336 | $($tt:tt)* |
| 1337 | ) => { |
| 1338 | $(#[cfg(feature = $std)])? |
| 1339 | fn $name(self$(, $arg: $t)*) $(-> $ret)? |
| 1340 | { |
| 1341 | <Self>::$name(self$(, $arg)*) |
| 1342 | } |
| 1343 | |
| 1344 | func!($($tt)*); |
| 1345 | }; |
| 1346 | ( |
| 1347 | $(@$std:literal)? |
| 1348 | $name:ident(&self$(, $arg:ident: $t:ty)*) $(-> $ret:ty)?; |
| 1349 | $($tt:tt)* |
| 1350 | ) => { |
| 1351 | $(#[cfg(feature = $std)])? |
| 1352 | fn $name(&self$(, $arg: $t)*) $(-> $ret)? |
| 1353 | { |
| 1354 | <Self>::$name(&self$(, $arg )*) |
| 1355 | } |
| 1356 | |
| 1357 | func!($($tt)*); |
| 1358 | }; |
| 1359 | ( |
| 1360 | $(@$std:literal)? |
| 1361 | $name:ident(&mut self$(, $arg:ident: $t:ty)*) $(-> $ret:ty)?; |
| 1362 | $($tt:tt)* |
| 1363 | ) => { |
| 1364 | $(#[cfg(feature = $std)])? |
| 1365 | fn $name(&mut self$(, $arg: $t)*) $(-> $ret)? |
| 1366 | { |
| 1367 | <Self>::$name(&mut self$(, $arg)*) |
| 1368 | } |
| 1369 | |
| 1370 | func!($($tt)*); |
| 1371 | }; |
| 1372 | ( |
| 1373 | $(@$std:literal)? |
| 1374 | $name:ident($($arg:ident: $t:ty),* $(,)?) $(-> $ret:ty)?; |
| 1375 | $($tt:tt)* |
| 1376 | ) => { |
| 1377 | $(#[cfg(feature = $std)])? |
| 1378 | fn $name($($arg: $t),*) $(-> $ret)? |
| 1379 | { |
| 1380 | <Self>::$name($($arg),*) |
| 1381 | } |
| 1382 | |
| 1383 | func!($($tt)*); |
| 1384 | }; |
| 1385 | () => {}; |
| 1386 | } |
| 1387 | |
| 1388 | macro_rules! impl_for { |
| 1389 | ( Fundamental => $($t:ty => $is_zero:expr),+ $(,)? ) => { $( |
| 1390 | impl Fundamental for $t { |
| 1391 | #[inline(always)] |
| 1392 | #[allow(clippy::redundant_closure_call)] |
| 1393 | fn as_bool(self) -> bool { ($is_zero)(self) } |
| 1394 | |
| 1395 | #[inline(always)] |
| 1396 | fn as_char(self) -> Option<char> { |
| 1397 | core::char::from_u32(self as u32) |
| 1398 | } |
| 1399 | |
| 1400 | #[inline(always)] |
| 1401 | fn as_i8(self) -> i8 { self as i8 } |
| 1402 | |
| 1403 | #[inline(always)] |
| 1404 | fn as_i16(self) -> i16 { self as i16 } |
| 1405 | |
| 1406 | #[inline(always)] |
| 1407 | fn as_i32(self) -> i32 { self as i32 } |
| 1408 | |
| 1409 | #[inline(always)] |
| 1410 | fn as_i64(self) -> i64 { self as i64 } |
| 1411 | |
| 1412 | #[inline(always)] |
| 1413 | fn as_i128(self) -> i128 { self as i128 } |
| 1414 | |
| 1415 | #[inline(always)] |
| 1416 | fn as_isize(self) -> isize { self as isize } |
| 1417 | |
| 1418 | #[inline(always)] |
| 1419 | fn as_u8(self) -> u8 { self as u8 } |
| 1420 | |
| 1421 | #[inline(always)] |
| 1422 | fn as_u16(self) -> u16 { self as u16 } |
| 1423 | |
| 1424 | #[inline(always)] |
| 1425 | fn as_u32(self) -> u32 { self as u32 } |
| 1426 | |
| 1427 | #[inline(always)] |
| 1428 | fn as_u64(self) -> u64 { self as u64 } |
| 1429 | |
| 1430 | #[inline(always)] |
| 1431 | fn as_u128(self) ->u128 { self as u128 } |
| 1432 | |
| 1433 | #[inline(always)] |
| 1434 | fn as_usize(self) -> usize { self as usize } |
| 1435 | |
| 1436 | #[inline(always)] |
| 1437 | fn as_f32(self) -> f32 { self as u32 as f32 } |
| 1438 | |
| 1439 | #[inline(always)] |
| 1440 | fn as_f64(self) -> f64 { self as u64 as f64 } |
| 1441 | } |
| 1442 | )+ }; |
| 1443 | ( Numeric => $($t:ty),+ $(,)? ) => { $( |
| 1444 | impl Numeric for $t { |
| 1445 | type Bytes = [u8; core::mem::size_of::<Self>()]; |
| 1446 | |
| 1447 | func! { |
| 1448 | to_be_bytes(self) -> Self::Bytes; |
| 1449 | to_le_bytes(self) -> Self::Bytes; |
| 1450 | to_ne_bytes(self) -> Self::Bytes; |
| 1451 | from_be_bytes(bytes: Self::Bytes) -> Self; |
| 1452 | from_le_bytes(bytes: Self::Bytes) -> Self; |
| 1453 | from_ne_bytes(bytes: Self::Bytes) -> Self; |
| 1454 | } |
| 1455 | } |
| 1456 | )+ }; |
| 1457 | ( Integral => $($t:ty),+ $(,)? ) => { $( |
| 1458 | impl Integral for $t { |
| 1459 | const ZERO: Self = 0; |
| 1460 | const ONE: Self = 1; |
| 1461 | const MIN: Self = <Self>::min_value(); |
| 1462 | const MAX: Self = <Self>::max_value(); |
| 1463 | |
| 1464 | const BITS: u32 = <Self>::BITS; |
| 1465 | |
| 1466 | func! { |
| 1467 | min_value() -> Self; |
| 1468 | max_value() -> Self; |
| 1469 | from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>; |
| 1470 | count_ones(self) -> u32; |
| 1471 | count_zeros(self) -> u32; |
| 1472 | leading_zeros(self) -> u32; |
| 1473 | trailing_zeros(self) -> u32; |
| 1474 | leading_ones(self) -> u32; |
| 1475 | trailing_ones(self) -> u32; |
| 1476 | rotate_left(self, n: u32) -> Self; |
| 1477 | rotate_right(self, n: u32) -> Self; |
| 1478 | swap_bytes(self) -> Self; |
| 1479 | reverse_bits(self) -> Self; |
| 1480 | from_be(self) -> Self; |
| 1481 | from_le(self) -> Self; |
| 1482 | to_be(self) -> Self; |
| 1483 | to_le(self) -> Self; |
| 1484 | checked_add(self, rhs: Self) -> Option<Self>; |
| 1485 | checked_sub(self, rhs: Self) -> Option<Self>; |
| 1486 | checked_mul(self, rhs: Self) -> Option<Self>; |
| 1487 | checked_div(self, rhs: Self) -> Option<Self>; |
| 1488 | checked_div_euclid(self, rhs: Self) -> Option<Self>; |
| 1489 | checked_rem(self, rhs: Self) -> Option<Self>; |
| 1490 | checked_rem_euclid(self, rhs: Self) -> Option<Self>; |
| 1491 | checked_neg(self) -> Option<Self>; |
| 1492 | checked_shl(self, rhs: u32) -> Option<Self>; |
| 1493 | checked_shr(self, rhs: u32) -> Option<Self>; |
| 1494 | checked_pow(self, rhs: u32) -> Option<Self>; |
| 1495 | saturating_add(self, rhs: Self) -> Self; |
| 1496 | saturating_sub(self, rhs: Self) -> Self; |
| 1497 | saturating_mul(self, rhs: Self) -> Self; |
| 1498 | saturating_pow(self, rhs: u32) -> Self; |
| 1499 | wrapping_add(self, rhs: Self) -> Self; |
| 1500 | wrapping_sub(self, rhs: Self) -> Self; |
| 1501 | wrapping_mul(self, rhs: Self) -> Self; |
| 1502 | wrapping_div(self, rhs: Self) -> Self; |
| 1503 | wrapping_div_euclid(self, rhs: Self) -> Self; |
| 1504 | wrapping_rem(self, rhs: Self) -> Self; |
| 1505 | wrapping_rem_euclid(self, rhs: Self) -> Self; |
| 1506 | wrapping_neg(self) -> Self; |
| 1507 | wrapping_shl(self, rhs: u32) -> Self; |
| 1508 | wrapping_shr(self, rhs: u32) -> Self; |
| 1509 | wrapping_pow(self, rhs: u32) -> Self; |
| 1510 | overflowing_add(self, rhs: Self) -> (Self, bool); |
| 1511 | overflowing_sub(self, rhs: Self) -> (Self, bool); |
| 1512 | overflowing_mul(self, rhs: Self) -> (Self, bool); |
| 1513 | overflowing_div(self, rhs: Self) -> (Self, bool); |
| 1514 | overflowing_div_euclid(self, rhs: Self) -> (Self, bool); |
| 1515 | overflowing_rem(self, rhs: Self) -> (Self, bool); |
| 1516 | overflowing_rem_euclid(self, rhs: Self) -> (Self, bool); |
| 1517 | overflowing_neg(self) -> (Self, bool); |
| 1518 | overflowing_shl(self, rhs: u32) -> (Self, bool); |
| 1519 | overflowing_shr(self, rhs: u32) -> (Self, bool); |
| 1520 | overflowing_pow(self, rhs: u32) -> (Self, bool); |
| 1521 | pow(self, rhs: u32) -> Self; |
| 1522 | div_euclid(self, rhs: Self) -> Self; |
| 1523 | rem_euclid(self, rhs: Self) -> Self; |
| 1524 | } |
| 1525 | } |
| 1526 | )+ }; |
| 1527 | ( Signed => $($t:ty),+ $(,)? ) => { $( |
| 1528 | impl Signed for $t { |
| 1529 | func! { |
| 1530 | checked_abs(self) -> Option<Self>; |
| 1531 | wrapping_abs(self) -> Self; |
| 1532 | overflowing_abs(self) -> (Self, bool); |
| 1533 | abs(self) -> Self; |
| 1534 | signum(self) -> Self; |
| 1535 | is_positive(self) -> bool; |
| 1536 | is_negative(self) -> bool; |
| 1537 | } |
| 1538 | } |
| 1539 | )+ }; |
| 1540 | ( Unsigned => $($t:ty),+ $(,)? ) => { $( |
| 1541 | impl Unsigned for $t { |
| 1542 | func! { |
| 1543 | is_power_of_two(self) -> bool; |
| 1544 | next_power_of_two(self) -> Self; |
| 1545 | checked_next_power_of_two(self) -> Option<Self>; |
| 1546 | } |
| 1547 | } |
| 1548 | )+ }; |
| 1549 | ( Floating => $($t:ident | $u:ty),+ $(,)? ) => { $( |
| 1550 | impl Floating for $t { |
| 1551 | type Raw = $u; |
| 1552 | |
| 1553 | const RADIX: u32 = core::$t::RADIX; |
| 1554 | const MANTISSA_DIGITS: u32 = core::$t::MANTISSA_DIGITS; |
| 1555 | const DIGITS: u32 = core::$t::DIGITS; |
| 1556 | const EPSILON: Self = core::$t::EPSILON; |
| 1557 | const MIN: Self = core::$t::MIN; |
| 1558 | const MIN_POSITIVE: Self = core::$t::MIN_POSITIVE; |
| 1559 | const MAX: Self = core::$t::MAX; |
| 1560 | const MIN_EXP: i32 = core::$t::MIN_EXP; |
| 1561 | const MAX_EXP: i32 = core::$t::MAX_EXP; |
| 1562 | const MIN_10_EXP: i32 = core::$t::MIN_10_EXP; |
| 1563 | const MAX_10_EXP: i32 = core::$t::MAX_10_EXP; |
| 1564 | const NAN: Self = core::$t::NAN; |
| 1565 | const INFINITY: Self = core::$t::INFINITY; |
| 1566 | const NEG_INFINITY: Self = core::$t::NEG_INFINITY; |
| 1567 | |
| 1568 | const PI: Self = core::$t::consts::PI; |
| 1569 | const FRAC_PI_2: Self = core::$t::consts::FRAC_PI_2; |
| 1570 | const FRAC_PI_3: Self = core::$t::consts::FRAC_PI_3; |
| 1571 | const FRAC_PI_4: Self = core::$t::consts::FRAC_PI_4; |
| 1572 | const FRAC_PI_6: Self = core::$t::consts::FRAC_PI_6; |
| 1573 | const FRAC_PI_8: Self = core::$t::consts::FRAC_PI_8; |
| 1574 | const FRAC_1_PI: Self = core::$t::consts::FRAC_1_PI; |
| 1575 | const FRAC_2_PI: Self = core::$t::consts::FRAC_2_PI; |
| 1576 | const FRAC_2_SQRT_PI: Self = core::$t::consts::FRAC_2_SQRT_PI; |
| 1577 | const SQRT_2: Self = core::$t::consts::SQRT_2; |
| 1578 | const FRAC_1_SQRT_2: Self = core::$t::consts::FRAC_1_SQRT_2; |
| 1579 | const E: Self = core::$t::consts::E; |
| 1580 | const LOG2_E: Self = core::$t::consts::LOG2_E; |
| 1581 | const LOG10_E: Self = core::$t::consts::LOG10_E; |
| 1582 | const LN_2: Self = core::$t::consts::LN_2; |
| 1583 | const LN_10: Self = core::$t::consts::LN_10; |
| 1584 | |
| 1585 | func! { |
| 1586 | @"std" floor(self) -> Self; |
| 1587 | @"std" ceil(self) -> Self; |
| 1588 | @"std" round(self) -> Self; |
| 1589 | @"std" trunc(self) -> Self; |
| 1590 | @"std" fract(self) -> Self; |
| 1591 | @"std" abs(self) -> Self; |
| 1592 | @"std" signum(self) -> Self; |
| 1593 | @"std" copysign(self, sign: Self) -> Self; |
| 1594 | @"std" mul_add(self, a: Self, b: Self) -> Self; |
| 1595 | @"std" div_euclid(self, rhs: Self) -> Self; |
| 1596 | @"std" rem_euclid(self, rhs: Self) -> Self; |
| 1597 | @"std" powi(self, n: i32) -> Self; |
| 1598 | @"std" powf(self, n: Self) -> Self; |
| 1599 | @"std" sqrt(self) -> Self; |
| 1600 | @"std" exp(self) -> Self; |
| 1601 | @"std" exp2(self) -> Self; |
| 1602 | @"std" ln(self) -> Self; |
| 1603 | @"std" log(self, base: Self) -> Self; |
| 1604 | @"std" log2(self) -> Self; |
| 1605 | @"std" log10(self) -> Self; |
| 1606 | @"std" cbrt(self) -> Self; |
| 1607 | @"std" hypot(self, other: Self) -> Self; |
| 1608 | @"std" sin(self) -> Self; |
| 1609 | @"std" cos(self) -> Self; |
| 1610 | @"std" tan(self) -> Self; |
| 1611 | @"std" asin(self) -> Self; |
| 1612 | @"std" acos(self) -> Self; |
| 1613 | @"std" atan(self) -> Self; |
| 1614 | @"std" atan2(self, other: Self) -> Self; |
| 1615 | @"std" sin_cos(self) -> (Self, Self); |
| 1616 | @"std" exp_m1(self) -> Self; |
| 1617 | @"std" ln_1p(self) -> Self; |
| 1618 | @"std" sinh(self) -> Self; |
| 1619 | @"std" cosh(self) -> Self; |
| 1620 | @"std" tanh(self) -> Self; |
| 1621 | @"std" asinh(self) -> Self; |
| 1622 | @"std" acosh(self) -> Self; |
| 1623 | @"std" atanh(self) -> Self; |
| 1624 | is_nan(self) -> bool; |
| 1625 | is_infinite(self) -> bool; |
| 1626 | is_finite(self) -> bool; |
| 1627 | is_normal(self) -> bool; |
| 1628 | classify(self) -> FpCategory; |
| 1629 | is_sign_positive(self) -> bool; |
| 1630 | is_sign_negative(self) -> bool; |
| 1631 | recip(self) -> Self; |
| 1632 | to_degrees(self) -> Self; |
| 1633 | to_radians(self) -> Self; |
| 1634 | max(self, other: Self) -> Self; |
| 1635 | min(self, other: Self) -> Self; |
| 1636 | to_bits(self) -> Self::Raw; |
| 1637 | from_bits(bits: Self::Raw) -> Self; |
| 1638 | } |
| 1639 | } |
| 1640 | )+ }; |
| 1641 | ( $which:ty => $($t:ty),+ $(,)? ) => { $( |
| 1642 | impl $which for $t {} |
| 1643 | )+ }; |
| 1644 | } |
| 1645 | |
| 1646 | impl_for!(Fundamental => |
| 1647 | bool => |this: bool| !this, |
| 1648 | char => |this| this != '\0', |
| 1649 | i8 => |this| this != 0, |
| 1650 | i16 => |this| this != 0, |
| 1651 | i32 => |this| this != 0, |
| 1652 | i64 => |this| this != 0, |
| 1653 | i128 => |this| this != 0, |
| 1654 | isize => |this| this != 0, |
| 1655 | u8 => |this| this != 0, |
| 1656 | u16 => |this| this != 0, |
| 1657 | u32 => |this| this != 0, |
| 1658 | u64 => |this| this != 0, |
| 1659 | u128 => |this| this != 0, |
| 1660 | usize => |this| this != 0, |
| 1661 | f32 => |this: f32| (-Self::EPSILON ..= Self::EPSILON).contains(&this), |
| 1662 | f64 => |this: f64| (-Self::EPSILON ..= Self::EPSILON).contains(&this), |
| 1663 | ); |
| 1664 | impl_for!(Numeric => i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64); |
| 1665 | impl_for!(Integral => i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize); |
| 1666 | impl_for!(Signed => i8, i16, i32, i64, i128, isize); |
| 1667 | impl_for!(Unsigned => u8, u16, u32, u64, u128, usize); |
| 1668 | impl_for!(Floating => f32 | u32, f64 | u64); |
| 1669 | |
| 1670 | impl_for!(Is8 => i8, u8); |
| 1671 | impl_for!(Is16 => i16, u16); |
| 1672 | impl_for!(Is32 => i32, u32, f32); |
| 1673 | impl_for!(Is64 => i64, u64, f64); |
| 1674 | impl_for!(Is128 => i128, u128); |
| 1675 | |
| 1676 | #[cfg(target_pointer_width = "16")] |
| 1677 | impl_for!(Is16 => isize, usize); |
| 1678 | |
| 1679 | #[cfg(target_pointer_width = "32")] |
| 1680 | impl_for!(Is32 => isize, usize); |
| 1681 | |
| 1682 | #[cfg(target_pointer_width = "64")] |
| 1683 | impl_for!(Is64 => isize, usize); |
| 1684 | |
| 1685 | impl_for!(AtLeast8 => i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64); |
| 1686 | impl_for!(AtLeast16 => i16, i32, i64, i128, u16, u32, u64, u128, f32, f64); |
| 1687 | impl_for!(AtLeast32 => i32, i64, i128, u32, u64, u128, f32, f64); |
| 1688 | impl_for!(AtLeast64 => i64, i128, u64, u128, f64); |
| 1689 | impl_for!(AtLeast128 => i128, u128); |
| 1690 | |
| 1691 | #[cfg(any( |
| 1692 | target_pointer_width = "16", |
| 1693 | target_pointer_width = "32", |
| 1694 | target_pointer_width = "64" |
| 1695 | ))] |
| 1696 | impl_for!(AtLeast16 => isize, usize); |
| 1697 | |
| 1698 | #[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] |
| 1699 | impl_for!(AtLeast32 => isize, usize); |
| 1700 | |
| 1701 | #[cfg(target_pointer_width = "64")] |
| 1702 | impl_for!(AtLeast64 => isize, usize); |
| 1703 | |
| 1704 | impl_for!(AtMost8 => i8, u8); |
| 1705 | impl_for!(AtMost16 => i8, i16, u8, u16); |
| 1706 | impl_for!(AtMost32 => i8, i16, i32, u8, u16, u32, f32); |
| 1707 | impl_for!(AtMost64 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, f32, f64); |
| 1708 | impl_for!(AtMost128 => i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64); |
| 1709 | |
| 1710 | #[cfg(target_pointer_width = "16")] |
| 1711 | impl_for!(AtMost16 => isize, usize); |
| 1712 | |
| 1713 | #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))] |
| 1714 | impl_for!(AtMost32 => isize, usize); |
| 1715 | |
| 1716 | #[cfg(test)] |
| 1717 | mod tests { |
| 1718 | use super::*; |
| 1719 | use static_assertions::*; |
| 1720 | |
| 1721 | assert_impl_all!(bool: Fundamental); |
| 1722 | assert_impl_all!(char: Fundamental); |
| 1723 | |
| 1724 | assert_impl_all!(i8: Integral, Signed, Is8); |
| 1725 | assert_impl_all!(i16: Integral, Signed, Is16); |
| 1726 | assert_impl_all!(i32: Integral, Signed, Is32); |
| 1727 | assert_impl_all!(i64: Integral, Signed, Is64); |
| 1728 | assert_impl_all!(i128: Integral, Signed, Is128); |
| 1729 | assert_impl_all!(isize: Integral, Signed); |
| 1730 | |
| 1731 | assert_impl_all!(u8: Integral, Unsigned, Is8); |
| 1732 | assert_impl_all!(u16: Integral, Unsigned, Is16); |
| 1733 | assert_impl_all!(u32: Integral, Unsigned, Is32); |
| 1734 | assert_impl_all!(u64: Integral, Unsigned, Is64); |
| 1735 | assert_impl_all!(u128: Integral, Unsigned, Is128); |
| 1736 | assert_impl_all!(usize: Integral, Unsigned); |
| 1737 | |
| 1738 | assert_impl_all!(f32: Floating, Is32); |
| 1739 | assert_impl_all!(f64: Floating, Is64); |
| 1740 | } |