| 1 | // Copyright 2015-2016 Brian Smith. |
| 2 | // |
| 3 | // Permission to use, copy, modify, and/or distribute this software for any |
| 4 | // purpose with or without fee is hereby granted, provided that the above |
| 5 | // copyright notice and this permission notice appear in all copies. |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
| 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 10 | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | |
| 15 | //! Polyfills for functionality that will (hopefully) be added to Rust's |
| 16 | //! standard library soon. |
| 17 | |
| 18 | #[cfg (any(target_pointer_width = "32" , target_pointer_width = "64" ))] |
| 19 | #[inline (always)] |
| 20 | pub const fn u64_from_usize(x: usize) -> u64 { |
| 21 | x as u64 |
| 22 | } |
| 23 | |
| 24 | #[cfg (any(target_pointer_width = "32" , target_pointer_width = "64" ))] |
| 25 | pub const fn usize_from_u32(x: u32) -> usize { |
| 26 | x as usize |
| 27 | } |
| 28 | |
| 29 | #[cfg (all( |
| 30 | target_arch = "aarch64" , |
| 31 | target_endian = "little" , |
| 32 | target_pointer_width = "64" |
| 33 | ))] |
| 34 | #[allow (clippy::cast_possible_truncation)] |
| 35 | pub fn usize_from_u64(x: u64) -> usize { |
| 36 | x as usize |
| 37 | } |
| 38 | |
| 39 | /// const-capable `x.try_into().unwrap_or(usize::MAX)` |
| 40 | #[allow (clippy::cast_possible_truncation)] |
| 41 | #[inline (always)] |
| 42 | pub const fn usize_from_u64_saturated(x: u64) -> usize { |
| 43 | const USIZE_MAX: u64 = u64_from_usize(usize::MAX); |
| 44 | if x < USIZE_MAX { |
| 45 | x as usize |
| 46 | } else { |
| 47 | usize::MAX |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #[macro_use ] |
| 52 | mod cold_error; |
| 53 | |
| 54 | mod array_flat_map; |
| 55 | mod array_split_map; |
| 56 | |
| 57 | pub mod cstr; |
| 58 | |
| 59 | pub mod sliceutil; |
| 60 | |
| 61 | #[cfg (feature = "alloc" )] |
| 62 | mod leading_zeros_skipped; |
| 63 | |
| 64 | #[cfg (any( |
| 65 | all(target_arch = "aarch64" , target_endian = "little" ), |
| 66 | all(target_arch = "arm" , target_endian = "little" ), |
| 67 | target_arch = "x86" , |
| 68 | target_arch = "x86_64" |
| 69 | ))] |
| 70 | pub mod once_cell { |
| 71 | pub mod race; |
| 72 | } |
| 73 | |
| 74 | mod notsend; |
| 75 | pub mod ptr; |
| 76 | |
| 77 | pub mod slice; |
| 78 | |
| 79 | #[cfg (test)] |
| 80 | mod test; |
| 81 | |
| 82 | mod unwrap_const; |
| 83 | |
| 84 | pub use self::{ |
| 85 | array_flat_map::ArrayFlatMap, array_split_map::ArraySplitMap, notsend::NotSend, |
| 86 | unwrap_const::unwrap_const, |
| 87 | }; |
| 88 | |
| 89 | #[cfg (feature = "alloc" )] |
| 90 | pub use leading_zeros_skipped::LeadingZerosStripped; |
| 91 | |
| 92 | #[cfg (test)] |
| 93 | mod tests { |
| 94 | use super::*; |
| 95 | #[test ] |
| 96 | fn test_usize_from_u64_saturated() { |
| 97 | const USIZE_MAX: u64 = u64_from_usize(usize::MAX); |
| 98 | assert_eq!(usize_from_u64_saturated(u64::MIN), usize::MIN); |
| 99 | assert_eq!(usize_from_u64_saturated(USIZE_MAX), usize::MAX); |
| 100 | assert_eq!(usize_from_u64_saturated(USIZE_MAX - 1), usize::MAX - 1); |
| 101 | |
| 102 | #[cfg (not(target_pointer_width = "64" ))] |
| 103 | { |
| 104 | assert_eq!(usize_from_u64_saturated(USIZE_MAX + 1), usize::MAX); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |