summaryrefslogtreecommitdiffstats
path: root/vendor/cast
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
commit9835e2ae736235810b4ea1c162ca5e65c547e770 (patch)
tree3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/cast
parentReleasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff)
downloadrustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz
rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/cast')
-rw-r--r--vendor/cast/.cargo-checksum.json1
-rw-r--r--vendor/cast/CHANGELOG.md110
-rw-r--r--vendor/cast/Cargo.toml27
-rw-r--r--vendor/cast/LICENSE-APACHE201
-rw-r--r--vendor/cast/LICENSE-MIT25
-rw-r--r--vendor/cast/README.md44
-rw-r--r--vendor/cast/src/lib.rs539
-rw-r--r--vendor/cast/src/test.rs208
8 files changed, 1155 insertions, 0 deletions
diff --git a/vendor/cast/.cargo-checksum.json b/vendor/cast/.cargo-checksum.json
new file mode 100644
index 000000000..ae8e79479
--- /dev/null
+++ b/vendor/cast/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"CHANGELOG.md":"6d46efd1132fccc4f659a6804030d7b54a1dd5ccbd8d24115128190e7e61d0ab","Cargo.toml":"fce66320ad4fa962c38c22656375a2db2666d39d2e7010736cedf98abe95db71","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"005b3f9b4687364514bbaea2a25e96d54ce6a59277d157386e8259cbcae8e095","README.md":"006ae145ee89fa14a9d755b206ec2011a3687408e9d5fe3943f2448767b01d62","src/lib.rs":"b13a11ba18de5f292ff353422103d8397bb72db08cb0f66056ac7c5bcfabf7b8","src/test.rs":"7102cbfa90e00686d3302ece3d42ce2196706dba2b6febbf3a9749c9cd051333"},"package":"37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"} \ No newline at end of file
diff --git a/vendor/cast/CHANGELOG.md b/vendor/cast/CHANGELOG.md
new file mode 100644
index 000000000..6efacb0b2
--- /dev/null
+++ b/vendor/cast/CHANGELOG.md
@@ -0,0 +1,110 @@
+# Change Log
+
+All notable changes to this project will be documented in this file.
+
+The format is based on [Keep a Changelog](http://keepachangelog.com/)
+and this project adheres to [Semantic Versioning](http://semver.org/).
+
+## [Unreleased]
+
+## [v0.3.0] - 2021-09-04
+
+### Changed
+
+- (breaking change) The guaranteed MSRV is now 1.31.0. ([#40])
+- (breaking change) The `std` Cargo feature is no longer enabled by default. ([#44])
+- 128-bit integer support is now always available by default. ([#37])
+
+[#37]: https://github.com/japaric/cast.rs/pull/37
+[#40]: https://github.com/japaric/cast.rs/pull/40
+[#44]: https://github.com/japaric/cast.rs/pull/44
+
+### Fixed
+
+- fixed casting `255f32` to `u8` returning `Error::Overflow` ([#23], [#42])
+- fixed intent of promote-and-back tests ([#39], [#43])
+
+[#23]: https://github.com/japaric/cast.rs/issues/23
+[#39]: https://github.com/japaric/cast.rs/issues/39
+[#42]: https://github.com/japaric/cast.rs/pull/42
+[#43]: https://github.com/japaric/cast.rs/pull/43
+
+### Removed
+
+- (breaking change) The `x128` Cargo feature has been removed
+- removed `rustc_version` and `semver` build dependencies ([#35], [#37])
+- removed all internal use of `unsafe` code ([#41])
+
+[#35]: https://github.com/japaric/cast.rs/issues/35
+[#41]: https://github.com/japaric/cast.rs/pull/41
+
+## [v0.2.7] - 2021-07-03
+
+### Changed
+
+- Bumped `rustc_version` dependency to v0.4.0
+
+## [v0.2.6] - 2021-05-15
+
+### Changed
+
+- Bumped `rustc_version` dependency
+
+## [v0.2.5] - 2021-04-13
+
+### Fixed
+
+- Build on platforms with 32-bit pointers
+
+## [v0.2.4] - 2021-04-11 - YANKED
+
+## [v0.2.3] - 2018-11-17
+
+### Changed
+
+- Documented the guaranteed MSRV to be 1.13
+- The `x128` feature now works on *stable* Rust 1.26+
+
+### Fixed
+
+- Overflow and underflow checks when casting a float to an unsigned integer
+
+## [v0.2.2] - 2017-05-07
+
+### Fixed
+
+- UB in the checked cast from `f32` to `u128`.
+
+## [v0.2.1] - 2017-05-06
+
+### Added
+
+- Support for 128-bit integers, behind the `x128` Cargo feature (nightly
+ needed).
+
+## [v0.2.0] - 2017-02-08
+
+### Added
+
+- Now `cast::Error` implements the `std::error::Error` trait among other traits
+ like `Display`, `Clone`, etc.
+
+### Changed
+
+- [breaking-change] This crate now depends on the `std` crate by default but you
+ can make it `no_std` by opting out of the `std` Cargo feature.
+
+## v0.1.0 - 2016-02-07
+
+Initial release
+
+[Unreleased]: https://github.com/japaric/cast.rs/compare/v0.3.0...HEAD
+[v0.3.0]: https://github.com/japaric/cast.rs/compare/v0.2.7...v0.3.0
+[v0.2.7]: https://github.com/japaric/cast.rs/compare/v0.2.6...v0.2.7
+[v0.2.6]: https://github.com/japaric/cast.rs/compare/v0.2.5...v0.2.6
+[v0.2.5]: https://github.com/japaric/cast.rs/compare/v0.2.4...v0.2.5
+[v0.2.4]: https://github.com/japaric/cast.rs/compare/v0.2.3...v0.2.4
+[v0.2.3]: https://github.com/japaric/cast.rs/compare/v0.2.2...v0.2.3
+[v0.2.2]: https://github.com/japaric/cast.rs/compare/v0.2.1...v0.2.2
+[v0.2.1]: https://github.com/japaric/cast.rs/compare/v0.2.0...v0.2.1
+[v0.2.0]: https://github.com/japaric/cast.rs/compare/v0.1.0...v0.2.0
diff --git a/vendor/cast/Cargo.toml b/vendor/cast/Cargo.toml
new file mode 100644
index 000000000..540e98c80
--- /dev/null
+++ b/vendor/cast/Cargo.toml
@@ -0,0 +1,27 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+edition = "2018"
+name = "cast"
+version = "0.3.0"
+authors = ["Jorge Aparicio <jorge@japaric.io>"]
+description = "Ergonomic, checked cast functions for primitive types"
+documentation = "https://docs.rs/cast"
+keywords = ["checked", "cast", "primitive", "integer", "float"]
+license = "MIT OR Apache-2.0"
+repository = "https://github.com/japaric/cast.rs"
+[dev-dependencies.quickcheck]
+version = "1.0.3"
+
+[features]
+std = []
diff --git a/vendor/cast/LICENSE-APACHE b/vendor/cast/LICENSE-APACHE
new file mode 100644
index 000000000..16fe87b06
--- /dev/null
+++ b/vendor/cast/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/vendor/cast/LICENSE-MIT b/vendor/cast/LICENSE-MIT
new file mode 100644
index 000000000..3c1a4ea66
--- /dev/null
+++ b/vendor/cast/LICENSE-MIT
@@ -0,0 +1,25 @@
+Copyright (c) 2014-2017 Jorge Aparicio
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/vendor/cast/README.md b/vendor/cast/README.md
new file mode 100644
index 000000000..3ed32f197
--- /dev/null
+++ b/vendor/cast/README.md
@@ -0,0 +1,44 @@
+[![crates.io](https://img.shields.io/crates/d/cast.svg)](https://crates.io/crates/cast)
+[![crates.io](https://img.shields.io/crates/v/cast.svg)](https://crates.io/crates/cast)
+
+# `cast`
+
+> Ergonomic, checked cast functions for primitive types
+
+``` rust
+extern crate cast;
+
+// `u8` and `u16` are checked cast functions, use them to cast from any numeric
+// primitive to `u8`/`u16` respectively
+use cast::{u8, u16, Error};
+
+// Infallible operations, like integer promotion, are equivalent to a normal
+// cast with `as`
+assert_eq!(u16(0u8), 0u16);
+
+// Everything else will return a `Result` depending on the success of the
+// operation
+assert_eq!(u8(0u16), Ok(0u8));
+assert_eq!(u8(256u16), Err(Error::Overflow));
+assert_eq!(u8(-1i8), Err(Error::Underflow));
+assert_eq!(u8(1. / 0.), Err(Error::Infinite));
+assert_eq!(u8(0. / 0.), Err(Error::NaN));
+```
+
+## [API docs](https://docs.rs/cast)
+
+## License
+
+Licensed under either of
+
+- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
+ http://www.apache.org/licenses/LICENSE-2.0)
+- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
+
+at your option.
+
+### Contribution
+
+Unless you explicitly state otherwise, any contribution intentionally submitted
+for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
+dual licensed as above, without any additional terms or conditions.
diff --git a/vendor/cast/src/lib.rs b/vendor/cast/src/lib.rs
new file mode 100644
index 000000000..f1db3fce7
--- /dev/null
+++ b/vendor/cast/src/lib.rs
@@ -0,0 +1,539 @@
+//! Ergonomic, checked cast functions for primitive types
+//!
+//! This crate provides one checked cast function for each numeric primitive.
+//! Use these functions to perform a cast from any other numeric primitive:
+//!
+//! ```
+//! use cast::{u8, u16, Error};
+//!
+//! # fn main() {
+//! // Infallible operations, like integer promotion, are equivalent to a normal
+//! // cast with `as`
+//! assert_eq!(u16(0u8), 0u16);
+//!
+//! // Everything else will return a `Result` depending on the success of the
+//! // operation
+//! assert_eq!(u8(0u16), Ok(0u8));
+//! assert_eq!(u8(256u16), Err(Error::Overflow));
+//! assert_eq!(u8(-1i8), Err(Error::Underflow));
+//! assert_eq!(u8(1. / 0.), Err(Error::Infinite));
+//! assert_eq!(u8(0. / 0.), Err(Error::NaN));
+//! # }
+//! ```
+//!
+//! There are no namespace problems between these functions, the "primitive
+//! modules" in `core`/`std` and the built-in primitive types, so all them can
+//! be in the same scope:
+//!
+//! ```
+//! use std::u8;
+//! use cast::{u8, u16};
+//!
+//! # fn main() {
+//! // `u8` as a type
+//! let x: u8 = 0;
+//! // `u8` as a module
+//! let y = u16(u8::MAX);
+//! // `u8` as a function
+//! let z = u8(y).unwrap();
+//! # }
+//! ```
+//!
+//! The checked cast functionality is also usable with type aliases via the
+//! `cast` static method:
+//!
+//! ```
+//! use std::os::raw::c_ulonglong;
+//! // NOTE avoid shadowing `std::convert::From` - cf. rust-lang/rfcs#1311
+//! use cast::From as _0;
+//!
+//! # fn main() {
+//! assert_eq!(c_ulonglong::cast(0u8), 0u64);
+//! # }
+//! ```
+//!
+//! This crate also provides a `From` trait that can be used, for example,
+//! to create a generic function that accepts any type that can be infallibly
+//! casted to `u32`.
+//!
+//! ```
+//! fn to_u32<T>(x: T) -> u32
+//! // reads as: "where u32 can be casted from T with output u32"
+//! where u32: cast::From<T, Output=u32>,
+//! {
+//! cast::u32(x)
+//! }
+//!
+//! # fn main() {
+//! assert_eq!(to_u32(0u8), 0u32);
+//! assert_eq!(to_u32(1u16), 1u32);
+//! assert_eq!(to_u32(2u32), 2u32);
+//!
+//! // to_u32(-1i32); // Compile error
+//! # }
+//! ```
+//!
+//! ## Minimal Supported Rust Version
+//!
+//! This crate is guaranteed to compile *as a dependency* on stable Rust 1.31 and up.
+//! It's not guaranteed that `cargo test`-ing this crate follows the MSRV.
+//! It *might* compile on older versions but that may change in any new patch release.
+//!
+//! ## Building without `std`
+//!
+//! This crate can be used without Rust's `std` crate by declaring it as
+//! follows in your `Cargo.toml`:
+//!
+//! ``` toml
+//! cast = { version = "*", default-features = false }
+//! ```
+
+#![allow(const_err)]
+#![cfg_attr(not(feature = "std"), no_std)]
+#![deny(missing_docs)]
+#![deny(unsafe_code)]
+#![deny(warnings)]
+
+#[cfg(test)]
+#[macro_use]
+extern crate quickcheck;
+
+use core::fmt;
+#[cfg(feature = "std")]
+use std::error;
+
+#[cfg(test)]
+mod test;
+
+/// Cast errors
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub enum Error {
+ /// Infinite value casted to a type that can only represent finite values
+ Infinite,
+ /// NaN value casted to a type that can't represent a NaN value
+ NaN,
+ /// Source value is greater than the maximum value that the destination type
+ /// can hold
+ Overflow,
+ /// Source value is smaller than the minimum value that the destination type
+ /// can hold
+ Underflow,
+}
+
+impl Error {
+ /// A private helper function that implements `description`, because
+ /// `description` is only available when we have `std` enabled.
+ fn description_helper(&self) -> &str {
+ match *self {
+ Error::Infinite => "Cannot store infinite value in finite type",
+ Error::NaN => "Cannot store NaN in type which does not support it",
+ Error::Overflow => "Overflow during numeric conversion",
+ Error::Underflow => "Underflow during numeric conversion",
+ }
+ }
+}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", self.description_helper())
+ }
+}
+
+#[cfg(feature = "std")]
+impl error::Error for Error {
+ fn description(&self) -> &str {
+ self.description_helper()
+ }
+}
+
+/// The "cast from" operation
+pub trait From<Src> {
+ /// The result of the cast operation: either `Self` or `Result<Self, Error>`
+ type Output;
+
+ /// Checked cast from `Src` to `Self`
+ fn cast(_: Src) -> Self::Output;
+}
+
+macro_rules! fns {
+ ($($ty:ident),+) => {
+ $(
+ /// Checked cast function
+ #[inline]
+ pub fn $ty<T>(x: T) -> <$ty as From<T>>::Output
+ where $ty: From<T>
+ {
+ <$ty as From<T>>::cast(x)
+ }
+ )+
+ }
+}
+
+fns!(f32, f64, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
+
+fns!(i128, u128);
+
+/// `$dst` can hold any value of `$src`
+macro_rules! promotion {
+ ($($src:ty => $($dst: ty),+);+;) => {
+ $(
+ $(
+ impl From<$src> for $dst {
+ type Output = $dst;
+
+ #[inline]
+ fn cast(src: $src) -> $dst {
+ src as $dst
+ }
+ }
+ )+
+ )+
+ }
+}
+
+/// `$dst` can hold any positive value of `$src`
+macro_rules! half_promotion {
+ ($($src:ty => $($dst:ty),+);+;) => {
+ $(
+ $(
+ impl From<$src> for $dst {
+ type Output = Result<$dst, Error>;
+
+ #[inline]
+ fn cast(src: $src) -> Self::Output {
+ if src < 0 {
+ Err(Error::Underflow)
+ } else {
+ Ok(src as $dst)
+ }
+ }
+ }
+ )+
+ )+
+ }
+}
+
+/// From an unsigned `$src` to a smaller `$dst`
+macro_rules! from_unsigned {
+ ($($src:ident => $($dst:ident),+);+;) => {
+ $(
+ $(
+ impl From<$src> for $dst {
+ type Output = Result<$dst, Error>;
+
+ #[inline]
+ fn cast(src: $src) -> Self::Output {
+ use core::$dst;
+
+ if src > $dst::MAX as $src {
+ Err(Error::Overflow)
+ } else {
+ Ok(src as $dst)
+ }
+ }
+ }
+ )+
+ )+
+ }
+}
+
+/// From a signed `$src` to a smaller `$dst`
+macro_rules! from_signed {
+ ($($src:ident => $($dst:ident),+);+;) => {
+ $(
+ $(
+ impl From<$src> for $dst {
+ type Output = Result<$dst, Error>;
+
+ #[inline]
+ fn cast(src: $src) -> Self::Output {
+ use core::$dst;
+
+ Err(if src < $dst::MIN as $src {
+ Error::Underflow
+ } else if src > $dst::MAX as $src {
+ Error::Overflow
+ } else {
+ return Ok(src as $dst);
+ })
+ }
+ }
+ )+
+ )+
+ }
+}
+
+/// From a float `$src` to an integer `$dst`
+macro_rules! from_float {
+ ($($src:ident => $($dst:ident),+);+;) => {
+ $(
+ $(
+ impl From<$src> for $dst {
+ type Output = Result<$dst, Error>;
+
+ #[inline]
+ fn cast(src: $src) -> Self::Output {
+ use core::{$dst, $src};
+
+ Err(if src != src {
+ Error::NaN
+ } else if src == $src::INFINITY ||
+ src == $src::NEG_INFINITY {
+ Error::Infinite
+ } else if {
+ // this '$dst::BITS' works on 1.31.0 (MSRV)
+ let dst_bits = core::mem::size_of::<$dst>() as u32 * 8;
+ let lossless = dst_bits < core::$src::MANTISSA_DIGITS;
+
+ let max = if lossless {
+ $dst::MAX as $src
+ } else {
+ // we subtract 1 ULP (unit of least precision) here because some
+ // lossy conversions like `u64::MAX as f64` round *up* and we want
+ // to avoid the check below evaluating to false in that case
+ $src::from_bits(($dst::MAX as $src).to_bits() - 1)
+ };
+
+ src > max
+ } {
+ Error::Overflow
+ } else if $dst::MIN == 0 {
+ // when casting to unsigned integer, negative values close to 0 but
+ // larger than 1.0 should be truncated to 0; this behavior matches
+ // casting from a float to a signed integer
+ if src <= -1.0 {
+ Error::Underflow
+ } else {
+ return Ok(src as $dst);
+ }
+ } else if src < $dst::MIN as $src {
+ Error::Underflow
+ } else {
+ return Ok(src as $dst);
+ })
+ }
+ }
+ )+
+ )+
+ }
+}
+
+/// From a float `$src` to an integer `$dst`, where $dst is large enough to contain
+/// all values of `$src`. We can't ever overflow here
+macro_rules! from_float_dst {
+ ($($src:ident => $($dst:ident),+);+;) => {
+ $(
+ $(
+ impl From<$src> for $dst {
+ type Output = Result<$dst, Error>;
+
+ #[inline]
+ #[allow(unused_comparisons)]
+ fn cast(src: $src) -> Self::Output {
+ use core::{$dst, $src};
+
+ Err(if src != src {
+ Error::NaN
+ } else if src == $src::INFINITY ||
+ src == $src::NEG_INFINITY {
+ Error::Infinite
+ } else if ($dst::MIN == 0) && src <= -1.0 {
+ Error::Underflow
+ } else {
+ return Ok(src as $dst);
+ })
+ }
+ }
+ )+
+ )+
+ }
+}
+
+// PLAY TETRIS! ;-)
+
+#[cfg(target_pointer_width = "32")]
+mod _32 {
+ use crate::{Error, From};
+
+ // Signed
+ promotion! {
+ i8 => f32, f64, i8, i16, i32, isize, i64;
+ i16 => f32, f64, i16, i32, isize, i64;
+ i32 => f32, f64, i32, isize, i64;
+ isize => f32, f64, i32, isize, i64;
+ i64 => f32, f64, i64;
+ }
+
+ half_promotion! {
+ i8 => u8, u16, u32, usize, u64;
+ i16 => u16, u32, usize, u64;
+ i32 => u32, usize, u64;
+ isize => u32, usize, u64;
+ i64 => u64;
+ }
+
+ from_signed! {
+
+ i16 => i8, u8;
+ i32 => i8, i16, u8, u16;
+ isize => i8, i16, u8, u16;
+ i64 => i8, i16, i32, isize, u8, u16, u32, usize;
+ }
+
+ // Unsigned
+ promotion! {
+ u8 => f32, f64, i16, i32, isize, i64, u8, u16, u32, usize, u64;
+ u16 => f32, f64, i32, isize, i64, u16, u32, usize, u64;
+ u32 => f32, f64, i64, u32, usize, u64;
+ usize => f32, f64, i64, u32, usize, u64;
+ u64 => f32, f64, u64;
+ }
+
+ from_unsigned! {
+ u8 => i8;
+ u16 => i8, i16, u8;
+ u32 => i8, i16, i32, isize, u8, u16;
+ usize => i8, i16, i32, isize, u8, u16;
+ u64 => i8, i16, i32, isize, i64, u8, u16, u32, usize;
+ }
+
+ // Float
+ promotion! {
+ f32 => f32, f64;
+ f64 => f64;
+ }
+
+ from_float! {
+ f32 => i8, i16, i32, isize, i64, u8, u16, u32, usize, u64;
+ f64 => i8, i16, i32, isize, i64, u8, u16, u32, usize, u64;
+ }
+}
+
+#[cfg(target_pointer_width = "64")]
+mod _64 {
+ use crate::{Error, From};
+
+ // Signed
+ promotion! {
+ i8 => f32, f64, i8, i16, i32, i64, isize;
+ i16 => f32, f64, i16, i32, i64, isize;
+ i32 => f32, f64, i32, i64, isize;
+ i64 => f32, f64, i64, isize;
+ isize => f32, f64, i64, isize;
+ }
+
+ half_promotion! {
+ i8 => u8, u16, u32, u64, usize;
+ i16 => u16, u32, u64, usize;
+ i32 => u32, u64, usize;
+ i64 => u64, usize;
+ isize => u64, usize;
+ }
+
+ from_signed! {
+
+ i16 => i8, u8;
+ i32 => i8, i16, u8, u16;
+ i64 => i8, i16, i32, u8, u16, u32;
+ isize => i8, i16, i32, u8, u16, u32;
+ }
+
+ // Unsigned
+ promotion! {
+ u8 => f32, f64, i16, i32, i64, isize, u8, u16, u32, u64, usize;
+ u16 => f32, f64, i32, i64, isize, u16, u32, u64, usize;
+ u32 => f32, f64, i64, isize, u32, u64, usize;
+ u64 => f32, f64, u64, usize;
+ usize => f32, f64, u64, usize;
+ }
+
+ from_unsigned! {
+ u8 => i8;
+ u16 => i8, i16, u8;
+ u32 => i8, i16, i32, u8, u16;
+ u64 => i8, i16, i32, i64, isize, u8, u16, u32;
+ usize => i8, i16, i32, i64, isize, u8, u16, u32;
+ }
+
+ // Float
+ promotion! {
+ f32 => f32, f64;
+ f64 => f64;
+ }
+
+ from_float! {
+ f32 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize;
+ f64 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize;
+ }
+}
+
+mod _x128 {
+ use crate::{Error, From};
+
+ // Signed
+ promotion! {
+ i8 => i128;
+ i16 => i128;
+ i32 => i128;
+ i64 => i128;
+ isize => i128;
+ i128 => f32, f64, i128;
+ }
+
+ half_promotion! {
+ i8 => u128;
+ i16 => u128;
+ i32 => u128;
+ i64 => u128;
+ isize => u128;
+ i128 => u128;
+ }
+
+ from_signed! {
+ i128 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize;
+ }
+
+ // Unsigned
+ promotion! {
+ u8 => i128, u128;
+ u16 => i128, u128;
+ u32 => i128, u128;
+ u64 => i128, u128;
+ usize => i128, u128;
+ u128 => f64, u128;
+ }
+
+ from_unsigned! {
+ u128 => f32, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, usize;
+ }
+
+ // Float
+ from_float_dst! {
+ f32 => u128;
+ }
+
+ from_float! {
+ f32 => i128;
+ f64 => i128, u128;
+ }
+}
+
+// The missing piece
+impl From<f64> for f32 {
+ type Output = Result<f32, Error>;
+
+ #[inline]
+ fn cast(src: f64) -> Self::Output {
+ use core::{f32, f64};
+
+ if src != src || src == f64::INFINITY || src == f64::NEG_INFINITY {
+ Ok(src as f32)
+ } else if src < f32::MIN as f64 {
+ Err(Error::Underflow)
+ } else if src > f32::MAX as f64 {
+ Err(Error::Overflow)
+ } else {
+ Ok(src as f32)
+ }
+ }
+}
diff --git a/vendor/cast/src/test.rs b/vendor/cast/src/test.rs
new file mode 100644
index 000000000..161a0220e
--- /dev/null
+++ b/vendor/cast/src/test.rs
@@ -0,0 +1,208 @@
+// If `src` can be promoted to `$dst`, then it must be Ok to cast `dst` back to
+// `$src`
+macro_rules! promote_and_back {
+ ($($src:ident => $($dst:ident),+);+;) => {
+ mod demoting_to {
+ $(
+ mod $src {
+ mod from {
+ use crate::From;
+
+ $(
+ quickcheck! {
+ fn $dst(src: $src) -> bool {
+ $src::cast($dst::cast(src)).is_ok()
+ }
+ }
+ )+
+ }
+ }
+ )+
+ }
+ }
+}
+
+#[cfg(target_pointer_width = "32")]
+promote_and_back! {
+ i8 => f32, f64, i16, i32, isize, i64, i128 ;
+ i16 => f32, f64, i32, isize, i64, i128 ;
+ i32 => f64, i64, i128 ;
+ isize => f64, i64, i128 ;
+ i64 => i128 ;
+ u8 => f32, f64, i16, i32, isize, i64, i128, u16, u32, usize, u64, u128;
+ u16 => f32, f64, i32, isize, i64, i128, u32, usize, u64, u128;
+ u32 => f64, i64, i128, u64, u128;
+ usize => f64, i64, i128, u64, u128;
+ u64 => i128, u128;
+}
+
+#[cfg(target_pointer_width = "64")]
+promote_and_back! {
+ i8 => f32, f64, i16, i32, i64, isize, i128 ;
+ i16 => f32, f64, i32, i64, isize, i128 ;
+ i32 => f64, i64, isize, i128 ;
+ i64 => i128 ;
+ isize => i128 ;
+ u8 => f32, f64, i16, i32, i64, isize, i128, u16, u32, u64, usize, u128;
+ u16 => f32, f64, i32, i64, isize, i128, u32, u64, usize, u128;
+ u32 => f64, i64, isize, i128, u64, usize, u128;
+ u64 => i128, u128;
+ usize => i128, u128;
+}
+
+// If it's Ok to cast `src` to `$dst`, it must also be Ok to cast `dst` back to
+// `$src`
+macro_rules! symmetric_cast_between {
+ ($($src:ident => $($dst:ident),+);+;) => {
+ mod symmetric_cast_between {
+ $(
+ mod $src {
+ mod and {
+ use quickcheck::TestResult;
+
+ use crate::From;
+
+ $(
+ quickcheck! {
+ fn $dst(src: $src) -> TestResult {
+ if let Ok(dst) = $dst::cast(src) {
+ TestResult::from_bool(
+ $src::cast(dst).is_ok())
+ } else {
+ TestResult::discard()
+ }
+ }
+ }
+ )+
+ }
+ }
+ )+
+ }
+ }
+}
+
+#[cfg(target_pointer_width = "32")]
+symmetric_cast_between! {
+ u8 => i8 ;
+ u16 => i8, i16 ;
+ u32 => i8, i16, i32 ;
+ usize => i8, i16, i32 ;
+ u64 => i8, i16, i32, i64, isize;
+}
+
+#[cfg(target_pointer_width = "64")]
+symmetric_cast_between! {
+ u8 => i8 ;
+ u16 => i8, i16 ;
+ u32 => i8, i16, i32 ;
+ u64 => i8, i16, i32, i64, isize ;
+ usize => i8, i16, i32, i64, isize ;
+ u128 => i8, i16, i32, i64, isize, i128;
+}
+
+macro_rules! from_float {
+ ($($src:ident => $($dst:ident),+);+;) => {
+ $(
+ mod $src {
+ mod inf {
+ mod to {
+ use crate::{Error, From};
+
+ $(
+ #[test]
+ fn $dst() {
+ let _0: $src = 0.;
+ let _1: $src = 1.;
+ let inf = _1 / _0;
+ let neg_inf = -_1 / _0;
+
+ assert_eq!($dst::cast(inf),
+ Err(Error::Infinite));
+ assert_eq!($dst::cast(neg_inf),
+ Err(Error::Infinite));
+ }
+ )+
+ }
+ }
+
+ mod nan {
+ mod to {
+ use crate::{Error, From};
+
+ $(
+ #[test]
+ fn $dst() {
+ let _0: $src = 0.;
+ let nan = _0 / _0;
+
+ assert_eq!($dst::cast(nan),
+ Err(Error::NaN));
+ }
+ )+
+ }
+ }
+ }
+ )+
+ }
+}
+
+from_float! {
+ f32 => i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize;
+ f64 => i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize;
+}
+
+#[test]
+fn test_fl_conversion() {
+ use crate::u128;
+ assert_eq!(u128(42.0f32), Ok(42));
+}
+
+#[test]
+fn gh16() {
+ assert_eq!(super::u64(-0.01_f64), Ok(0));
+ assert_eq!(super::u64(-0.99_f32), Ok(0));
+
+ assert_eq!(super::u32(-0.99_f64), Ok(0));
+ assert_eq!(super::u32(-0.01_f32), Ok(0));
+
+ assert_eq!(super::u64(0.01_f64), Ok(0));
+ assert_eq!(super::u64(0.99_f32), Ok(0));
+
+ assert_eq!(super::u32(0.99_f64), Ok(0));
+ assert_eq!(super::u32(0.01_f32), Ok(0));
+}
+
+#[test]
+fn gh15() {
+ assert_eq!(super::u32(32_f32.exp2()), Err(super::Error::Overflow));
+ assert_eq!(super::u32(32_f64.exp2()), Err(super::Error::Overflow));
+
+ assert_eq!(super::u64(64_f32.exp2()), Err(super::Error::Overflow));
+ assert_eq!(super::u64(64_f64.exp2()), Err(super::Error::Overflow));
+
+ assert_eq!(super::u8(8_f32.exp2()), Err(super::Error::Overflow));
+ assert_eq!(super::u8(8_f64.exp2()), Err(super::Error::Overflow));
+
+ assert_eq!(super::u16(16_f32.exp2()), Err(super::Error::Overflow));
+ assert_eq!(super::u16(16_f64.exp2()), Err(super::Error::Overflow));
+}
+
+#[test]
+fn gh23_lossless_integer_max_min_to_float() {
+ // f32::MANTISSA_DIGITS = 24
+ assert_eq!(Ok(u8::MAX), super::u8(255f32));
+ assert_eq!(Ok(u16::MAX), super::u16(65_535f32));
+
+ // f64::MANTISSA_DIGITS = 53
+ assert_eq!(Ok(u8::MAX), super::u8(255f64));
+ assert_eq!(Ok(u16::MAX), super::u16(65_535f64));
+ assert_eq!(Ok(u32::MAX), super::u32(4_294_967_295f64));
+
+ // also check negative values (not part of the original bug)
+ assert_eq!(Ok(i8::MIN), super::i8(-128f32));
+ assert_eq!(Ok(i16::MIN), super::i16(-32_768f32));
+
+ assert_eq!(Ok(i8::MIN), super::i8(-128f64));
+ assert_eq!(Ok(i16::MIN), super::i16(-32_768f64));
+ assert_eq!(Ok(i32::MIN), super::i32(-2_147_483_648f64));
+}