ring/
polyfill.rs

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 AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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)]
20pub 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"))]
25pub 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)]
35pub 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)]
42pub 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]
52mod cold_error;
53
54mod array_flat_map;
55mod array_split_map;
56
57pub mod cstr;
58
59pub mod sliceutil;
60
61#[cfg(feature = "alloc")]
62mod 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))]
70pub mod once_cell {
71    pub mod race;
72}
73
74mod notsend;
75pub mod ptr;
76
77pub mod slice;
78
79#[cfg(test)]
80mod test;
81
82mod unwrap_const;
83
84pub use self::{
85    array_flat_map::ArrayFlatMap, array_split_map::ArraySplitMap, notsend::NotSend,
86    unwrap_const::unwrap_const,
87};
88
89#[cfg(feature = "alloc")]
90pub use leading_zeros_skipped::LeadingZerosStripped;
91
92#[cfg(test)]
93mod 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}