az-1.2.1/.cargo_vcs_info.json0000644000000001360000000000100114470ustar { "git": { "sha1": "defb12cccc0a806721fdd091eb724d7227d6b480" }, "path_in_vcs": "" }az-1.2.1/.gitignore000064400000000000000000000000230072674642500122520ustar 00000000000000/target Cargo.lock az-1.2.1/Cargo.toml0000644000000014670000000000100074550ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "az" version = "1.2.1" description = "Casts and checked casts" documentation = "https://docs.rs/az" readme = "README.md" keywords = [ "cast", "checked", "integer", "float", ] categories = ["no-std"] license = "MIT/Apache-2.0" repository = "https://gitlab.com/tspiteri/az" [features] fail-on-warnings = [] az-1.2.1/Cargo.toml.orig000064400000000000000000000011450072674642500131570ustar 00000000000000# Copyright © 2019–2021 Trevor Spiteri # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. This file is offered as-is, # without any warranty. [package] name = "az" version = "1.2.1" edition = "2018" description = "Casts and checked casts" documentation = "https://docs.rs/az" repository = "https://gitlab.com/tspiteri/az" readme = "README.md" keywords = ["cast", "checked", "integer", "float"] categories = ["no-std"] license = "MIT/Apache-2.0" [features] fail-on-warnings = [] az-1.2.1/LICENSE-APACHE000064400000000000000000000251370072674642500122230ustar 00000000000000 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. az-1.2.1/LICENSE-MIT000064400000000000000000000020500072674642500117200ustar 00000000000000Copyright © 2019–2021 Trevor Spiteri 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. az-1.2.1/README.md000064400000000000000000000122760072674642500115560ustar 00000000000000 # Numeric casts This crate provides casts and checked casts. # What’s new ### Version 1.2.1 news (2022-07-25) * Fix build issue using rustc 1.64.0-nightly under Windows. ### Version 1.2.0 news (2021-11-24) * The following traits were added, which can be used for constraints in the opposite direction to the other cast traits. * [CastFrom][cf-1-2] * [CheckedCastFrom][ccf-1-2] * [SaturatingCastFrom][scf-1-2] * [WrappingCastFrom][wcf-1-2] * [OverflowingCastFrom][ocf-1-2] * [UnwrappedCastFrom][ucf-1-2] [ccf-1-2]: https://docs.rs/az/~1.2/az/trait.CheckedCastFrom.html [cf-1-2]: https://docs.rs/az/~1.2/az/trait.CastFrom.html [ocf-1-2]: https://docs.rs/az/~1.2/az/trait.OverflowingCastFrom.html [scf-1-2]: https://docs.rs/az/~1.2/az/trait.SaturatingCastFrom.html [ucf-1-2]: https://docs.rs/az/~1.2/az/trait.UnwrappedCastFrom.html [wcf-1-2]: https://docs.rs/az/~1.2/az/trait.WrappingCastFrom.html ### Other releases Details on other releases can be found in [*RELEASES.md*]. [*RELEASES.md*]: https://gitlab.com/tspiteri/az/blob/master/RELEASES.md ## Quick examples ```rust use az::{Az, OverflowingAs, WrappingAs}; use core::num::Wrapping; // Panics on overflow with `debug_assertions`, otherwise wraps assert_eq!(12i32.az::(), 12u32); // Always wraps let wrapped = 1u32.wrapping_neg(); assert_eq!((-1).wrapping_as::(), wrapped); assert_eq!((-1).overflowing_as::(), (wrapped, true)); // Wrapping can also be obtained using `Wrapping` assert_eq!((-1).az::>().0, wrapped); ``` Conversions from floating-point to integers are also supported. Numbers are rounded towards zero, but the [`Round`] wrapper can be used to convert floating-point numbers to integers with rounding to the nearest, with ties rounded to even. ```rust use az::{Az, CheckedAs, Round, SaturatingAs}; use core::f32; assert_eq!(15.7.az::(), 15); assert_eq!(Round(15.5).az::(), 16); assert_eq!(1.5e20.saturating_as::(), i32::max_value()); assert_eq!(f32::NAN.checked_as::(), None); ``` ## Implementing casts for other types To provide casts for another type, you should implement the [`Cast`] trait and if necessary the [`CheckedCast`], [`SaturatingCast`], [`WrappingCast`], [`OverflowingCast`] and [`UnwrappedCast`] traits. The [`Az`], [`CheckedAs`], [`SaturatingAs`], [`WrappingAs`], [`OverflowingAs`] and [`UnwrappedAs`] traits are already implemented for all types using blanket implementations that make use of the former traits. The cast traits can also be implemented for references. This can be useful for expensive types that are not [`Copy`]. For example if you have your own integer type that does not implement [`Copy`], you could implement casts like in the following example. (`I` could be an expensive type, for example a bignum integer, but for the example it is only a wrapped [`i32`].) ```rust use az::{Az, Cast}; use core::borrow::Borrow; struct I(i32); impl Cast for &'_ I { fn cast(self) -> i64 { self.0.cast() } } let owned = I(12); assert_eq!(owned.borrow().az::(), 12); ``` ## Using the *az* crate The *az* crate is available on [crates.io][*az* crate]. To use it in your crate, add it as a dependency inside [*Cargo.toml*]: ```toml [dependencies] az = "1.2" ``` The crate requires rustc version 1.31.0 or later. ## License This crate is free software: you can redistribute it and/or modify it under the terms of either * the [Apache License, Version 2.0][LICENSE-APACHE] or * the [MIT License][LICENSE-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 License, Version 2.0, shall be dual licensed as above, without any additional terms or conditions. [*Cargo.toml*]: https://doc.rust-lang.org/cargo/guide/dependencies.html [*az* crate]: https://crates.io/crates/az [LICENSE-APACHE]: https://www.apache.org/licenses/LICENSE-2.0 [LICENSE-MIT]: https://opensource.org/licenses/MIT [`Az`]: https://docs.rs/az/~1.2/az/trait.Az.html [`Cast`]: https://docs.rs/az/~1.2/az/trait.Cast.html [`CheckedAs`]: https://docs.rs/az/~1.2/az/trait.CheckedAs.html [`CheckedCast`]: https://docs.rs/az/~1.2/az/trait.CheckedCast.html [`Copy`]: https://doc.rust-lang.org/nightly/core/marker/trait.Copy.html [`OverflowingAs`]: https://docs.rs/az/~1.2/az/trait.OverflowingAs.html [`OverflowingCast`]: https://docs.rs/az/~1.2/az/trait.OverflowingCast.html [`Round`]: https://docs.rs/az/~1.2/az/struct.Round.html [`SaturatingAs`]: https://docs.rs/az/~1.2/az/trait.SaturatingAs.html [`SaturatingCast`]: https://docs.rs/az/~1.2/az/trait.SaturatingCast.html [`UnwrappedAs`]: https://docs.rs/az/~1.2/az/trait.UnwrappedAs.html [`UnwrappedCast`]: https://docs.rs/az/~1.2/az/trait.UnwrappedCast.html [`WrappingAs`]: https://docs.rs/az/~1.2/az/trait.WrappingAs.html [`WrappingCast`]: https://docs.rs/az/~1.2/az/trait.WrappingCast.html [`i32`]: https://doc.rust-lang.org/nightly/std/primitive.i32.html az-1.2.1/RELEASES.md000064400000000000000000000060370072674642500120220ustar 00000000000000 Version 1.2.1 (2022-07-25) ========================== * Fix build issue using rustc 1.64.0-nightly under Windows. Version 1.2.0 (2021-11-24) ========================== * The following traits were added, which can be used for constraints in the opposite direction to the other cast traits. * [CastFrom][cf-1-2] * [CheckedCastFrom][ccf-1-2] * [SaturatingCastFrom][scf-1-2] * [WrappingCastFrom][wcf-1-2] * [OverflowingCastFrom][ocf-1-2] * [UnwrappedCastFrom][ucf-1-2] [ccf-1-2]: https://docs.rs/az/~1.2/az/trait.CheckedCastFrom.html [cf-1-2]: https://docs.rs/az/~1.2/az/trait.CastFrom.html [ocf-1-2]: https://docs.rs/az/~1.2/az/trait.OverflowingCastFrom.html [scf-1-2]: https://docs.rs/az/~1.2/az/trait.SaturatingCastFrom.html [ucf-1-2]: https://docs.rs/az/~1.2/az/trait.UnwrappedCastFrom.html [wcf-1-2]: https://docs.rs/az/~1.2/az/trait.WrappingCastFrom.html Version 1.1.2 (2021-08-23) ========================== * Now the [`Debug`] implementation for [`Round`][r-1-1] outputs the value only without “`Round()`” around it. Version 1.1.1 (2021-03-25) ========================== * The `track_caller` attribute is now applied to panicking functions if supported by the compiler. Version 1.1.0 (2021-02-03) ========================== * Unwrapped casts were added, which panic on overflow even when debug assertions are not enabled. * [UnwrappedCast][uc-1-1] * [UnwrappedAs][ua-1-1] * [unwrapped_cast][ucf-1-1] [r-1-1]: https://docs.rs/az/~1.1/az/struct.Round.html [ua-1-1]: https://docs.rs/az/~1.1/az/trait.UnwrappedAs.html [uc-1-1]: https://docs.rs/az/~1.1/az/trait.UnwrappedCast.html [ucf-1-1]: https://docs.rs/az/~1.1/az/fn.unwrapped_cast.html Version 1.0.0 (2020-04-18) ========================== * All deprecated items were removed. Version 0.3.1 (2020-04-17) ========================== * Static casts were deprecated as their use case was unclear. Version 0.3.0 (2019-10-01) ========================== * The behavior of static casts was changed: now they return `Option`, but an implementation should either always return `Some` or always return `None`. * Bug fix: checked casts from floating-point to wrapped integers were panicking for infinite or NaN. Version 0.2.0 (2019-09-10) ========================== * The old `*As` traits were renamed to `*Cast`. * New more convenient `*As` traits were added. Version 0.1.0 (2019-09-09) ========================== * Conversions between integers and floating-point numbers. * Checked, saturating, wrapping and overflowing conversions. * Static conversions when the conversion cannot fail. * Rounding conversions from floating-point numbers to integers using `Round`. [`Debug`]: https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html az-1.2.1/build.rs000064400000000000000000000052620072674642500117410ustar 00000000000000// Copyright © 2019–2021 Trevor Spiteri // Copying and distribution of this file, with or without // modification, are permitted in any medium without royalty provided // the copyright notice and this notice are preserved. This file is // offered as-is, without any warranty. use std::{ env, ffi::OsString, fs::{self, File}, io::{Result as IoResult, Write}, path::{Path, PathBuf}, process::{Command, Stdio}, }; fn main() { check_feature("track_caller", TRY_TRACK_CALLER); } fn check_feature(name: &str, contents: &str) { let rustc = cargo_env("RUSTC"); let out_dir = PathBuf::from(cargo_env("OUT_DIR")); let try_dir = out_dir.join(format!("try_{}", name)); let filename = format!("try_{}.rs", name); create_dir_or_panic(&try_dir); println!("$ cd {:?}", try_dir); create_file_or_panic(&try_dir.join(&filename), contents); let mut cmd = Command::new(&rustc); cmd.current_dir(&try_dir) .stdout(Stdio::null()) .stderr(Stdio::null()) .args(&[&*filename, "--emit=dep-info,metadata"]); println!("$ {:?} >& /dev/null", cmd); let status = cmd .status() .unwrap_or_else(|_| panic!("Unable to execute: {:?}", cmd)); if status.success() { println!("cargo:rustc-cfg={}", name); } if cfg!(target_os = "windows") { // remove_dir_all started to fail on rustc 1.64.0-nightly let _ = remove_dir(&try_dir); } else { remove_dir_or_panic(&try_dir); } } fn cargo_env(name: &str) -> OsString { env::var_os(name) .unwrap_or_else(|| panic!("environment variable not found: {}, please use cargo", name)) } fn remove_dir(dir: &Path) -> IoResult<()> { if !dir.exists() { return Ok(()); } assert!(dir.is_dir(), "Not a directory: {:?}", dir); println!("$ rm -r {:?}", dir); fs::remove_dir_all(dir) } fn remove_dir_or_panic(dir: &Path) { remove_dir(dir).unwrap_or_else(|_| panic!("Unable to remove directory: {:?}", dir)); } fn create_dir(dir: &Path) -> IoResult<()> { println!("$ mkdir -p {:?}", dir); fs::create_dir_all(dir) } fn create_dir_or_panic(dir: &Path) { create_dir(dir).unwrap_or_else(|_| panic!("Unable to create directory: {:?}", dir)); } fn create_file_or_panic(filename: &Path, contents: &str) { println!("$ printf '%s' {:?}... > {:?}", &contents[0..10], filename); let mut file = File::create(filename).unwrap_or_else(|_| panic!("Unable to create file: {:?}", filename)); file.write_all(contents.as_bytes()) .unwrap_or_else(|_| panic!("Unable to write to file: {:?}", filename)); } const TRY_TRACK_CALLER: &str = r#"// try_track_caller.rs #[track_caller] fn _tracked() {} fn main() {} "#; az-1.2.1/etc/appveyor.yml000064400000000000000000000024740072674642500134410ustar 00000000000000# Copyright © 2019–2021 Trevor Spiteri # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. This file is offered as-is, # without any warranty. skip_tags: true environment: matrix: - TARGET: x86_64-pc-windows-msvc - TARGET: x86_64-pc-windows-gnu - TARGET: i686-pc-windows-msvc - TARGET: i686-pc-windows-gnu install: - set PATH=%USERPROFILE%\.cargo\bin;%PATH% - curl -sSf -o rustup-init.exe https://win.rustup.rs - rustup-init.exe -y --default-host %TARGET% --default-toolchain none --no-modify-path - rustup --version - rustup install beta-%TARGET% nightly-%TARGET% 1.31.1-%TARGET% - rustup component add --toolchain beta-%TARGET% rustfmt clippy build: false test_script: - cargo +beta-%TARGET% clippy --all-targets --features fail-on-warnings - cargo +beta-%TARGET% test --features fail-on-warnings - cargo +beta-%TARGET% test --release --features fail-on-warnings - cargo +beta-%TARGET% fmt -- --check - cargo +nightly-%TARGET% test --features fail-on-warnings - cargo +nightly-%TARGET% test --release --features fail-on-warnings - cargo +1.31.1-%TARGET% test --features fail-on-warnings - cargo +1.31.1-%TARGET% test --release --features fail-on-warnings az-1.2.1/etc/gitlab-ci.yml000064400000000000000000000107100072674642500134170ustar 00000000000000# Copyright © 2019–2021 Trevor Spiteri # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. This file is offered as-is, # without any warranty. x86_64-gnulinux: image: amd64/rust:1 variables: TARGET: x86_64 script: - rustup self update - rustup toolchain install --profile minimal {beta,stable,nightly,1.31.1}-$TARGET - rustup component add --toolchain beta-$TARGET rustfmt clippy - cargo +beta-$TARGET clippy --all-targets --features fail-on-warnings - cargo +beta-$TARGET test --features fail-on-warnings - cargo +beta-$TARGET test --release --features fail-on-warnings - cargo +beta-$TARGET fmt -- --check - cargo +stable-$TARGET test --features fail-on-warnings - cargo +stable-$TARGET test --release --features fail-on-warnings - cargo +nightly-$TARGET test --features fail-on-warnings - cargo +nightly-$TARGET test --release --features fail-on-warnings - cargo +1.31.1-$TARGET test --features fail-on-warnings - cargo +1.31.1-$TARGET test --release --features fail-on-warnings i686-gnulinux: image: i386/rust:1 variables: TARGET: i686 script: - rustup self update - rustup toolchain install --profile minimal {beta,stable,nightly,1.31.1}-$TARGET - rustup component add --toolchain beta-$TARGET rustfmt clippy - cargo +beta-$TARGET clippy --all-targets --features fail-on-warnings - cargo +beta-$TARGET test --features fail-on-warnings - cargo +beta-$TARGET test --release --features fail-on-warnings - cargo +beta-$TARGET fmt -- --check - cargo +stable-$TARGET test --features fail-on-warnings - cargo +stable-$TARGET test --release --features fail-on-warnings - cargo +nightly-$TARGET test --features fail-on-warnings - cargo +nightly-$TARGET test --release --features fail-on-warnings - cargo +1.31.1-$TARGET test --features fail-on-warnings - cargo +1.31.1-$TARGET test --release --features fail-on-warnings x86_64-gnulinux-tarpaulin: image: amd64/rust:1 variables: TOOLCHAIN: stable-x86_64 REQ_COVERAGE: "100" cache: key: $CI_JOB_NAME paths: - cargo/ before_script: - rustup self update - rustup toolchain install --profile minimal $TOOLCHAIN - if [ -d cargo/registry/cache ]; then rm -rf $CARGO_HOME/registry/cache; mkdir -p $CARGO_HOME/registry; cp -R cargo/registry/cache $CARGO_HOME/registry/; echo Copied registry/cache; fi - if [ -d $CARGO_HOME/registry/src ]; then rm -r $CARGO_HOME/registry/src; fi - if [ -d $CARGO_HOME/registry/cache ]; then (cd $CARGO_HOME/registry; find cache -name \*.crate | sort) fi after_script: - if [ -d $CARGO_HOME/registry/cache ]; then (cd $CARGO_HOME/registry; for c in cache/*/*.crate; do s=src/${c#cache/}; if [ ! -e ${s%.crate} ]; then rm -v $c; fi; done; find cache -name \*.crate | sort) fi - rm -rf cargo - mkdir -p cargo/registry - if [ -d $CARGO_HOME/registry/cache ]; then cp -R $CARGO_HOME/registry/cache cargo/registry/; echo Updated registry/cache; fi script: - cargo +$TOOLCHAIN install --version 0.20.1 --locked cargo-tarpaulin - stdbuf -oL cargo +$TOOLCHAIN tarpaulin -v --ignore-tests | tee tarpaulin.log - echo "Check that coverage not less than $REQ_COVERAGE%" - tail -1 tarpaulin.log | awk '{ if ($1 < '$REQ_COVERAGE') { exit 1 } }' pages: image: amd64/rust:1 variables: TOOLCHAIN: nightly-x86_64 cache: key: $CI_JOB_NAME paths: - cargo/ before_script: - rustup self update - rustup toolchain install --profile minimal $TOOLCHAIN - if [ -d cargo/registry/cache ]; then rm -rf $CARGO_HOME/registry/cache; mkdir -p $CARGO_HOME/registry; cp -R cargo/registry/cache $CARGO_HOME/registry/; echo Copied registry/cache; fi - if [ -d $CARGO_HOME/registry/src ]; then rm -r $CARGO_HOME/registry/src; fi - if [ -d $CARGO_HOME/registry/cache ]; then (cd $CARGO_HOME/registry; find cache -name \*.crate | sort) fi after_script: - if [ -d $CARGO_HOME/registry/cache ]; then (cd $CARGO_HOME/registry; for c in cache/*/*.crate; do s=src/${c#cache/}; if [ ! -e ${s%.crate} ]; then rm -v $c; fi; done; find cache -name \*.crate | sort) fi - rm -rf cargo - mkdir -p cargo/registry - if [ -d $CARGO_HOME/registry/cache ]; then cp -R $CARGO_HOME/registry/cache cargo/registry/; echo Updated registry/cache; fi script: - rm -rf public - mkdir public - cp etc/index.html public - cargo +$TOOLCHAIN doc - mv target/doc public/dev artifacts: paths: - public only: - master az-1.2.1/etc/index.html000064400000000000000000000012710072674642500130400ustar 00000000000000 Numeric casts

Numeric casts

The az crate provides casts and checked casts. Here you can find the documentation for the current development version of the crate.

Documentation

az-1.2.1/src/float.rs000064400000000000000000000035270072674642500125400ustar 00000000000000// Copyright © 2019–2021 Trevor Spiteri // This library is free software: you can redistribute it and/or // modify it under the terms of either // // * the Apache License, Version 2.0 or // * the MIT License // // at your option. // // You should have recieved copies of the Apache License and the MIT // License along with the library. If not, see // and // . use crate::{cast, Cast, CheckedCast, Round, UnwrappedCast}; use core::fmt::{Debug, Display, Formatter, LowerExp, Result as FmtResult, UpperExp}; macro_rules! convert { ($($Src:ty),* => $Dst:ty) => { $( impl Cast<$Dst> for $Src { #[inline] fn cast(self) -> $Dst { self as $Dst } } impl CheckedCast<$Dst> for $Src { #[inline] fn checked_cast(self) -> Option<$Dst> { Some(cast(self)) } } impl UnwrappedCast<$Dst> for $Src { #[inline] fn unwrapped_cast(self) -> $Dst { cast(self) } } )* }; } convert! { i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64 => f32 } convert! { i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64 => f64 } impl Display for Round { fn fmt(&self, f: &mut Formatter) -> FmtResult { Display::fmt(&self.0, f) } } impl Debug for Round { fn fmt(&self, f: &mut Formatter) -> FmtResult { Debug::fmt(&self.0, f) } } impl LowerExp for Round { fn fmt(&self, f: &mut Formatter) -> FmtResult { LowerExp::fmt(&self.0, f) } } impl UpperExp for Round { fn fmt(&self, f: &mut Formatter) -> FmtResult { UpperExp::fmt(&self.0, f) } } az-1.2.1/src/int.rs000064400000000000000000000450700072674642500122240ustar 00000000000000// Copyright © 2019–2021 Trevor Spiteri // This library is free software: you can redistribute it and/or // modify it under the terms of either // // * the Apache License, Version 2.0 or // * the MIT License // // at your option. // // You should have recieved copies of the Apache License and the MIT // License along with the library. If not, see // and // . use crate::{ cast, checked_cast, overflowing_cast, saturating_cast, wrapping_cast, Cast, CheckedCast, OverflowingCast, Round, SaturatingCast, UnwrappedCast, WrappingCast, }; use core::{mem, num::Wrapping}; macro_rules! bool_to_int { ($Dst:ty) => { impl Cast<$Dst> for bool { #[inline] fn cast(self) -> $Dst { self as $Dst } } impl CheckedCast<$Dst> for bool { #[inline] fn checked_cast(self) -> Option<$Dst> { Some(self as $Dst) } } impl SaturatingCast<$Dst> for bool { #[inline] fn saturating_cast(self) -> $Dst { self as $Dst } } impl WrappingCast<$Dst> for bool { #[inline] fn wrapping_cast(self) -> $Dst { self as $Dst } } impl OverflowingCast<$Dst> for bool { #[inline] fn overflowing_cast(self) -> ($Dst, bool) { (self as $Dst, false) } } impl UnwrappedCast<$Dst> for bool { #[inline] fn unwrapped_cast(self) -> $Dst { self as $Dst } } }; } macro_rules! common { ($Src:ty => $Dst:ty) => { impl Cast<$Dst> for $Src { #[inline] #[cfg_attr(track_caller, track_caller)] fn cast(self) -> $Dst { let (wrapped, overflow) = overflowing_cast(self); debug_assert!(!overflow, "{} overflows", self); let _ = overflow; wrapped } } impl CheckedCast<$Dst> for $Src { #[inline] fn checked_cast(self) -> Option<$Dst> { match overflowing_cast(self) { (value, false) => Some(value), (_, true) => None, } } } impl SaturatingCast<$Dst> for $Src { #[inline] fn saturating_cast(self) -> $Dst { match overflowing_cast(self) { (value, false) => value, (_, true) => { if self > 0 { <$Dst>::max_value() } else { <$Dst>::min_value() } } } } } impl WrappingCast<$Dst> for $Src { #[inline] fn wrapping_cast(self) -> $Dst { overflowing_cast(self).0 } } impl UnwrappedCast<$Dst> for $Src { #[inline] fn unwrapped_cast(self) -> $Dst { match overflowing_cast(self) { (value, false) => value, (_, true) => panic!("overflow"), } } } }; } macro_rules! same_signedness { ($($Src:ty),* => $Dst:ty) => { $( common! { $Src => $Dst } impl OverflowingCast<$Dst> for $Src { #[inline] fn overflowing_cast(self) -> ($Dst, bool) { let wrapped = self as $Dst; let overflow = self != wrapped as $Src; (wrapped, overflow) } } )* }; } macro_rules! signed_to_unsigned { ($($Src:ty),* => $Dst:ty) => { $( common! { $Src => $Dst } impl OverflowingCast<$Dst> for $Src { #[inline] fn overflowing_cast(self) -> ($Dst, bool) { let wrapped = self as $Dst; let overflow = self < 0 || self != wrapped as $Src; (wrapped, overflow) } } )* }; } macro_rules! unsigned_to_signed { ($($Src:ty),* => $Dst:ty) => { $( common! { $Src => $Dst } impl OverflowingCast<$Dst> for $Src { #[inline] fn overflowing_cast(self) -> ($Dst, bool) { let wrapped = self as $Dst; let overflow = wrapped < 0 || self != wrapped as $Src; (wrapped, overflow) } } )* }; } macro_rules! wrapping_int { ($($Src:ty),* => $Dst:ty) => { $( impl Cast> for $Src { #[inline] fn cast(self) -> Wrapping<$Dst> { Wrapping(wrapping_cast(self)) } } impl CheckedCast> for $Src { #[inline] fn checked_cast(self) -> Option> { Some(cast(self)) } } impl UnwrappedCast> for $Src { #[inline] fn unwrapped_cast(self) -> Wrapping<$Dst> { cast(self) } } )* }; } macro_rules! float_to_int { ($Src:ty, $ViaU:ty, $ViaI:ty => $($Dst:ty)*) => { $( impl Cast<$Dst> for $Src { #[inline] #[cfg_attr(track_caller, track_caller)] fn cast(self) -> $Dst { let (wrapped, overflow) = overflowing_cast(self); debug_assert!(!overflow, "{} overflows", self); let _ = overflow; wrapped } } impl CheckedCast<$Dst> for $Src { fn checked_cast(self) -> Option<$Dst> { let f: Float<$ViaU> = self.into(); match f.kind { FloatKind::Nan | FloatKind::Infinite | FloatKind::Overflowing(_, true) => None, FloatKind::Overflowing(abs, false) => { if f.neg { let i = abs as $ViaI; if i == <$ViaI>::min_value() { checked_cast(i) } else if i < 0 { None } else { checked_cast(-i) } } else { checked_cast(abs) } } } } } impl SaturatingCast<$Dst> for $Src { #[cfg_attr(track_caller, track_caller)] fn saturating_cast(self) -> $Dst { let f: Float<$ViaU> = self.into(); let saturated = if f.neg { <$Dst>::min_value() } else { <$Dst>::max_value() }; match f.kind { FloatKind::Nan => panic!("NaN"), FloatKind::Infinite | FloatKind::Overflowing(_, true) => saturated, FloatKind::Overflowing(abs, false) => { if f.neg { let i = abs as $ViaI; if i == <$ViaI>::min_value() { saturating_cast(i) } else if i < 0 { saturated } else { saturating_cast(-i) } } else { saturating_cast(abs) } } } } } impl WrappingCast<$Dst> for $Src { #[inline] #[cfg_attr(track_caller, track_caller)] fn wrapping_cast(self) -> $Dst { overflowing_cast(self).0 } } impl OverflowingCast<$Dst> for $Src { #[cfg_attr(track_caller, track_caller)] fn overflowing_cast(self) -> ($Dst, bool) { let f: Float<$ViaU> = self.into(); match f.kind { FloatKind::Nan => panic!("NaN"), FloatKind::Infinite => panic!("infinite"), FloatKind::Overflowing(abs, overflow) => { if f.neg { let i = abs as $ViaI; let (wrapped, overflow2) = if i == <$ViaI>::min_value() { overflowing_cast(i) } else if i < 0 { (wrapping_cast::<_, $Dst>(abs).wrapping_neg(), true) } else { overflowing_cast(-i) }; (wrapped, overflow | overflow2) } else { let (wrapped, overflow2) = overflowing_cast(abs); (wrapped, overflow | overflow2) } } } } } impl UnwrappedCast<$Dst> for $Src { #[inline] fn unwrapped_cast(self) -> $Dst { match overflowing_cast(self) { (val, false) => val, (_, true) => panic!("overflow"), } } } impl Cast> for $Src { #[inline] #[cfg_attr(track_caller, track_caller)] fn cast(self) -> Wrapping<$Dst> { Wrapping(wrapping_cast(self)) } } impl CheckedCast> for $Src { fn checked_cast(self) -> Option> { let f: Float<$ViaU> = self.into(); match f.kind { FloatKind::Nan | FloatKind::Infinite => None, FloatKind::Overflowing(abs, _) => { let wrapped = if f.neg { let i = abs as $ViaI; if i == <$ViaI>::min_value() { wrapping_cast(i) } else if i < 0 { wrapping_cast::<_, $Dst>(abs).wrapping_neg() } else { wrapping_cast(-i) } } else { wrapping_cast(abs) }; Some(Wrapping(wrapped)) } } } } impl UnwrappedCast> for $Src { #[inline] fn unwrapped_cast(self) -> Wrapping<$Dst> { cast(self) } } )* }; } float_to_int! { f32, u32, i32 => i8 i16 i32 } float_to_int! { f32, u64, i64 => i64 } float_to_int! { f32, u128, i128 => i128 } #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))] float_to_int! { f32, u32, i32 => isize } #[cfg(target_pointer_width = "64")] float_to_int! { f32, u64, i64 => isize } float_to_int! { f32, u32, i32 => u8 u16 u32 } float_to_int! { f32, u64, i64 => u64 } float_to_int! { f32, u128, i128 => u128 } #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))] float_to_int! { f32, u32, i32 => usize } #[cfg(target_pointer_width = "64")] float_to_int! { f32, u64, i64 => usize } float_to_int! { f64, u64, i64 => i8 i16 i32 i64 } float_to_int! { f64, u128, i128 => i128 } float_to_int! { f64, u64, i64 => isize } float_to_int! { f64, u64, i64 => u8 u16 u32 u64 } float_to_int! { f64, u128, i128 => u128 } float_to_int! { f64, u64, i64 => usize } float_to_int! { Round, u32, i32 => i8 i16 i32 } float_to_int! { Round, u64, i64 => i64 } float_to_int! { Round, u128, i128 => i128 } #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))] float_to_int! { Round, u32, i32 => isize } #[cfg(target_pointer_width = "64")] float_to_int! { Round, u64, i64 => isize } float_to_int! { Round, u32, i32 => u8 u16 u32 } float_to_int! { Round, u64, i64 => u64 } float_to_int! { Round, u128, i128 => u128 } #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))] float_to_int! { Round, u32, i32 => usize } #[cfg(target_pointer_width = "64")] float_to_int! { Round, u64, i64 => usize } float_to_int! { Round, u64, i64 => i8 i16 i32 i64 } float_to_int! { Round, u128, i128 => i128 } float_to_int! { Round, u64, i64 => isize } float_to_int! { Round, u64, i64 => u8 u16 u32 u64 } float_to_int! { Round, u128, i128 => u128 } float_to_int! { Round, u64, i64 => usize } macro_rules! signed { ($($Dst:ty),*) => { $( bool_to_int! { $Dst } same_signedness! { i8, i16, i32, i64, i128, isize => $Dst } unsigned_to_signed! { u8, u16, u32, u64, u128, usize => $Dst } wrapping_int! { bool, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize => $Dst } )* }; } macro_rules! unsigned { ($($Dst:ty),*) => { $( bool_to_int! { $Dst } signed_to_unsigned! { i8, i16, i32, i64, i128, isize => $Dst } same_signedness! { u8, u16, u32, u64, u128, usize => $Dst } wrapping_int! { bool, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize => $Dst } )* }; } signed! { i8, i16, i32, i64, i128, isize } unsigned! { u8, u16, u32, u64, u128, usize } enum FloatKind { Nan, Infinite, Overflowing(Uns, bool), } struct Float { neg: bool, kind: FloatKind, } macro_rules! from_for_float { ($Src:ty, $Uns:ty, $PREC:expr => $($Dst:ty),*) => { $( impl From<$Src> for Float<$Dst> { fn from(src: $Src) -> Self { const SRC_NBITS: i32 = mem::size_of::<$Src>() as i32 * 8; const DST_NBITS: i32 = mem::size_of::<$Dst>() as i32 * 8; const MANT_NBITS: i32 = $PREC - 1; const EXP_NBITS: i32 = SRC_NBITS - MANT_NBITS - 1; const EXP_BIAS: i32 = (1 << (EXP_NBITS - 1)) - 1; const SIGN_MASK: $Uns = !(!0 >> 1); const MANT_MASK: $Uns = !(!0 << MANT_NBITS); const EXP_MASK: $Uns = !(SIGN_MASK | MANT_MASK); let u = src.to_bits(); let neg = (u & SIGN_MASK) != 0; let biased_exp = u & EXP_MASK; if biased_exp == EXP_MASK { let kind = if (u & MANT_MASK) == 0 { FloatKind::Infinite } else { FloatKind::Nan }; return Float { neg, kind }; } let shift = (biased_exp >> MANT_NBITS) as i32 - (EXP_BIAS + MANT_NBITS); // Check if the magnitude is smaller than one. Do not return // early if shift == -MANT_NBITS, as there is implicit one. if shift < -MANT_NBITS { let kind = FloatKind::Overflowing(0, false); return Float { neg, kind }; } // Check if the least significant bit will be in a $Dst. if shift >= DST_NBITS { let kind = FloatKind::Overflowing(0, true); return Float { neg, kind }; } let mut significand: $Dst = (u & MANT_MASK).into(); // Add implicit one. significand |= 1 << MANT_NBITS; let kind = if shift < 0 { FloatKind::Overflowing(significand >> -shift, false) } else { let wrapped = significand << shift; let overflow = (wrapped >> shift) != significand; FloatKind::Overflowing(wrapped, overflow) }; Float { neg, kind } } } impl From> for Float<$Dst> { fn from(src: Round<$Src>) -> Self { const SRC_NBITS: i32 = mem::size_of::<$Src>() as i32 * 8; const DST_NBITS: i32 = mem::size_of::<$Dst>() as i32 * 8; const MANT_NBITS: i32 = $PREC - 1; const EXP_NBITS: i32 = SRC_NBITS - MANT_NBITS - 1; const EXP_BIAS: i32 = (1 << (EXP_NBITS - 1)) - 1; const SIGN_MASK: $Uns = !(!0 >> 1); const MANT_MASK: $Uns = !(!0 << MANT_NBITS); const EXP_MASK: $Uns = !(SIGN_MASK | MANT_MASK); let src = src.0; let u = src.to_bits(); let neg = (u & SIGN_MASK) != 0; let biased_exp = u & EXP_MASK; if biased_exp == EXP_MASK { let kind = if (u & MANT_MASK) == 0 { FloatKind::Infinite } else { FloatKind::Nan }; return Float { neg, kind }; } let shift = (biased_exp >> MANT_NBITS) as i32 - (EXP_BIAS + MANT_NBITS); // If shift = -MANT_BITS, then 1 ≤ x < 2. // If shift = -MANT_BITS - 1, then 0.5 ≤ x < 1, which can be rounded up. // If shift < -MANT_BITS - 1, then x < 0.5, which is rounded down. //// || (shift == -MANT_NBITS - 1 && ((u & MANT_MASK) != 0 || x)) if shift < -MANT_NBITS - 1 { let kind = FloatKind::Overflowing(0, false); return Float { neg, kind }; } // Check if the least significant bit will be in a $Dst. if shift >= DST_NBITS { let kind = FloatKind::Overflowing(0, true); return Float { neg, kind }; } let mut significand: $Dst = (u & MANT_MASK).into(); // Add implicit one. significand |= 1 << MANT_NBITS; let kind = if shift < 0 { let right = -shift; let round_bit = 1 << (right - 1); if (significand & round_bit) != 0 && (significand & (3 * round_bit - 1)) != 0 { significand += round_bit; } FloatKind::Overflowing(significand >> right, false) } else { let wrapped = significand << shift; let overflow = (wrapped >> shift) != significand; FloatKind::Overflowing(wrapped, overflow) }; Float { neg, kind } } } )* }; } from_for_float! { f32, u32, 24 => u32, u64, u128 } from_for_float! { f64, u64, 53 => u64, u128 } az-1.2.1/src/lib.rs000064400000000000000000000734620072674642500122060ustar 00000000000000// Copyright © 2019–2021 Trevor Spiteri // This library is free software: you can redistribute it and/or // modify it under the terms of either // // * the Apache License, Version 2.0 or // * the MIT License // // at your option. // // You should have recieved copies of the Apache License and the MIT // License along with the library. If not, see // and // . /*! # Numeric casts This crate provides casts and checked casts. ## Quick examples ```rust use az::{Az, OverflowingAs, WrappingAs}; use core::num::Wrapping; // Panics on overflow with `debug_assertions`, otherwise wraps assert_eq!(12i32.az::(), 12u32); // Always wraps let wrapped = 1u32.wrapping_neg(); assert_eq!((-1).wrapping_as::(), wrapped); assert_eq!((-1).overflowing_as::(), (wrapped, true)); // Wrapping can also be obtained using `Wrapping` assert_eq!((-1).az::>().0, wrapped); ``` Conversions from floating-point to integers are also supported. Numbers are rounded towards zero, but the [`Round`] wrapper can be used to convert floating-point numbers to integers with rounding to the nearest, with ties rounded to even. ```rust use az::{Az, CheckedAs, Round, SaturatingAs}; use core::f32; assert_eq!(15.7.az::(), 15); assert_eq!(Round(15.5).az::(), 16); assert_eq!(1.5e20.saturating_as::(), i32::max_value()); assert_eq!(f32::NAN.checked_as::(), None); ``` ## Implementing casts for other types To provide casts for another type, you should implement the [`Cast`] trait and if necessary the [`CheckedCast`], [`SaturatingCast`], [`WrappingCast`], [`OverflowingCast`] and [`UnwrappedCast`] traits. The [`Az`], [`CheckedAs`], [`SaturatingAs`], [`WrappingAs`], [`OverflowingAs`] and [`UnwrappedAs`] traits are already implemented for all types using blanket implementations that make use of the former traits. The cast traits can also be implemented for references. This can be useful for expensive types that are not [`Copy`]. For example if you have your own integer type that does not implement [`Copy`], you could implement casts like in the following example. (The type `I` could be an expensive type, for example a bignum integer, but for the example it is only a wrapped [`i32`].) ```rust use az::{Az, Cast}; use core::borrow::Borrow; struct I(i32); impl Cast for &'_ I { fn cast(self) -> i64 { self.0.cast() } } let owned = I(12); assert_eq!((&owned).az::(), 12); // borrow can be used if chaining is required assert_eq!(owned.borrow().az::(), 12); ``` ## Using the *az* crate The *az* crate is available on [crates.io][*az* crate]. To use it in your crate, add it as a dependency inside [*Cargo.toml*]: ```toml [dependencies] az = "1.2" ``` The crate requires rustc version 1.31.0 or later. ## License This crate is free software: you can redistribute it and/or modify it under the terms of either * the [Apache License, Version 2.0][LICENSE-APACHE] or * the [MIT License][LICENSE-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 License, Version 2.0, shall be dual licensed as above, without any additional terms or conditions. [*Cargo.toml*]: https://doc.rust-lang.org/cargo/guide/dependencies.html [*az* crate]: https://crates.io/crates/az [LICENSE-APACHE]: https://www.apache.org/licenses/LICENSE-2.0 [LICENSE-MIT]: https://opensource.org/licenses/MIT */ #![no_std] #![warn(missing_docs)] #![doc(html_root_url = "https://docs.rs/az/~1.2")] #![doc(test(attr(deny(warnings))))] #![cfg_attr(feature = "fail-on-warnings", deny(warnings))] #[cfg(test)] extern crate std; mod float; mod int; #[cfg(test)] mod tests; /** Used to cast values. It is normally easier to use the [`Az`] trait instead of this trait. # Panics When debug assertions are enabled, this trait’s method panics if the value does not fit in the destination. When debug assertions are *not* enabled (usual in release mode), the wrapped value can be returned, but it is not considered a breaking change if in the future it panics; if wrapping is required use [`WrappingCast`] instead. This trait’s method also panics with no debug assertions if the value does not fit and cannot be wrapped, for example when trying to cast floating-point ∞ into an integer type. # Examples ```rust use az::Cast; let a: u32 = 5i32.cast(); assert_eq!(a, 5); assert_eq!(Cast::::cast(17.1f32), 17); ``` */ pub trait Cast { /// Casts the value. fn cast(self) -> Dst; } /** Used for checked casts. This trait’s method returns [`None`] if the value does not fit. It is normally easier to use the [`CheckedAs`] trait instead of this trait. # Examples ```rust use az::CheckedCast; use core::f32; let a: Option = 5i32.checked_cast(); assert_eq!(a, Some(5)); assert_eq!(CheckedCast::::checked_cast(-5i32), None); assert_eq!(CheckedCast::::checked_cast(17.1f32), Some(17)); let b: Option = f32::NAN.checked_cast(); assert_eq!(b, None); ``` */ pub trait CheckedCast { /// Casts the value. fn checked_cast(self) -> Option; } /** Used to cast into the destination type, saturating if the value does not fit. It is normally easier to use the [`SaturatingAs`] trait instead of this trait. # Panics This trait’s method panics if the value does not fit and saturation does not make sense, for example when trying to cast floating-point NaN into an integer type. # Examples ```rust use az::SaturatingCast; let a: u32 = (-1).saturating_cast(); assert_eq!(a, 0); assert_eq!(SaturatingCast::::saturating_cast(17.0 + 256.0), 255); ``` */ pub trait SaturatingCast { /// Casts the value. fn saturating_cast(self) -> Dst; } /** Wrapping cast. It is normally easier to use the [`WrappingAs`] trait instead of this trait. # Panics This trait’s method panics if the value does not fit and cannot be wrapped, for example when trying to cast floating-point ∞ into an integer type. # Examples ```rust use az::WrappingCast; let a: u32 = (-1).wrapping_cast(); assert_eq!(a, u32::max_value()); assert_eq!(WrappingCast::::wrapping_cast(17.0 + 256.0), 17); ``` */ pub trait WrappingCast { /// Casts the value. fn wrapping_cast(self) -> Dst; } /** Used for overflowing casts. This trait’s method returns a [tuple] of the value and a [`bool`], indicating whether an overflow has occurred. On overflow, the wrapped value is returned. It is normally easier to use the [`OverflowingAs`] trait instead of this trait. # Examples ```rust use az::OverflowingCast; let a: (u8, bool) = 17i32.overflowing_cast(); assert_eq!(a, (17, false)); assert_eq!(OverflowingCast::::overflowing_cast(-1), (u32::max_value(), true)); assert_eq!(OverflowingCast::::overflowing_cast(17.0 + 256.0), (17, true)); ``` # Panics This trait’s method panics if the value does not fit and cannot be wrapped, for example when trying to cast floating-point ∞ into an integer type. */ pub trait OverflowingCast { /// Casts the value. fn overflowing_cast(self) -> (Dst, bool); } /** Used to cast values, panicking if the value does not fit. It is normally easier to use the [`UnwrappedAs`] trait instead of this trait. # Panics This trait’s method panics if the value does not fit in the destination, even when debug assertions are not enabled. # Examples ```rust use az::UnwrappedCast; let a: u32 = 5i32.unwrapped_cast(); assert_eq!(a, 5); assert_eq!(UnwrappedCast::::unwrapped_cast(17.1f32), 17); ``` The following panics because of overflow. ```rust,should_panic use az::UnwrappedCast; let _overflow: u32 = (-5i32).unwrapped_cast(); ``` */ pub trait UnwrappedCast { /// Casts the value. #[cfg_attr(track_caller, track_caller)] fn unwrapped_cast(self) -> Dst; } /** Used to cast values. This trait enables trait constraints for casting in the opposite direction to [`Cast`]. # Examples ```rust use az::CastFrom; trait Tr { type Assoc: CastFrom; fn assoc_from_u8(a: u8) -> Self::Assoc { CastFrom::cast_from(a) } } impl Tr for () { type Assoc = i8; } assert_eq!(<() as Tr>::assoc_from_u8(5u8), 5i8); ``` */ pub trait CastFrom { /// Casts the value. fn cast_from(src: Src) -> Self; } impl, Dst> CastFrom for Dst { #[inline] #[cfg_attr(track_caller, track_caller)] fn cast_from(src: Src) -> Self { src.cast() } } /** Used for checked casts. This trait enables trait constraints for casting in the opposite direction to [`CheckedCast`]. # Examples ```rust use az::CheckedCastFrom; trait Tr { type Assoc: CheckedCastFrom; fn checked_assoc_from_u8(a: u8) -> Option { CheckedCastFrom::checked_cast_from(a) } } impl Tr for () { type Assoc = i8; } assert_eq!(<() as Tr>::checked_assoc_from_u8(5u8), Some(5i8)); assert_eq!(<() as Tr>::checked_assoc_from_u8(255u8), None); ``` */ pub trait CheckedCastFrom: Sized { /// Casts the value. fn checked_cast_from(src: Src) -> Option; } impl, Dst> CheckedCastFrom for Dst { #[inline] #[cfg_attr(track_caller, track_caller)] fn checked_cast_from(src: Src) -> Option { src.checked_cast() } } /** Used to cast, saturating if the value does not fit. This trait enables trait constraints for casting in the opposite direction to [`SaturatingCast`]. # Examples ```rust use az::SaturatingCastFrom; trait Tr { type Assoc: SaturatingCastFrom; fn saturating_assoc_from_u8(a: u8) -> Self::Assoc { SaturatingCastFrom::saturating_cast_from(a) } } impl Tr for () { type Assoc = i8; } assert_eq!(<() as Tr>::saturating_assoc_from_u8(5u8), 5i8); assert_eq!(<() as Tr>::saturating_assoc_from_u8(255u8), 127i8); ``` */ pub trait SaturatingCastFrom { /// Casts the value. fn saturating_cast_from(src: Src) -> Self; } impl, Dst> SaturatingCastFrom for Dst { #[inline] #[cfg_attr(track_caller, track_caller)] fn saturating_cast_from(src: Src) -> Self { src.saturating_cast() } } /** Wrapping cast. This trait enables trait constraints for casting in the opposite direction to [`WrappingCast`]. # Examples ```rust use az::WrappingCastFrom; trait Tr { type Assoc: WrappingCastFrom; fn wrapping_assoc_from_u8(a: u8) -> Self::Assoc { WrappingCastFrom::wrapping_cast_from(a) } } impl Tr for () { type Assoc = i8; } assert_eq!(<() as Tr>::wrapping_assoc_from_u8(5u8), 5i8); assert_eq!(<() as Tr>::wrapping_assoc_from_u8(255u8), -1i8); ``` */ pub trait WrappingCastFrom { /// Casts the value. fn wrapping_cast_from(src: Src) -> Self; } impl, Dst> WrappingCastFrom for Dst { #[inline] #[cfg_attr(track_caller, track_caller)] fn wrapping_cast_from(src: Src) -> Self { src.wrapping_cast() } } /** Used for overflowing casts. This trait enables trait constraints for casting in the opposite direction to [`OverflowingCast`]. # Examples ```rust use az::OverflowingCastFrom; trait Tr { type Assoc: OverflowingCastFrom; fn overflowing_assoc_from_u8(a: u8) -> (Self::Assoc, bool) { OverflowingCastFrom::overflowing_cast_from(a) } } impl Tr for () { type Assoc = i8; } assert_eq!(<() as Tr>::overflowing_assoc_from_u8(5u8), (5i8, false)); assert_eq!(<() as Tr>::overflowing_assoc_from_u8(255u8), (-1i8, true)); ``` */ pub trait OverflowingCastFrom: Sized { /// Casts the value. fn overflowing_cast_from(src: Src) -> (Self, bool); } impl, Dst> OverflowingCastFrom for Dst { #[inline] #[cfg_attr(track_caller, track_caller)] fn overflowing_cast_from(src: Src) -> (Self, bool) { src.overflowing_cast() } } /** Used to cast values, panicking if the value does not fit. This trait enables trait constraints for casting in the opposite direction to [`UnwrappedCast`]. # Examples ```rust use az::UnwrappedCastFrom; trait Tr { type Assoc: UnwrappedCastFrom; fn unwrapped_assoc_from_u8(a: u8) -> Self::Assoc { UnwrappedCastFrom::unwrapped_cast_from(a) } } impl Tr for () { type Assoc = i8; } assert_eq!(<() as Tr>::unwrapped_assoc_from_u8(5u8), 5i8); ``` The following assertion would panic because of overflow. ```rust, should_panic # use az::UnwrappedCastFrom; # trait Tr { # type Assoc: UnwrappedCastFrom; # fn unwrapped_assoc_from_u8(a: u8) -> Self::Assoc { # UnwrappedCastFrom::unwrapped_cast_from(a) # } # } # impl Tr for () { # type Assoc = i8; # } let _overflow = <() as Tr>::unwrapped_assoc_from_u8(255u8); ``` */ pub trait UnwrappedCastFrom { /// Casts the value. fn unwrapped_cast_from(src: Src) -> Self; } impl, Dst> UnwrappedCastFrom for Dst { #[inline] #[cfg_attr(track_caller, track_caller)] fn unwrapped_cast_from(src: Src) -> Self { src.unwrapped_cast() } } /** Used to cast values. This is a convenience trait to enable writing src.[az][`Az::az`]::<Dst>(). This would not work with the [Cast][`Cast`]::[cast][`Cast::cast`] method because the [`Cast`] trait is generic while its [`cast`][`Cast::cast`] method is not generic. This trait’s method is suitable for chaining. If there is an implementation of [Cast][`Cast`]<Dst> for `&Src` but not for `Src`, and the variable `src` is of type `Src`, then src.[az][`Az::az`]::<Dst>() would not work and (&src).[az][`Az::az`]::<Dst>() is not easy to use with chaining, but src.[borrow][`borrow`]().[az][`Az::az`]::<Dst>() works. # Panics When debug assertions are enabled, this trait’s method panics if the value does not fit in the destination. When debug assertions are *not* enabled (usual in release mode), the wrapped value can be returned, but it is not considered a breaking change if in the future it panics; if wrapping is required use [`WrappingAs`] instead. This trait’s method also panics with no debug assertions if the value does not fit and cannot be wrapped, for example when trying to cast floating-point ∞ into an integer type. # Examples ```rust use az::Az; assert_eq!(5i32.az::(), 5); assert_eq!(17.1f32.az::(), 17); ``` The following example shows how this trait can be used when [`Cast`] is implemented for a reference type. ```rust use az::{Az, Cast}; use core::borrow::Borrow; struct I(i32); impl Cast for &'_ I { fn cast(self) -> i64 { self.0.cast() } } let r = &I(-5); assert_eq!(r.az::(), -5); let owned = I(12); assert_eq!(owned.borrow().az::(), 12); ``` [`borrow`]: `core::borrow::Borrow::borrow` */ pub trait Az { /// Casts the value. fn az(self) -> Dst where Self: Cast; } impl Az for T { #[inline] #[cfg_attr(track_caller, track_caller)] fn az(self) -> Dst where Self: Cast, { self.cast() } } /** Used for checked casts. This trait’s method returns [`None`] if the value does not fit. This is a convenience trait to enable writing src.[checked\_as][`CheckedAs::checked_as`]::<Dst>(). This would not work with the [CheckedCast][`CheckedCast`]::[checked\_cast][`CheckedCast::checked_cast`] method because the [`CheckedCast`] trait is generic while its [`checked_cast`][`CheckedCast::checked_cast`] method is not generic. This trait’s method is suitable for chaining. If there is an implementation of [CheckedCast][`CheckedCast`]<Dst> for `&Src` but not for `Src`, and the variable `src` is of type `Src`, then src.[checked\_as][`CheckedAs::checked_as`]::<Dst>() would not work and (&src).[checked\_as][`CheckedAs::checked_as`]::<Dst>() is not easy to use with chaining, but src.[borrow][`borrow`]().[checked\_as][`CheckedAs::checked_as`]::<Dst>() works. # Examples ```rust use az::CheckedAs; use core::f32; assert_eq!(5i32.checked_as::(), Some(5)); assert_eq!((-5i32).checked_as::(), None); assert_eq!(17.1f32.checked_as::(), Some(17)); assert_eq!(f32::NAN.checked_as::(), None); ``` The following example shows how this trait can be used when [`CheckedCast`] is implemented for a reference type. ```rust use az::{CheckedAs, CheckedCast}; use core::borrow::Borrow; struct I(i32); impl CheckedCast for &'_ I { fn checked_cast(self) -> Option { self.0.checked_cast() } } let r = &I(-5); assert_eq!(r.checked_as::(), None); let owned = I(12); assert_eq!(owned.borrow().checked_as::(), Some(12)); ``` [`borrow`]: `core::borrow::Borrow::borrow` */ pub trait CheckedAs { /// Casts the value. fn checked_as(self) -> Option where Self: CheckedCast; } impl CheckedAs for T { #[inline] #[cfg_attr(track_caller, track_caller)] fn checked_as(self) -> Option where Self: CheckedCast, { self.checked_cast() } } /** Used to cast into the destination type, saturating if the value does not fit. This is a convenience trait to enable writing src.[saturating\_as][`SaturatingAs::saturating_as`]::<Dst>(). This would not work with the [SaturatingCast][`SaturatingCast`]::[saturating\_cast][`SaturatingCast::saturating_cast`] method because the [`SaturatingCast`] trait is generic while its [`SaturatingCast::saturating_cast`] method is not generic. This trait’s method is suitable for chaining. If there is an implementation of [SaturatingCast][`SaturatingCast`]<Dst> for `&Src` but not for `Src`, and the variable `src` is of type `Src`, then src.[saturating\_as][`SaturatingAs::saturating_as`]::<Dst>() would not work and (&src).[saturating\_as][`SaturatingAs::saturating_as`]::<Dst>() is not easy to use with chaining, but src.[borrow][`borrow`]().[saturating\_as][`SaturatingAs::saturating_as`]::<Dst>() works. # Panics This trait’s method panics if the value does not fit and saturation does not make sense, for example when trying to cast floating-point NaN into an integer type. # Examples ```rust use az::SaturatingAs; assert_eq!((-1).saturating_as::(), 0); assert_eq!((17.0 + 256.0).saturating_as::(), 255); ``` The following example shows how this trait can be used when [`SaturatingCast`] is implemented for a reference type. ```rust use az::{SaturatingAs, SaturatingCast}; use core::borrow::Borrow; struct I(i32); impl SaturatingCast for &'_ I { fn saturating_cast(self) -> u32 { self.0.saturating_cast() } } let r = &I(-5); assert_eq!(r.saturating_as::(), 0); let owned = I(12); assert_eq!(owned.borrow().saturating_as::(), 12); ``` [`borrow`]: `core::borrow::Borrow::borrow` */ pub trait SaturatingAs { /// Casts the value. fn saturating_as(self) -> Dst where Self: SaturatingCast; } impl SaturatingAs for T { #[inline] #[cfg_attr(track_caller, track_caller)] fn saturating_as(self) -> Dst where Self: SaturatingCast, { self.saturating_cast() } } /** Wrapping cast. This is a convenience trait to enable writing src.[wrapping\_as][`WrappingAs::wrapping_as`]::<Dst>(). This would not work with the [WrappingCast][`WrappingCast`]::[wrapping\_cast][`WrappingCast::wrapping_cast`] method because the [`WrappingCast`] trait is generic while its [`WrappingCast::wrapping_cast`] method is not generic. This trait’s method is suitable for chaining. If there is an implementation of [WrappingCast][`WrappingCast`]<Dst> for `&Src` but not for `Src`, and the variable `src` is of type `Src`, then src.[wrapping\_as][`WrappingAs::wrapping_as`]::<Dst>() would not work and (&src).[wrapping\_as][`WrappingAs::wrapping_as`]::<Dst>() is not easy to use with chaining, but src.[borrow][`borrow`]().[wrapping\_as][`WrappingAs::wrapping_as`]::<Dst>() works. # Panics This trait’s method panics if the value does not fit and cannot be wrapped, for example when trying to cast floating-point ∞ into an integer type. # Examples ```rust use az::WrappingAs; assert_eq!((-1).wrapping_as::(), u32::max_value()); assert_eq!((17.0 + 256.0).wrapping_as::(), 17); ``` The following example shows how this trait can be used when [`WrappingCast`] is implemented for a reference type. ```rust use az::{WrappingAs, WrappingCast}; use core::borrow::Borrow; struct I(i32); impl WrappingCast for &'_ I { fn wrapping_cast(self) -> u32 { self.0.wrapping_cast() } } let r = &I(-5); assert_eq!(r.wrapping_as::(), 5u32.wrapping_neg()); let owned = I(12); assert_eq!(owned.borrow().wrapping_as::(), 12); ``` [`borrow`]: `core::borrow::Borrow::borrow` */ pub trait WrappingAs { /// Casts the value. fn wrapping_as(self) -> Dst where Self: WrappingCast; } impl WrappingAs for T { #[inline] #[cfg_attr(track_caller, track_caller)] fn wrapping_as(self) -> Dst where Self: WrappingCast, { self.wrapping_cast() } } /** Used for overflowing casts. This trait’s method returns a [tuple] of the value and a [`bool`], indicating whether an overflow has occurred. On overflow, the wrapped value is returned. This is a convenience trait to enable writing src.[overflowing\_as][`OverflowingAs::overflowing_as`]::<Dst>(). This would not work with the [OverflowingCast][`OverflowingCast`]::[overflowing\_cast][`OverflowingCast::overflowing_cast`] method because the [`OverflowingCast`] trait is generic while its [`OverflowingCast::overflowing_cast`] method is not generic. This trait’s method is suitable for chaining. If there is an implementation of [OverflowingCast][`OverflowingCast`]<Dst> for `&Src` but not for `Src`, and the variable `src` is of type `Src`, then src.[overflowing\_as][`OverflowingAs::overflowing_as`]::<Dst>() would not work and (&src).[overflowing\_as][`OverflowingAs::overflowing_as`]::<Dst>() is not easy to use with chaining, but src.[borrow][`borrow`]().[overflowing\_as][`OverflowingAs::overflowing_as`]::<Dst>() works. # Panics This trait’s method panics if the value does not fit and cannot be wrapped, for example when trying to cast floating-point ∞ into an integer type. # Examples ```rust use az::OverflowingAs; assert_eq!(17i32.overflowing_as::(), (17, false)); assert_eq!((-1).overflowing_as::(), (u32::max_value(), true)); assert_eq!((17.0 + 256.0).overflowing_as::(), (17, true)); ``` The following example shows how this trait can be used when [`OverflowingCast`] is implemented for a reference type. ```rust use az::{OverflowingAs, OverflowingCast}; use core::borrow::Borrow; struct I(i32); impl OverflowingCast for &'_ I { fn overflowing_cast(self) -> (u32, bool) { self.0.overflowing_cast() } } let r = &I(-5); assert_eq!(r.overflowing_as::(), (5u32.wrapping_neg(), true)); let owned = I(12); assert_eq!(owned.borrow().overflowing_as::(), (12, false)); ``` [`borrow`]: `core::borrow::Borrow::borrow` */ pub trait OverflowingAs { /// Casts the value. fn overflowing_as(self) -> (Dst, bool) where Self: OverflowingCast; } impl OverflowingAs for T { #[inline] #[cfg_attr(track_caller, track_caller)] fn overflowing_as(self) -> (Dst, bool) where Self: OverflowingCast, { self.overflowing_cast() } } /** Used to cast values, panicking if the value does not fit. This is a convenience trait to enable writing src.[unwrapped\_as][`UnwrappedAs::unwrapped_as`]::<Dst>(). This would not work with the [UnwrappedCast][`UnwrappedCast`]::[unwrapped\_cast][`UnwrappedCast::unwrapped_cast`] method because the [`UnwrappedCast`] trait is generic while its [`UnwrappedCast::unwrapped_cast`] method is not generic. This trait’s method is suitable for chaining. If there is an implementation of [UnwrappedCast][`UnwrappedCast`]<Dst> for `&Src` but not for `Src`, and the variable `src` is of type `Src`, then src.[unwrapped\_as][`UnwrappedAs::unwrapped_as`]::<Dst>() would not work and (&src).[unwrapped\_as][`UnwrappedAs::unwrapped_as`]::<Dst>() is not easy to use with chaining, but src.[borrow][`borrow`]().[unwrapped\_as][`UnwrappedAs::unwrapped_as`]::<Dst>() works. # Panics This trait’s method panics if the value does not fit in the destination, even when debug assertions are not enabled. # Examples ```rust use az::UnwrappedAs; assert_eq!(5i32.unwrapped_as::(), 5); assert_eq!(17.1f32.unwrapped_as::(), 17); ``` The following panics because of overflow. ```rust,should_panic use az::UnwrappedAs; let _overflow = (-5i32).unwrapped_as::(); ``` The following example shows how this trait can be used when [`UnwrappedCast`] is implemented for a reference type. ```rust use az::{UnwrappedAs, UnwrappedCast}; use core::borrow::Borrow; struct I(i32); impl UnwrappedCast for &'_ I { fn unwrapped_cast(self) -> i64 { self.0.unwrapped_cast() } } let r = &I(-5); assert_eq!(r.unwrapped_as::(), -5); let owned = I(12); assert_eq!(owned.borrow().unwrapped_as::(), 12); ``` [`borrow`]: `core::borrow::Borrow::borrow` */ pub trait UnwrappedAs { /// Casts the value. #[cfg_attr(track_caller, track_caller)] fn unwrapped_as(self) -> Dst where Self: UnwrappedCast; } impl UnwrappedAs for T { #[inline] fn unwrapped_as(self) -> Dst where Self: UnwrappedCast, { self.unwrapped_cast() } } /// Casts the value. /// /// # Panics /// /// When debug assertions are enabled, panics if the value does not /// fit in the destination. When debug assertions are *not* enabled /// (usual in release mode), the wrapped value can be returned, but it /// is not considered a breaking change if in the future it panics; if /// wrapping is required use [`wrapping_cast`] instead. /// /// This function also panics with no debug assertions if the value /// does not fit and cannot be wrapped, for example when trying to /// cast floating-point ∞ into an integer type. /// /// # Examples /// /// ```rust /// assert_eq!(az::cast::(5), 5); /// assert_eq!(az::cast::(17.1), 17); /// ``` #[inline] #[cfg_attr(track_caller, track_caller)] pub fn cast, Dst>(src: Src) -> Dst { src.cast() } /// Casts the value, returning [`None`] if the value does not fit. /// /// # Examples /// /// ```rust /// use core::f32; /// /// assert_eq!(az::checked_cast::(5), Some(5)); /// assert_eq!(az::checked_cast::(-5), None); /// assert_eq!(az::checked_cast::(17.1), Some(17)); /// assert_eq!(az::checked_cast::(f32::NAN), None); /// ``` #[inline] #[cfg_attr(track_caller, track_caller)] pub fn checked_cast, Dst>(src: Src) -> Option { src.checked_cast() } /// Casts the value, saturating if the value does not fit. /// /// # Panics /// /// Panics if the value does not fit and saturation does not make /// sense, for example when trying to cast floating-point NaN into an /// integer type. /// /// # Examples /// /// ```rust /// assert_eq!(az::saturating_cast::(-1), 0); /// assert_eq!(az::saturating_cast::(17.0 + 256.0), 255); /// ``` #[inline] #[cfg_attr(track_caller, track_caller)] pub fn saturating_cast, Dst>(src: Src) -> Dst { src.saturating_cast() } /// Casts the value, wrapping on overflow. /// /// # Panics /// /// Panics if the value does not fit and cannot be wrapped, for /// example when trying to cast floating-point ∞ into an integer type. /// /// # Examples /// /// ```rust /// assert_eq!(az::wrapping_cast::(-1), u32::max_value()); /// assert_eq!(az::wrapping_cast::(17.0 + 256.0), 17); /// ``` #[inline] #[cfg_attr(track_caller, track_caller)] pub fn wrapping_cast, Dst>(src: Src) -> Dst { src.wrapping_cast() } /// Overflowing cast. /// /// Returns a [tuple] of the value and a [`bool`], indicating whether /// an overflow has occurred. On overflow, the wrapped value is /// returned. /// /// # Panics /// /// Panics if the value does not fit and cannot be wrapped, for /// example when trying to cast floating-point ∞ into an integer type. /// /// # Examples /// /// ```rust /// assert_eq!(az::overflowing_cast::(-1), (u32::max_value(), true)); /// assert_eq!(az::overflowing_cast::(17.0 + 256.0), (17, true)); /// ``` #[inline] #[cfg_attr(track_caller, track_caller)] pub fn overflowing_cast, Dst>(src: Src) -> (Dst, bool) { src.overflowing_cast() } /// Casts the value, panicking if the value does not fit. /// /// # Panics /// /// Panics if the value does not fit in the destination, even when /// debug assertions are not enabled. /// /// # Examples /// /// ```rust /// assert_eq!(az::unwrapped_cast::(5), 5); /// assert_eq!(az::unwrapped_cast::(17.1), 17); /// ``` /// /// The following panics because of overflow. /// /// ```rust,should_panic /// let _overflow = az::unwrapped_cast::(-5); /// ``` #[inline] #[cfg_attr(track_caller, track_caller)] pub fn unwrapped_cast, Dst>(src: Src) -> Dst { src.unwrapped_cast() } /// Used to convert floating-point numbers to integers with rounding /// to the nearest, with ties rounded to even. /// /// The underlying value can be retrieved through the `.0` index. /// /// # Examples /// /// ```rust /// use az::Round; /// assert_eq!(az::cast::<_, i32>(Round(0.4)), 0); /// assert_eq!(az::cast::<_, i32>(Round(0.6)), 1); /// // ties rounded to even /// assert_eq!(az::cast::<_, i32>(Round(-0.5)), 0); /// assert_eq!(az::cast::<_, i32>(Round(-1.5)), -2); /// ``` #[repr(transparent)] #[derive(Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord)] pub struct Round(pub T); az-1.2.1/src/tests/float_to_int.rs000064400000000000000000001625270072674642500152640ustar 00000000000000// Copyright © 2019–2021 Trevor Spiteri // This library is free software: you can redistribute it and/or // modify it under the terms of either // // * the Apache License, Version 2.0 or // * the MIT License // // at your option. // // You should have recieved copies of the Apache License and the MIT // License along with the library. If not, see // and // . use crate::{ cast, checked_cast, overflowing_cast, saturating_cast, tests::{Float, Int}, unwrapped_cast, wrapping_cast, Cast, CheckedCast, OverflowingCast, Round, SaturatingCast, UnwrappedCast, WrappingCast, }; use core::{f32, f64, mem, num::Wrapping}; fn float_to_nonwrapping_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_prec = Src::prec(); let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; let src_0 = Src::int_shl(0, 0); let src_1 = Src::int_shl(1, 0); let src_1_5 = Src::int_shl(3, -1); let src_8 = Src::int_shl(1, (dst_nbits - 1) as i8); let src_1ish = if src_prec > dst_nbits { src_1 } else { Src::int_shl(1, (dst_nbits - src_prec) as i8) }; let src_7ish = src_8 - src_1ish; let src_6 = Src::int_shl(6, (dst_nbits - 4) as i8); let src_9ish = src_8 + src_1ish; let src_c = Src::int_shl(12, (dst_nbits - 4) as i8); let src_fish = src_8 + src_7ish; let src_max = Src::max(); let src_inf = Src::inf(); let src_nan = Src::nan(); let dst_z0 = Dst::zero(); let dst_p1 = Dst::one(); let dst_m1 = !dst_z0; let (dst_p1ish, dst_m1ish) = if src_prec > dst_nbits { (dst_p1, dst_m1) } else { ( dst_p1 << (dst_nbits - src_prec), dst_m1 << (dst_nbits - src_prec), ) }; let dst_p4 = dst_p1 << (dst_nbits - 2); let dst_p6 = dst_p4 + (dst_p4 >> 1); let dst_p7ish = dst_p4 + (dst_p4 - dst_p1ish); let dst_m4 = dst_z0 - dst_p4; let dst_m6 = dst_z0 - dst_p6; let dst_m8 = dst_m4 + dst_m4; let dst_m7ish = dst_m8 + dst_p1ish; let dst_p7 = !dst_m8; let (dst_wmax, dst_wmax_neg) = if src_nbits == 32 && dst_nbits == 128 { let max = !dst_z0 << (dst_nbits - src_prec); (max, !max + dst_p1) } else { (dst_z0, dst_z0) }; assert_eq!(cast(-src_8), dst_m8); assert_eq!(cast(-src_7ish), dst_m7ish); assert_eq!(cast(-src_6), dst_m6); assert_eq!(cast(-src_1_5), dst_m1); assert_eq!(cast(-src_1), dst_m1); assert_eq!(cast(-src_0), dst_z0); assert_eq!(cast(src_0), dst_z0); assert_eq!(cast(src_1), dst_p1); assert_eq!(cast(src_1_5), dst_p1); assert_eq!(cast(src_6), dst_p6); assert_eq!(cast(src_7ish), dst_p7ish); assert_eq!(checked_cast(-src_nan), None); assert_eq!(checked_cast(-src_inf), None); assert_eq!(checked_cast(-src_max), None); assert_eq!(checked_cast(-src_fish), None); assert_eq!(checked_cast(-src_c), None); assert_eq!(checked_cast(-src_9ish), None); assert_eq!(checked_cast(-src_8), Some(dst_m8)); assert_eq!(checked_cast(-src_7ish), Some(dst_m7ish)); assert_eq!(checked_cast(-src_6), Some(dst_m6)); assert_eq!(checked_cast(-src_1_5), Some(dst_m1)); assert_eq!(checked_cast(-src_1), Some(dst_m1)); assert_eq!(checked_cast(-src_0), Some(dst_z0)); assert_eq!(checked_cast(src_0), Some(dst_z0)); assert_eq!(checked_cast(src_1), Some(dst_p1)); assert_eq!(checked_cast(src_1_5), Some(dst_p1)); assert_eq!(checked_cast(src_6), Some(dst_p6)); assert_eq!(checked_cast(src_7ish), Some(dst_p7ish)); assert_eq!(checked_cast(src_8), None); assert_eq!(checked_cast(src_9ish), None); assert_eq!(checked_cast(src_c), None); assert_eq!(checked_cast(src_fish), None); assert_eq!(checked_cast(src_max), None); assert_eq!(checked_cast(src_inf), None); assert_eq!(checked_cast(src_nan), None); assert_eq!(saturating_cast(-src_inf), dst_m8); assert_eq!(saturating_cast(-src_max), dst_m8); assert_eq!(saturating_cast(-src_fish), dst_m8); assert_eq!(saturating_cast(-src_c), dst_m8); assert_eq!(saturating_cast(-src_9ish), dst_m8); assert_eq!(saturating_cast(-src_8), dst_m8); assert_eq!(saturating_cast(-src_7ish), dst_m7ish); assert_eq!(saturating_cast(-src_6), dst_m6); assert_eq!(saturating_cast(-src_1_5), dst_m1); assert_eq!(saturating_cast(-src_1), dst_m1); assert_eq!(saturating_cast(-src_0), dst_z0); assert_eq!(saturating_cast(src_0), dst_z0); assert_eq!(saturating_cast(src_1), dst_p1); assert_eq!(saturating_cast(src_1_5), dst_p1); assert_eq!(saturating_cast(src_6), dst_p6); assert_eq!(saturating_cast(src_7ish), dst_p7ish); assert_eq!(saturating_cast(src_8), dst_p7); assert_eq!(saturating_cast(src_9ish), dst_p7); assert_eq!(saturating_cast(src_c), dst_p7); assert_eq!(saturating_cast(src_fish), dst_p7); assert_eq!(saturating_cast(src_max), dst_p7); assert_eq!(saturating_cast(src_inf), dst_p7); assert_eq!(wrapping_cast(-src_max), dst_wmax_neg); assert_eq!(wrapping_cast(-src_fish), dst_p1ish); assert_eq!(wrapping_cast(-src_c), dst_p4); assert_eq!(wrapping_cast(-src_9ish), dst_p7ish); assert_eq!(wrapping_cast(-src_8), dst_m8); assert_eq!(wrapping_cast(-src_7ish), dst_m7ish); assert_eq!(wrapping_cast(-src_6), dst_m6); assert_eq!(wrapping_cast(-src_1_5), dst_m1); assert_eq!(wrapping_cast(-src_1), dst_m1); assert_eq!(wrapping_cast(-src_0), dst_z0); assert_eq!(wrapping_cast(src_0), dst_z0); assert_eq!(wrapping_cast(src_1), dst_p1); assert_eq!(wrapping_cast(src_1_5), dst_p1); assert_eq!(wrapping_cast(src_6), dst_p6); assert_eq!(wrapping_cast(src_7ish), dst_p7ish); assert_eq!(wrapping_cast(src_8), dst_m8); assert_eq!(wrapping_cast(src_9ish), dst_m7ish); assert_eq!(wrapping_cast(src_c), dst_m4); assert_eq!(wrapping_cast(src_fish), dst_m1ish); assert_eq!(wrapping_cast(src_max), dst_wmax); assert_eq!(overflowing_cast(-src_max), (dst_wmax_neg, true)); assert_eq!(overflowing_cast(-src_fish), (dst_p1ish, true)); assert_eq!(overflowing_cast(-src_c), (dst_p4, true)); assert_eq!(overflowing_cast(-src_9ish), (dst_p7ish, true)); assert_eq!(overflowing_cast(-src_8), (dst_m8, false)); assert_eq!(overflowing_cast(-src_7ish), (dst_m7ish, false)); assert_eq!(overflowing_cast(-src_6), (dst_m6, false)); assert_eq!(overflowing_cast(-src_1_5), (dst_m1, false)); assert_eq!(overflowing_cast(-src_1), (dst_m1, false)); assert_eq!(overflowing_cast(-src_0), (dst_z0, false)); assert_eq!(overflowing_cast(src_0), (dst_z0, false)); assert_eq!(overflowing_cast(src_1), (dst_p1, false)); assert_eq!(overflowing_cast(src_1_5), (dst_p1, false)); assert_eq!(overflowing_cast(src_6), (dst_p6, false)); assert_eq!(overflowing_cast(src_7ish), (dst_p7ish, false)); assert_eq!(overflowing_cast(src_8), (dst_m8, true)); assert_eq!(overflowing_cast(src_9ish), (dst_m7ish, true)); assert_eq!(overflowing_cast(src_c), (dst_m4, true)); assert_eq!(overflowing_cast(src_fish), (dst_m1ish, true)); assert_eq!(overflowing_cast(src_max), (dst_wmax, true)); assert_eq!(unwrapped_cast(-src_8), dst_m8); assert_eq!(unwrapped_cast(-src_7ish), dst_m7ish); assert_eq!(unwrapped_cast(-src_6), dst_m6); assert_eq!(unwrapped_cast(-src_1_5), dst_m1); assert_eq!(unwrapped_cast(-src_1), dst_m1); assert_eq!(unwrapped_cast(-src_0), dst_z0); assert_eq!(unwrapped_cast(src_0), dst_z0); assert_eq!(unwrapped_cast(src_1), dst_p1); assert_eq!(unwrapped_cast(src_1_5), dst_p1); assert_eq!(unwrapped_cast(src_6), dst_p6); assert_eq!(unwrapped_cast(src_7ish), dst_p7ish); } fn float_to_wrapping_signed() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_prec = Src::prec(); let dst_nbits = mem::size_of::() * 8; let src_0 = Src::int_shl(0, 0); let src_1 = Src::int_shl(1, 0); let src_1_5 = Src::int_shl(3, -1); let src_8 = Src::int_shl(1, (dst_nbits - 1) as i8); let src_1ish = if src_prec > dst_nbits { src_1 } else { Src::int_shl(1, (dst_nbits - src_prec) as i8) }; let src_7ish = src_8 - src_1ish; let src_6 = Src::int_shl(6, (dst_nbits - 4) as i8); let src_9ish = src_8 + src_1ish; let src_c = Src::int_shl(12, (dst_nbits - 4) as i8); let src_fish = src_8 + src_7ish; let src_max = Src::max(); let src_inf = Src::inf(); let src_nan = Src::nan(); let dst_z0 = Wrapping(wrapping_cast(src_0)); let dst_p1 = Wrapping(wrapping_cast(src_1)); let dst_m1 = Wrapping(wrapping_cast(-src_1)); let dst_p1ish = Wrapping(wrapping_cast(-src_fish)); let dst_m1ish = Wrapping(wrapping_cast(src_fish)); let dst_p4 = Wrapping(wrapping_cast(-src_c)); let dst_p6 = Wrapping(wrapping_cast(src_6)); let dst_p7ish = Wrapping(wrapping_cast(src_7ish)); let dst_m4 = Wrapping(wrapping_cast(src_c)); let dst_m6 = Wrapping(wrapping_cast(-src_6)); let dst_m8 = Wrapping(wrapping_cast(-src_8)); let dst_m7ish = Wrapping(wrapping_cast(-src_7ish)); let dst_wmax = Wrapping(wrapping_cast(src_max)); let dst_wmax_neg = Wrapping(wrapping_cast(-src_max)); assert_eq!(cast(-src_max), dst_wmax_neg); assert_eq!(cast(-src_fish), dst_p1ish); assert_eq!(cast(-src_c), dst_p4); assert_eq!(cast(-src_9ish), dst_p7ish); assert_eq!(cast(-src_8), dst_m8); assert_eq!(cast(-src_7ish), dst_m7ish); assert_eq!(cast(-src_6), dst_m6); assert_eq!(cast(-src_1_5), dst_m1); assert_eq!(cast(-src_1), dst_m1); assert_eq!(cast(-src_0), dst_z0); assert_eq!(cast(src_0), dst_z0); assert_eq!(cast(src_1), dst_p1); assert_eq!(cast(src_1_5), dst_p1); assert_eq!(cast(src_6), dst_p6); assert_eq!(cast(src_7ish), dst_p7ish); assert_eq!(cast(src_8), dst_m8); assert_eq!(cast(src_9ish), dst_m7ish); assert_eq!(cast(src_c), dst_m4); assert_eq!(cast(src_fish), dst_m1ish); assert_eq!(cast(src_max), dst_wmax); assert_eq!(checked_cast(-src_nan), None); assert_eq!(checked_cast(-src_inf), None); assert_eq!(checked_cast(-src_max), Some(dst_wmax_neg)); assert_eq!(checked_cast(-src_fish), Some(dst_p1ish)); assert_eq!(checked_cast(-src_c), Some(dst_p4)); assert_eq!(checked_cast(-src_9ish), Some(dst_p7ish)); assert_eq!(checked_cast(-src_8), Some(dst_m8)); assert_eq!(checked_cast(-src_7ish), Some(dst_m7ish)); assert_eq!(checked_cast(-src_6), Some(dst_m6)); assert_eq!(checked_cast(-src_1_5), Some(dst_m1)); assert_eq!(checked_cast(-src_1), Some(dst_m1)); assert_eq!(checked_cast(-src_0), Some(dst_z0)); assert_eq!(checked_cast(src_0), Some(dst_z0)); assert_eq!(checked_cast(src_1), Some(dst_p1)); assert_eq!(checked_cast(src_1_5), Some(dst_p1)); assert_eq!(checked_cast(src_6), Some(dst_p6)); assert_eq!(checked_cast(src_7ish), Some(dst_p7ish)); assert_eq!(checked_cast(src_8), Some(dst_m8)); assert_eq!(checked_cast(src_9ish), Some(dst_m7ish)); assert_eq!(checked_cast(src_c), Some(dst_m4)); assert_eq!(checked_cast(src_fish), Some(dst_m1ish)); assert_eq!(checked_cast(src_max), Some(dst_wmax)); assert_eq!(checked_cast(src_inf), None); assert_eq!(checked_cast(src_nan), None); assert_eq!(unwrapped_cast(-src_max), dst_wmax_neg); assert_eq!(unwrapped_cast(-src_fish), dst_p1ish); assert_eq!(unwrapped_cast(-src_c), dst_p4); assert_eq!(unwrapped_cast(-src_9ish), dst_p7ish); assert_eq!(unwrapped_cast(-src_8), dst_m8); assert_eq!(unwrapped_cast(-src_7ish), dst_m7ish); assert_eq!(unwrapped_cast(-src_6), dst_m6); assert_eq!(unwrapped_cast(-src_1_5), dst_m1); assert_eq!(unwrapped_cast(-src_1), dst_m1); assert_eq!(unwrapped_cast(-src_0), dst_z0); assert_eq!(unwrapped_cast(src_0), dst_z0); assert_eq!(unwrapped_cast(src_1), dst_p1); assert_eq!(unwrapped_cast(src_1_5), dst_p1); assert_eq!(unwrapped_cast(src_6), dst_p6); assert_eq!(unwrapped_cast(src_7ish), dst_p7ish); assert_eq!(unwrapped_cast(src_8), dst_m8); assert_eq!(unwrapped_cast(src_9ish), dst_m7ish); assert_eq!(unwrapped_cast(src_c), dst_m4); assert_eq!(unwrapped_cast(src_fish), dst_m1ish); assert_eq!(unwrapped_cast(src_max), dst_wmax); } fn round_to_nonwrapping_signed() where Round: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_prec = Src::prec(); let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; let src_0 = Src::int_shl(0, 0); let src_1 = Src::int_shl(1, 0); let src_1_5 = Src::int_shl(3, -1); let src_8 = Src::int_shl(1, (dst_nbits - 1) as i8); let src_1ish = if src_prec > dst_nbits { src_1 } else { Src::int_shl(1, (dst_nbits - src_prec) as i8) }; let src_7ish = src_8 - src_1ish; let src_6 = Src::int_shl(6, (dst_nbits - 4) as i8); let src_9ish = src_8 + src_1ish; let src_c = Src::int_shl(12, (dst_nbits - 4) as i8); let src_fish = src_8 + src_7ish; let src_max = Src::max(); let src_inf = Src::inf(); let src_nan = Src::nan(); let dst_z0 = Dst::zero(); let dst_p1 = Dst::one(); let dst_p2 = dst_p1 + dst_p1; let dst_m1 = !dst_z0; let dst_m2 = dst_m1 + dst_m1; let (dst_p1ish, dst_m1ish) = if src_prec > dst_nbits { (dst_p1, dst_m1) } else { ( dst_p1 << (dst_nbits - src_prec), dst_m1 << (dst_nbits - src_prec), ) }; let dst_p4 = dst_p1 << (dst_nbits - 2); let dst_p6 = dst_p4 + (dst_p4 >> 1); let dst_p7ish = dst_p4 + (dst_p4 - dst_p1ish); let dst_m4 = dst_z0 - dst_p4; let dst_m6 = dst_z0 - dst_p6; let dst_m8 = dst_m4 + dst_m4; let dst_m7ish = dst_m8 + dst_p1ish; let dst_p7 = !dst_m8; let (dst_wmax, dst_wmax_neg) = if src_nbits == 32 && dst_nbits == 128 { let max = !dst_z0 << (dst_nbits - src_prec); (max, !max + dst_p1) } else { (dst_z0, dst_z0) }; assert_eq!(cast(Round(-src_8)), dst_m8); assert_eq!(cast(Round(-src_7ish)), dst_m7ish); assert_eq!(cast(Round(-src_6)), dst_m6); assert_eq!(cast(Round(-src_1_5)), dst_m2); assert_eq!(cast(Round(-src_1)), dst_m1); assert_eq!(cast(Round(-src_0)), dst_z0); assert_eq!(cast(Round(src_0)), dst_z0); assert_eq!(cast(Round(src_1)), dst_p1); assert_eq!(cast(Round(src_1_5)), dst_p2); assert_eq!(cast(Round(src_6)), dst_p6); assert_eq!(cast(Round(src_7ish)), dst_p7ish); assert_eq!(checked_cast(Round(-src_nan)), None); assert_eq!(checked_cast(Round(-src_inf)), None); assert_eq!(checked_cast(Round(-src_max)), None); assert_eq!(checked_cast(Round(-src_fish)), None); assert_eq!(checked_cast(Round(-src_c)), None); assert_eq!(checked_cast(Round(-src_9ish)), None); assert_eq!(checked_cast(Round(-src_8)), Some(dst_m8)); assert_eq!(checked_cast(Round(-src_7ish)), Some(dst_m7ish)); assert_eq!(checked_cast(Round(-src_6)), Some(dst_m6)); assert_eq!(checked_cast(Round(-src_1_5)), Some(dst_m2)); assert_eq!(checked_cast(Round(-src_1)), Some(dst_m1)); assert_eq!(checked_cast(Round(-src_0)), Some(dst_z0)); assert_eq!(checked_cast(Round(src_0)), Some(dst_z0)); assert_eq!(checked_cast(Round(src_1)), Some(dst_p1)); assert_eq!(checked_cast(Round(src_1_5)), Some(dst_p2)); assert_eq!(checked_cast(Round(src_6)), Some(dst_p6)); assert_eq!(checked_cast(Round(src_7ish)), Some(dst_p7ish)); assert_eq!(checked_cast(Round(src_8)), None); assert_eq!(checked_cast(Round(src_9ish)), None); assert_eq!(checked_cast(Round(src_c)), None); assert_eq!(checked_cast(Round(src_fish)), None); assert_eq!(checked_cast(Round(src_max)), None); assert_eq!(checked_cast(Round(src_inf)), None); assert_eq!(checked_cast(Round(src_nan)), None); assert_eq!(saturating_cast(Round(-src_inf)), dst_m8); assert_eq!(saturating_cast(Round(-src_max)), dst_m8); assert_eq!(saturating_cast(Round(-src_fish)), dst_m8); assert_eq!(saturating_cast(Round(-src_c)), dst_m8); assert_eq!(saturating_cast(Round(-src_9ish)), dst_m8); assert_eq!(saturating_cast(Round(-src_8)), dst_m8); assert_eq!(saturating_cast(Round(-src_7ish)), dst_m7ish); assert_eq!(saturating_cast(Round(-src_6)), dst_m6); assert_eq!(saturating_cast(Round(-src_1_5)), dst_m2); assert_eq!(saturating_cast(Round(-src_1)), dst_m1); assert_eq!(saturating_cast(Round(-src_0)), dst_z0); assert_eq!(saturating_cast(Round(src_0)), dst_z0); assert_eq!(saturating_cast(Round(src_1)), dst_p1); assert_eq!(saturating_cast(Round(src_1_5)), dst_p2); assert_eq!(saturating_cast(Round(src_6)), dst_p6); assert_eq!(saturating_cast(Round(src_7ish)), dst_p7ish); assert_eq!(saturating_cast(Round(src_8)), dst_p7); assert_eq!(saturating_cast(Round(src_9ish)), dst_p7); assert_eq!(saturating_cast(Round(src_c)), dst_p7); assert_eq!(saturating_cast(Round(src_fish)), dst_p7); assert_eq!(saturating_cast(Round(src_max)), dst_p7); assert_eq!(saturating_cast(Round(src_inf)), dst_p7); assert_eq!(wrapping_cast(Round(-src_max)), dst_wmax_neg); assert_eq!(wrapping_cast(Round(-src_fish)), dst_p1ish); assert_eq!(wrapping_cast(Round(-src_c)), dst_p4); assert_eq!(wrapping_cast(Round(-src_9ish)), dst_p7ish); assert_eq!(wrapping_cast(Round(-src_8)), dst_m8); assert_eq!(wrapping_cast(Round(-src_7ish)), dst_m7ish); assert_eq!(wrapping_cast(Round(-src_6)), dst_m6); assert_eq!(wrapping_cast(Round(-src_1_5)), dst_m2); assert_eq!(wrapping_cast(Round(-src_1)), dst_m1); assert_eq!(wrapping_cast(Round(-src_0)), dst_z0); assert_eq!(wrapping_cast(Round(src_0)), dst_z0); assert_eq!(wrapping_cast(Round(src_1)), dst_p1); assert_eq!(wrapping_cast(Round(src_1_5)), dst_p2); assert_eq!(wrapping_cast(Round(src_6)), dst_p6); assert_eq!(wrapping_cast(Round(src_7ish)), dst_p7ish); assert_eq!(wrapping_cast(Round(src_8)), dst_m8); assert_eq!(wrapping_cast(Round(src_9ish)), dst_m7ish); assert_eq!(wrapping_cast(Round(src_c)), dst_m4); assert_eq!(wrapping_cast(Round(src_fish)), dst_m1ish); assert_eq!(wrapping_cast(Round(src_max)), dst_wmax); assert_eq!(overflowing_cast(Round(-src_max)), (dst_wmax_neg, true)); assert_eq!(overflowing_cast(Round(-src_fish)), (dst_p1ish, true)); assert_eq!(overflowing_cast(Round(-src_c)), (dst_p4, true)); assert_eq!(overflowing_cast(Round(-src_9ish)), (dst_p7ish, true)); assert_eq!(overflowing_cast(Round(-src_8)), (dst_m8, false)); assert_eq!(overflowing_cast(Round(-src_7ish)), (dst_m7ish, false)); assert_eq!(overflowing_cast(Round(-src_6)), (dst_m6, false)); assert_eq!(overflowing_cast(Round(-src_1_5)), (dst_m2, false)); assert_eq!(overflowing_cast(Round(-src_1)), (dst_m1, false)); assert_eq!(overflowing_cast(Round(-src_0)), (dst_z0, false)); assert_eq!(overflowing_cast(Round(src_0)), (dst_z0, false)); assert_eq!(overflowing_cast(Round(src_1)), (dst_p1, false)); assert_eq!(overflowing_cast(Round(src_1_5)), (dst_p2, false)); assert_eq!(overflowing_cast(Round(src_6)), (dst_p6, false)); assert_eq!(overflowing_cast(Round(src_7ish)), (dst_p7ish, false)); assert_eq!(overflowing_cast(Round(src_8)), (dst_m8, true)); assert_eq!(overflowing_cast(Round(src_9ish)), (dst_m7ish, true)); assert_eq!(overflowing_cast(Round(src_c)), (dst_m4, true)); assert_eq!(overflowing_cast(Round(src_fish)), (dst_m1ish, true)); assert_eq!(overflowing_cast(Round(src_max)), (dst_wmax, true)); assert_eq!(unwrapped_cast(Round(-src_8)), dst_m8); assert_eq!(unwrapped_cast(Round(-src_7ish)), dst_m7ish); assert_eq!(unwrapped_cast(Round(-src_6)), dst_m6); assert_eq!(unwrapped_cast(Round(-src_1_5)), dst_m2); assert_eq!(unwrapped_cast(Round(-src_1)), dst_m1); assert_eq!(unwrapped_cast(Round(-src_0)), dst_z0); assert_eq!(unwrapped_cast(Round(src_0)), dst_z0); assert_eq!(unwrapped_cast(Round(src_1)), dst_p1); assert_eq!(unwrapped_cast(Round(src_1_5)), dst_p2); assert_eq!(unwrapped_cast(Round(src_6)), dst_p6); assert_eq!(unwrapped_cast(Round(src_7ish)), dst_p7ish); } fn round_to_wrapping_signed() where Round: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_prec = Src::prec(); let dst_nbits = mem::size_of::() * 8; let src_0 = Src::int_shl(0, 0); let src_1 = Src::int_shl(1, 0); let src_1_5 = Src::int_shl(3, -1); let src_8 = Src::int_shl(1, (dst_nbits - 1) as i8); let src_1ish = if src_prec > dst_nbits { src_1 } else { Src::int_shl(1, (dst_nbits - src_prec) as i8) }; let src_7ish = src_8 - src_1ish; let src_6 = Src::int_shl(6, (dst_nbits - 4) as i8); let src_9ish = src_8 + src_1ish; let src_c = Src::int_shl(12, (dst_nbits - 4) as i8); let src_fish = src_8 + src_7ish; let src_max = Src::max(); let src_inf = Src::inf(); let src_nan = Src::nan(); let dst_z0 = Wrapping(wrapping_cast(Round(src_0))); let dst_p1 = Wrapping(wrapping_cast(Round(src_1))); let dst_p2 = Wrapping(wrapping_cast(Round(src_1_5))); let dst_m1 = Wrapping(wrapping_cast(Round(-src_1))); let dst_m2 = Wrapping(wrapping_cast(Round(-src_1_5))); let dst_p1ish = Wrapping(wrapping_cast(Round(-src_fish))); let dst_m1ish = Wrapping(wrapping_cast(Round(src_fish))); let dst_p4 = Wrapping(wrapping_cast(Round(-src_c))); let dst_p6 = Wrapping(wrapping_cast(Round(src_6))); let dst_p7ish = Wrapping(wrapping_cast(Round(src_7ish))); let dst_m4 = Wrapping(wrapping_cast(Round(src_c))); let dst_m6 = Wrapping(wrapping_cast(Round(-src_6))); let dst_m8 = Wrapping(wrapping_cast(Round(-src_8))); let dst_m7ish = Wrapping(wrapping_cast(Round(-src_7ish))); let dst_wmax = Wrapping(wrapping_cast(Round(src_max))); let dst_wmax_neg = Wrapping(wrapping_cast(Round(-src_max))); assert_eq!(cast(Round(-src_max)), dst_wmax_neg); assert_eq!(cast(Round(-src_fish)), dst_p1ish); assert_eq!(cast(Round(-src_c)), dst_p4); assert_eq!(cast(Round(-src_9ish)), dst_p7ish); assert_eq!(cast(Round(-src_8)), dst_m8); assert_eq!(cast(Round(-src_7ish)), dst_m7ish); assert_eq!(cast(Round(-src_6)), dst_m6); assert_eq!(cast(Round(-src_1_5)), dst_m2); assert_eq!(cast(Round(-src_1)), dst_m1); assert_eq!(cast(Round(-src_0)), dst_z0); assert_eq!(cast(Round(src_0)), dst_z0); assert_eq!(cast(Round(src_1)), dst_p1); assert_eq!(cast(Round(src_1_5)), dst_p2); assert_eq!(cast(Round(src_6)), dst_p6); assert_eq!(cast(Round(src_7ish)), dst_p7ish); assert_eq!(cast(Round(src_8)), dst_m8); assert_eq!(cast(Round(src_9ish)), dst_m7ish); assert_eq!(cast(Round(src_c)), dst_m4); assert_eq!(cast(Round(src_fish)), dst_m1ish); assert_eq!(cast(Round(src_max)), dst_wmax); assert_eq!(checked_cast(Round(-src_nan)), None); assert_eq!(checked_cast(Round(-src_inf)), None); assert_eq!(checked_cast(Round(-src_max)), Some(dst_wmax_neg)); assert_eq!(checked_cast(Round(-src_fish)), Some(dst_p1ish)); assert_eq!(checked_cast(Round(-src_c)), Some(dst_p4)); assert_eq!(checked_cast(Round(-src_9ish)), Some(dst_p7ish)); assert_eq!(checked_cast(Round(-src_8)), Some(dst_m8)); assert_eq!(checked_cast(Round(-src_7ish)), Some(dst_m7ish)); assert_eq!(checked_cast(Round(-src_6)), Some(dst_m6)); assert_eq!(checked_cast(Round(-src_1_5)), Some(dst_m2)); assert_eq!(checked_cast(Round(-src_1)), Some(dst_m1)); assert_eq!(checked_cast(Round(-src_0)), Some(dst_z0)); assert_eq!(checked_cast(Round(src_0)), Some(dst_z0)); assert_eq!(checked_cast(Round(src_1)), Some(dst_p1)); assert_eq!(checked_cast(Round(src_1_5)), Some(dst_p2)); assert_eq!(checked_cast(Round(src_6)), Some(dst_p6)); assert_eq!(checked_cast(Round(src_7ish)), Some(dst_p7ish)); assert_eq!(checked_cast(Round(src_8)), Some(dst_m8)); assert_eq!(checked_cast(Round(src_9ish)), Some(dst_m7ish)); assert_eq!(checked_cast(Round(src_c)), Some(dst_m4)); assert_eq!(checked_cast(Round(src_fish)), Some(dst_m1ish)); assert_eq!(checked_cast(Round(src_max)), Some(dst_wmax)); assert_eq!(checked_cast(Round(src_inf)), None); assert_eq!(checked_cast(Round(src_nan)), None); assert_eq!(unwrapped_cast(Round(-src_max)), dst_wmax_neg); assert_eq!(unwrapped_cast(Round(-src_fish)), dst_p1ish); assert_eq!(unwrapped_cast(Round(-src_c)), dst_p4); assert_eq!(unwrapped_cast(Round(-src_9ish)), dst_p7ish); assert_eq!(unwrapped_cast(Round(-src_8)), dst_m8); assert_eq!(unwrapped_cast(Round(-src_7ish)), dst_m7ish); assert_eq!(unwrapped_cast(Round(-src_6)), dst_m6); assert_eq!(unwrapped_cast(Round(-src_1_5)), dst_m2); assert_eq!(unwrapped_cast(Round(-src_1)), dst_m1); assert_eq!(unwrapped_cast(Round(-src_0)), dst_z0); assert_eq!(unwrapped_cast(Round(src_0)), dst_z0); assert_eq!(unwrapped_cast(Round(src_1)), dst_p1); assert_eq!(unwrapped_cast(Round(src_1_5)), dst_p2); assert_eq!(unwrapped_cast(Round(src_6)), dst_p6); assert_eq!(unwrapped_cast(Round(src_7ish)), dst_p7ish); assert_eq!(unwrapped_cast(Round(src_8)), dst_m8); assert_eq!(unwrapped_cast(Round(src_9ish)), dst_m7ish); assert_eq!(unwrapped_cast(Round(src_c)), dst_m4); assert_eq!(unwrapped_cast(Round(src_fish)), dst_m1ish); assert_eq!(unwrapped_cast(Round(src_max)), dst_wmax); } fn float_to_nonwrapping_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_prec = Src::prec(); let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; let src_0 = Src::int_shl(0, 0); let src_1 = Src::int_shl(1, 0); let src_1_5 = Src::int_shl(3, -1); let src_8 = Src::int_shl(1, (dst_nbits - 1) as i8); let src_1ish = if src_prec > dst_nbits { src_1 } else { Src::int_shl(1, (dst_nbits - src_prec) as i8) }; let src_7ish = src_8 - src_1ish; let src_6 = Src::int_shl(6, (dst_nbits - 4) as i8); let src_9ish = src_8 + src_1ish; let src_c = Src::int_shl(12, (dst_nbits - 4) as i8); let src_fish = src_8 + src_7ish; let src_max = Src::max(); let src_inf = Src::inf(); let src_nan = Src::nan(); let dst_0 = Dst::zero(); let dst_1 = Dst::one(); let (dst_1ish, dst_fish) = if src_prec > dst_nbits { (dst_1, !dst_1 + dst_1) } else { let ish = dst_1 << (dst_nbits - src_prec); (ish, !ish + dst_1) }; let dst_4 = dst_1 << (dst_nbits - 2); let dst_6 = dst_4 + (dst_4 >> 1); let dst_7ish = dst_4 + (dst_4 - dst_1ish); let dst_8 = dst_1 << (dst_nbits - 1); let dst_7 = !dst_8; let dst_9ish = dst_8 + dst_1ish; let dst_a = dst_6 + dst_4; let dst_c = dst_8 + dst_4; let dst_f = dst_8 + dst_7; let (dst_wmax, dst_wmax_neg) = if src_nbits == 32 && dst_nbits == 128 { let max = !dst_0 << (dst_nbits - src_prec); (max, !max + dst_1) } else { (dst_0, dst_0) }; assert_eq!(cast(-src_0), dst_0); assert_eq!(cast(src_0), dst_0); assert_eq!(cast(src_1), dst_1); assert_eq!(cast(src_1_5), dst_1); assert_eq!(cast(src_6), dst_6); assert_eq!(cast(src_7ish), dst_7ish); assert_eq!(cast(src_8), dst_8); assert_eq!(cast(src_9ish), dst_9ish); assert_eq!(cast(src_c), dst_c); assert_eq!(cast(src_fish), dst_fish); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(cast(src_max), dst_wmax); } assert_eq!(checked_cast(-src_nan), None); assert_eq!(checked_cast(-src_inf), None); assert_eq!(checked_cast(-src_max), None); assert_eq!(checked_cast(-src_fish), None); assert_eq!(checked_cast(-src_c), None); assert_eq!(checked_cast(-src_9ish), None); assert_eq!(checked_cast(-src_8), None); assert_eq!(checked_cast(-src_7ish), None); assert_eq!(checked_cast(-src_6), None); assert_eq!(checked_cast(-src_1_5), None); assert_eq!(checked_cast(-src_1), None); assert_eq!(checked_cast(-src_0), Some(dst_0)); assert_eq!(checked_cast(src_0), Some(dst_0)); assert_eq!(checked_cast(src_1), Some(dst_1)); assert_eq!(checked_cast(src_1_5), Some(dst_1)); assert_eq!(checked_cast(src_6), Some(dst_6)); assert_eq!(checked_cast(src_7ish), Some(dst_7ish)); assert_eq!(checked_cast(src_8), Some(dst_8)); assert_eq!(checked_cast(src_9ish), Some(dst_9ish)); assert_eq!(checked_cast(src_c), Some(dst_c)); assert_eq!(checked_cast(src_fish), Some(dst_fish)); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(checked_cast(src_max), Some(dst_wmax)); } else { assert_eq!(checked_cast(src_max), None); } assert_eq!(checked_cast(src_inf), None); assert_eq!(checked_cast(src_nan), None); assert_eq!(saturating_cast(-src_inf), dst_0); assert_eq!(saturating_cast(-src_max), dst_0); assert_eq!(saturating_cast(-src_fish), dst_0); assert_eq!(saturating_cast(-src_c), dst_0); assert_eq!(saturating_cast(-src_9ish), dst_0); assert_eq!(saturating_cast(-src_8), dst_0); assert_eq!(saturating_cast(-src_7ish), dst_0); assert_eq!(saturating_cast(-src_6), dst_0); assert_eq!(saturating_cast(-src_1_5), dst_0); assert_eq!(saturating_cast(-src_1), dst_0); assert_eq!(saturating_cast(-src_0), dst_0); assert_eq!(saturating_cast(src_0), dst_0); assert_eq!(saturating_cast(src_1), dst_1); assert_eq!(saturating_cast(src_1_5), dst_1); assert_eq!(saturating_cast(src_6), dst_6); assert_eq!(saturating_cast(src_7ish), dst_7ish); assert_eq!(saturating_cast(src_8), dst_8); assert_eq!(saturating_cast(src_9ish), dst_9ish); assert_eq!(saturating_cast(src_c), dst_c); assert_eq!(saturating_cast(src_fish), dst_fish); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(saturating_cast(src_max), dst_wmax); } else { assert_eq!(saturating_cast(src_max), dst_f); } assert_eq!(saturating_cast(src_inf), dst_f); assert_eq!(wrapping_cast(-src_max), dst_wmax_neg); assert_eq!(wrapping_cast(-src_fish), dst_1ish); assert_eq!(wrapping_cast(-src_c), dst_4); assert_eq!(wrapping_cast(-src_9ish), dst_7ish); assert_eq!(wrapping_cast(-src_8), dst_8); assert_eq!(wrapping_cast(-src_7ish), dst_9ish); assert_eq!(wrapping_cast(-src_6), dst_a); assert_eq!(wrapping_cast(-src_1_5), dst_f); assert_eq!(wrapping_cast(-src_1), dst_f); assert_eq!(wrapping_cast(-src_0), dst_0); assert_eq!(wrapping_cast(src_0), dst_0); assert_eq!(wrapping_cast(src_1), dst_1); assert_eq!(wrapping_cast(src_1_5), dst_1); assert_eq!(wrapping_cast(src_6), dst_6); assert_eq!(wrapping_cast(src_7ish), dst_7ish); assert_eq!(wrapping_cast(src_8), dst_8); assert_eq!(wrapping_cast(src_9ish), dst_9ish); assert_eq!(wrapping_cast(src_c), dst_c); assert_eq!(wrapping_cast(src_fish), dst_fish); assert_eq!(wrapping_cast(src_max), dst_wmax); assert_eq!(overflowing_cast(-src_max), (dst_wmax_neg, true)); assert_eq!(overflowing_cast(-src_fish), (dst_1ish, true)); assert_eq!(overflowing_cast(-src_c), (dst_4, true)); assert_eq!(overflowing_cast(-src_9ish), (dst_7ish, true)); assert_eq!(overflowing_cast(-src_8), (dst_8, true)); assert_eq!(overflowing_cast(-src_7ish), (dst_9ish, true)); assert_eq!(overflowing_cast(-src_6), (dst_a, true)); assert_eq!(overflowing_cast(-src_1_5), (dst_f, true)); assert_eq!(overflowing_cast(-src_1), (dst_f, true)); assert_eq!(overflowing_cast(-src_0), (dst_0, false)); assert_eq!(overflowing_cast(src_0), (dst_0, false)); assert_eq!(overflowing_cast(src_1), (dst_1, false)); assert_eq!(overflowing_cast(src_1_5), (dst_1, false)); assert_eq!(overflowing_cast(src_6), (dst_6, false)); assert_eq!(overflowing_cast(src_7ish), (dst_7ish, false)); assert_eq!(overflowing_cast(src_8), (dst_8, false)); assert_eq!(overflowing_cast(src_9ish), (dst_9ish, false)); assert_eq!(overflowing_cast(src_c), (dst_c, false)); assert_eq!(overflowing_cast(src_fish), (dst_fish, false)); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(overflowing_cast(src_max), (dst_wmax, false)); } else { assert_eq!(overflowing_cast(src_max), (dst_wmax, true)); } assert_eq!(unwrapped_cast(-src_0), dst_0); assert_eq!(unwrapped_cast(src_0), dst_0); assert_eq!(unwrapped_cast(src_1), dst_1); assert_eq!(unwrapped_cast(src_1_5), dst_1); assert_eq!(unwrapped_cast(src_6), dst_6); assert_eq!(unwrapped_cast(src_7ish), dst_7ish); assert_eq!(unwrapped_cast(src_8), dst_8); assert_eq!(unwrapped_cast(src_9ish), dst_9ish); assert_eq!(unwrapped_cast(src_c), dst_c); assert_eq!(unwrapped_cast(src_fish), dst_fish); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(unwrapped_cast(src_max), dst_wmax); } } fn float_to_wrapping_unsigned() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_prec = Src::prec(); let dst_nbits = mem::size_of::() * 8; let src_0 = Src::int_shl(0, 0); let src_1 = Src::int_shl(1, 0); let src_1_5 = Src::int_shl(3, -1); let src_8 = Src::int_shl(1, (dst_nbits - 1) as i8); let src_1ish = if src_prec > dst_nbits { src_1 } else { Src::int_shl(1, (dst_nbits - src_prec) as i8) }; let src_7ish = src_8 - src_1ish; let src_6 = Src::int_shl(6, (dst_nbits - 4) as i8); let src_9ish = src_8 + src_1ish; let src_c = Src::int_shl(12, (dst_nbits - 4) as i8); let src_fish = src_8 + src_7ish; let src_max = Src::max(); let src_inf = Src::inf(); let src_nan = Src::nan(); let dst_0 = Wrapping(wrapping_cast(src_0)); let dst_1 = Wrapping(wrapping_cast(src_1)); let dst_1ish = Wrapping(wrapping_cast(-src_fish)); let dst_fish = Wrapping(wrapping_cast(src_fish)); let dst_4 = Wrapping(wrapping_cast(-src_c)); let dst_6 = Wrapping(wrapping_cast(src_6)); let dst_7ish = Wrapping(wrapping_cast(src_7ish)); let dst_8 = Wrapping(wrapping_cast(src_8)); let dst_9ish = Wrapping(wrapping_cast(src_9ish)); let dst_a = Wrapping(wrapping_cast(-src_6)); let dst_c = Wrapping(wrapping_cast(src_c)); let dst_f = Wrapping(wrapping_cast(-src_1)); let dst_wmax = Wrapping(wrapping_cast(src_max)); let dst_wmax_neg = Wrapping(wrapping_cast(-src_max)); assert_eq!(cast(-src_max), dst_wmax_neg); assert_eq!(cast(-src_fish), dst_1ish); assert_eq!(cast(-src_c), dst_4); assert_eq!(cast(-src_9ish), dst_7ish); assert_eq!(cast(-src_8), dst_8); assert_eq!(cast(-src_7ish), dst_9ish); assert_eq!(cast(-src_6), dst_a); assert_eq!(cast(-src_1_5), dst_f); assert_eq!(cast(-src_1), dst_f); assert_eq!(cast(-src_0), dst_0); assert_eq!(cast(src_0), dst_0); assert_eq!(cast(src_1), dst_1); assert_eq!(cast(src_1_5), dst_1); assert_eq!(cast(src_6), dst_6); assert_eq!(cast(src_7ish), dst_7ish); assert_eq!(cast(src_8), dst_8); assert_eq!(cast(src_9ish), dst_9ish); assert_eq!(cast(src_c), dst_c); assert_eq!(cast(src_fish), dst_fish); assert_eq!(cast(src_max), dst_wmax); assert_eq!(checked_cast(-src_nan), None); assert_eq!(checked_cast(-src_inf), None); assert_eq!(checked_cast(-src_max), Some(dst_wmax_neg)); assert_eq!(checked_cast(-src_fish), Some(dst_1ish)); assert_eq!(checked_cast(-src_c), Some(dst_4)); assert_eq!(checked_cast(-src_9ish), Some(dst_7ish)); assert_eq!(checked_cast(-src_8), Some(dst_8)); assert_eq!(checked_cast(-src_7ish), Some(dst_9ish)); assert_eq!(checked_cast(-src_6), Some(dst_a)); assert_eq!(checked_cast(-src_1_5), Some(dst_f)); assert_eq!(checked_cast(-src_1), Some(dst_f)); assert_eq!(checked_cast(-src_0), Some(dst_0)); assert_eq!(checked_cast(src_0), Some(dst_0)); assert_eq!(checked_cast(src_1), Some(dst_1)); assert_eq!(checked_cast(src_1_5), Some(dst_1)); assert_eq!(checked_cast(src_6), Some(dst_6)); assert_eq!(checked_cast(src_7ish), Some(dst_7ish)); assert_eq!(checked_cast(src_8), Some(dst_8)); assert_eq!(checked_cast(src_9ish), Some(dst_9ish)); assert_eq!(checked_cast(src_c), Some(dst_c)); assert_eq!(checked_cast(src_fish), Some(dst_fish)); assert_eq!(checked_cast(src_max), Some(dst_wmax)); assert_eq!(checked_cast(src_inf), None); assert_eq!(checked_cast(src_nan), None); assert_eq!(unwrapped_cast(-src_max), dst_wmax_neg); assert_eq!(unwrapped_cast(-src_fish), dst_1ish); assert_eq!(unwrapped_cast(-src_c), dst_4); assert_eq!(unwrapped_cast(-src_9ish), dst_7ish); assert_eq!(unwrapped_cast(-src_8), dst_8); assert_eq!(unwrapped_cast(-src_7ish), dst_9ish); assert_eq!(unwrapped_cast(-src_6), dst_a); assert_eq!(unwrapped_cast(-src_1_5), dst_f); assert_eq!(unwrapped_cast(-src_1), dst_f); assert_eq!(unwrapped_cast(-src_0), dst_0); assert_eq!(unwrapped_cast(src_0), dst_0); assert_eq!(unwrapped_cast(src_1), dst_1); assert_eq!(unwrapped_cast(src_1_5), dst_1); assert_eq!(unwrapped_cast(src_6), dst_6); assert_eq!(unwrapped_cast(src_7ish), dst_7ish); assert_eq!(unwrapped_cast(src_8), dst_8); assert_eq!(unwrapped_cast(src_9ish), dst_9ish); assert_eq!(unwrapped_cast(src_c), dst_c); assert_eq!(unwrapped_cast(src_fish), dst_fish); assert_eq!(unwrapped_cast(src_max), dst_wmax); } fn round_to_nonwrapping_unsigned() where Round: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_prec = Src::prec(); let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; let src_0 = Src::int_shl(0, 0); let src_1 = Src::int_shl(1, 0); let src_1_5 = Src::int_shl(3, -1); let src_8 = Src::int_shl(1, (dst_nbits - 1) as i8); let src_1ish = if src_prec > dst_nbits { src_1 } else { Src::int_shl(1, (dst_nbits - src_prec) as i8) }; let src_7ish = src_8 - src_1ish; let src_6 = Src::int_shl(6, (dst_nbits - 4) as i8); let src_9ish = src_8 + src_1ish; let src_c = Src::int_shl(12, (dst_nbits - 4) as i8); let src_fish = src_8 + src_7ish; let src_max = Src::max(); let src_inf = Src::inf(); let src_nan = Src::nan(); let dst_0 = Dst::zero(); let dst_1 = Dst::one(); let dst_2 = dst_1 + dst_1; let (dst_1ish, dst_fish) = if src_prec > dst_nbits { (dst_1, !dst_1 + dst_1) } else { let ish = dst_1 << (dst_nbits - src_prec); (ish, !ish + dst_1) }; let dst_4 = dst_1 << (dst_nbits - 2); let dst_6 = dst_4 + (dst_4 >> 1); let dst_7ish = dst_4 + (dst_4 - dst_1ish); let dst_8 = dst_1 << (dst_nbits - 1); let dst_7 = !dst_8; let dst_9ish = dst_8 + dst_1ish; let dst_a = dst_6 + dst_4; let dst_c = dst_8 + dst_4; let dst_e = dst_7 + dst_7; let dst_f = dst_8 + dst_7; let (dst_wmax, dst_wmax_neg) = if src_nbits == 32 && dst_nbits == 128 { let max = !dst_0 << (dst_nbits - src_prec); (max, !max + dst_1) } else { (dst_0, dst_0) }; assert_eq!(cast(Round(-src_0)), dst_0); assert_eq!(cast(Round(src_0)), dst_0); assert_eq!(cast(Round(src_1)), dst_1); assert_eq!(cast(Round(src_1_5)), dst_2); assert_eq!(cast(Round(src_6)), dst_6); assert_eq!(cast(Round(src_7ish)), dst_7ish); assert_eq!(cast(Round(src_8)), dst_8); assert_eq!(cast(Round(src_9ish)), dst_9ish); assert_eq!(cast(Round(src_c)), dst_c); assert_eq!(cast(Round(src_fish)), dst_fish); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(cast(Round(src_max)), dst_wmax); } assert_eq!(checked_cast(Round(-src_nan)), None); assert_eq!(checked_cast(Round(-src_inf)), None); assert_eq!(checked_cast(Round(-src_max)), None); assert_eq!(checked_cast(Round(-src_fish)), None); assert_eq!(checked_cast(Round(-src_c)), None); assert_eq!(checked_cast(Round(-src_9ish)), None); assert_eq!(checked_cast(Round(-src_8)), None); assert_eq!(checked_cast(Round(-src_7ish)), None); assert_eq!(checked_cast(Round(-src_6)), None); assert_eq!(checked_cast(Round(-src_1_5)), None); assert_eq!(checked_cast(Round(-src_1)), None); assert_eq!(checked_cast(Round(-src_0)), Some(dst_0)); assert_eq!(checked_cast(Round(src_0)), Some(dst_0)); assert_eq!(checked_cast(Round(src_1)), Some(dst_1)); assert_eq!(checked_cast(Round(src_1_5)), Some(dst_2)); assert_eq!(checked_cast(Round(src_6)), Some(dst_6)); assert_eq!(checked_cast(Round(src_7ish)), Some(dst_7ish)); assert_eq!(checked_cast(Round(src_8)), Some(dst_8)); assert_eq!(checked_cast(Round(src_9ish)), Some(dst_9ish)); assert_eq!(checked_cast(Round(src_c)), Some(dst_c)); assert_eq!(checked_cast(Round(src_fish)), Some(dst_fish)); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(checked_cast(Round(src_max)), Some(dst_wmax)); } else { assert_eq!(checked_cast(Round(src_max)), None); } assert_eq!(checked_cast(Round(src_inf)), None); assert_eq!(checked_cast(Round(src_nan)), None); assert_eq!(saturating_cast(Round(-src_inf)), dst_0); assert_eq!(saturating_cast(Round(-src_max)), dst_0); assert_eq!(saturating_cast(Round(-src_fish)), dst_0); assert_eq!(saturating_cast(Round(-src_c)), dst_0); assert_eq!(saturating_cast(Round(-src_9ish)), dst_0); assert_eq!(saturating_cast(Round(-src_8)), dst_0); assert_eq!(saturating_cast(Round(-src_7ish)), dst_0); assert_eq!(saturating_cast(Round(-src_6)), dst_0); assert_eq!(saturating_cast(Round(-src_1_5)), dst_0); assert_eq!(saturating_cast(Round(-src_1)), dst_0); assert_eq!(saturating_cast(Round(-src_0)), dst_0); assert_eq!(saturating_cast(Round(src_0)), dst_0); assert_eq!(saturating_cast(Round(src_1)), dst_1); assert_eq!(saturating_cast(Round(src_1_5)), dst_2); assert_eq!(saturating_cast(Round(src_6)), dst_6); assert_eq!(saturating_cast(Round(src_7ish)), dst_7ish); assert_eq!(saturating_cast(Round(src_8)), dst_8); assert_eq!(saturating_cast(Round(src_9ish)), dst_9ish); assert_eq!(saturating_cast(Round(src_c)), dst_c); assert_eq!(saturating_cast(Round(src_fish)), dst_fish); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(saturating_cast(Round(src_max)), dst_wmax); } else { assert_eq!(saturating_cast(Round(src_max)), dst_f); } assert_eq!(saturating_cast(Round(src_inf)), dst_f); assert_eq!(wrapping_cast(Round(-src_max)), dst_wmax_neg); assert_eq!(wrapping_cast(Round(-src_fish)), dst_1ish); assert_eq!(wrapping_cast(Round(-src_c)), dst_4); assert_eq!(wrapping_cast(Round(-src_9ish)), dst_7ish); assert_eq!(wrapping_cast(Round(-src_8)), dst_8); assert_eq!(wrapping_cast(Round(-src_7ish)), dst_9ish); assert_eq!(wrapping_cast(Round(-src_6)), dst_a); assert_eq!(wrapping_cast(Round(-src_1_5)), dst_e); assert_eq!(wrapping_cast(Round(-src_1)), dst_f); assert_eq!(wrapping_cast(Round(-src_0)), dst_0); assert_eq!(wrapping_cast(Round(src_0)), dst_0); assert_eq!(wrapping_cast(Round(src_1)), dst_1); assert_eq!(wrapping_cast(Round(src_1_5)), dst_2); assert_eq!(wrapping_cast(Round(src_6)), dst_6); assert_eq!(wrapping_cast(Round(src_7ish)), dst_7ish); assert_eq!(wrapping_cast(Round(src_8)), dst_8); assert_eq!(wrapping_cast(Round(src_9ish)), dst_9ish); assert_eq!(wrapping_cast(Round(src_c)), dst_c); assert_eq!(wrapping_cast(Round(src_fish)), dst_fish); assert_eq!(wrapping_cast(Round(src_max)), dst_wmax); assert_eq!(overflowing_cast(Round(-src_max)), (dst_wmax_neg, true)); assert_eq!(overflowing_cast(Round(-src_fish)), (dst_1ish, true)); assert_eq!(overflowing_cast(Round(-src_c)), (dst_4, true)); assert_eq!(overflowing_cast(Round(-src_9ish)), (dst_7ish, true)); assert_eq!(overflowing_cast(Round(-src_8)), (dst_8, true)); assert_eq!(overflowing_cast(Round(-src_7ish)), (dst_9ish, true)); assert_eq!(overflowing_cast(Round(-src_6)), (dst_a, true)); assert_eq!(overflowing_cast(Round(-src_1_5)), (dst_e, true)); assert_eq!(overflowing_cast(Round(-src_1)), (dst_f, true)); assert_eq!(overflowing_cast(Round(-src_0)), (dst_0, false)); assert_eq!(overflowing_cast(Round(src_0)), (dst_0, false)); assert_eq!(overflowing_cast(Round(src_1)), (dst_1, false)); assert_eq!(overflowing_cast(Round(src_1_5)), (dst_2, false)); assert_eq!(overflowing_cast(Round(src_6)), (dst_6, false)); assert_eq!(overflowing_cast(Round(src_7ish)), (dst_7ish, false)); assert_eq!(overflowing_cast(Round(src_8)), (dst_8, false)); assert_eq!(overflowing_cast(Round(src_9ish)), (dst_9ish, false)); assert_eq!(overflowing_cast(Round(src_c)), (dst_c, false)); assert_eq!(overflowing_cast(Round(src_fish)), (dst_fish, false)); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(overflowing_cast(Round(src_max)), (dst_wmax, false)); } else { assert_eq!(overflowing_cast(Round(src_max)), (dst_wmax, true)); } assert_eq!(unwrapped_cast(Round(-src_0)), dst_0); assert_eq!(unwrapped_cast(Round(src_0)), dst_0); assert_eq!(unwrapped_cast(Round(src_1)), dst_1); assert_eq!(unwrapped_cast(Round(src_1_5)), dst_2); assert_eq!(unwrapped_cast(Round(src_6)), dst_6); assert_eq!(unwrapped_cast(Round(src_7ish)), dst_7ish); assert_eq!(unwrapped_cast(Round(src_8)), dst_8); assert_eq!(unwrapped_cast(Round(src_9ish)), dst_9ish); assert_eq!(unwrapped_cast(Round(src_c)), dst_c); assert_eq!(unwrapped_cast(Round(src_fish)), dst_fish); if src_nbits == 32 && dst_nbits == 128 { assert_eq!(unwrapped_cast(Round(src_max)), dst_wmax); } } fn round_to_wrapping_unsigned() where Round: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_prec = Src::prec(); let dst_nbits = mem::size_of::() * 8; let src_0 = Src::int_shl(0, 0); let src_1 = Src::int_shl(1, 0); let src_1_5 = Src::int_shl(3, -1); let src_8 = Src::int_shl(1, (dst_nbits - 1) as i8); let src_1ish = if src_prec > dst_nbits { src_1 } else { Src::int_shl(1, (dst_nbits - src_prec) as i8) }; let src_7ish = src_8 - src_1ish; let src_6 = Src::int_shl(6, (dst_nbits - 4) as i8); let src_9ish = src_8 + src_1ish; let src_c = Src::int_shl(12, (dst_nbits - 4) as i8); let src_fish = src_8 + src_7ish; let src_max = Src::max(); let src_inf = Src::inf(); let src_nan = Src::nan(); let dst_0 = Wrapping(wrapping_cast(Round(src_0))); let dst_1 = Wrapping(wrapping_cast(Round(src_1))); let dst_2 = Wrapping(wrapping_cast(Round(src_1_5))); let dst_1ish = Wrapping(wrapping_cast(Round(-src_fish))); let dst_fish = Wrapping(wrapping_cast(Round(src_fish))); let dst_4 = Wrapping(wrapping_cast(Round(-src_c))); let dst_6 = Wrapping(wrapping_cast(Round(src_6))); let dst_7ish = Wrapping(wrapping_cast(Round(src_7ish))); let dst_8 = Wrapping(wrapping_cast(Round(src_8))); let dst_9ish = Wrapping(wrapping_cast(Round(src_9ish))); let dst_a = Wrapping(wrapping_cast(Round(-src_6))); let dst_c = Wrapping(wrapping_cast(Round(src_c))); let dst_e = Wrapping(wrapping_cast(Round(-src_1_5))); let dst_f = Wrapping(wrapping_cast(Round(-src_1))); let dst_wmax = Wrapping(wrapping_cast(Round(src_max))); let dst_wmax_neg = Wrapping(wrapping_cast(Round(-src_max))); assert_eq!(cast(Round(-src_max)), dst_wmax_neg); assert_eq!(cast(Round(-src_fish)), dst_1ish); assert_eq!(cast(Round(-src_c)), dst_4); assert_eq!(cast(Round(-src_9ish)), dst_7ish); assert_eq!(cast(Round(-src_8)), dst_8); assert_eq!(cast(Round(-src_7ish)), dst_9ish); assert_eq!(cast(Round(-src_6)), dst_a); assert_eq!(cast(Round(-src_1_5)), dst_e); assert_eq!(cast(Round(-src_1)), dst_f); assert_eq!(cast(Round(-src_0)), dst_0); assert_eq!(cast(Round(src_0)), dst_0); assert_eq!(cast(Round(src_1)), dst_1); assert_eq!(cast(Round(src_1_5)), dst_2); assert_eq!(cast(Round(src_6)), dst_6); assert_eq!(cast(Round(src_7ish)), dst_7ish); assert_eq!(cast(Round(src_8)), dst_8); assert_eq!(cast(Round(src_9ish)), dst_9ish); assert_eq!(cast(Round(src_c)), dst_c); assert_eq!(cast(Round(src_fish)), dst_fish); assert_eq!(cast(Round(src_max)), dst_wmax); assert_eq!(checked_cast(Round(-src_nan)), None); assert_eq!(checked_cast(Round(-src_inf)), None); assert_eq!(checked_cast(Round(-src_max)), Some(dst_wmax_neg)); assert_eq!(checked_cast(Round(-src_fish)), Some(dst_1ish)); assert_eq!(checked_cast(Round(-src_c)), Some(dst_4)); assert_eq!(checked_cast(Round(-src_9ish)), Some(dst_7ish)); assert_eq!(checked_cast(Round(-src_8)), Some(dst_8)); assert_eq!(checked_cast(Round(-src_7ish)), Some(dst_9ish)); assert_eq!(checked_cast(Round(-src_6)), Some(dst_a)); assert_eq!(checked_cast(Round(-src_1_5)), Some(dst_e)); assert_eq!(checked_cast(Round(-src_1)), Some(dst_f)); assert_eq!(checked_cast(Round(-src_0)), Some(dst_0)); assert_eq!(checked_cast(Round(src_0)), Some(dst_0)); assert_eq!(checked_cast(Round(src_1)), Some(dst_1)); assert_eq!(checked_cast(Round(src_1_5)), Some(dst_2)); assert_eq!(checked_cast(Round(src_6)), Some(dst_6)); assert_eq!(checked_cast(Round(src_7ish)), Some(dst_7ish)); assert_eq!(checked_cast(Round(src_8)), Some(dst_8)); assert_eq!(checked_cast(Round(src_9ish)), Some(dst_9ish)); assert_eq!(checked_cast(Round(src_c)), Some(dst_c)); assert_eq!(checked_cast(Round(src_fish)), Some(dst_fish)); assert_eq!(checked_cast(Round(src_max)), Some(dst_wmax)); assert_eq!(checked_cast(Round(src_inf)), None); assert_eq!(checked_cast(Round(src_nan)), None); assert_eq!(unwrapped_cast(Round(-src_max)), dst_wmax_neg); assert_eq!(unwrapped_cast(Round(-src_fish)), dst_1ish); assert_eq!(unwrapped_cast(Round(-src_c)), dst_4); assert_eq!(unwrapped_cast(Round(-src_9ish)), dst_7ish); assert_eq!(unwrapped_cast(Round(-src_8)), dst_8); assert_eq!(unwrapped_cast(Round(-src_7ish)), dst_9ish); assert_eq!(unwrapped_cast(Round(-src_6)), dst_a); assert_eq!(unwrapped_cast(Round(-src_1_5)), dst_e); assert_eq!(unwrapped_cast(Round(-src_1)), dst_f); assert_eq!(unwrapped_cast(Round(-src_0)), dst_0); assert_eq!(unwrapped_cast(Round(src_0)), dst_0); assert_eq!(unwrapped_cast(Round(src_1)), dst_1); assert_eq!(unwrapped_cast(Round(src_1_5)), dst_2); assert_eq!(unwrapped_cast(Round(src_6)), dst_6); assert_eq!(unwrapped_cast(Round(src_7ish)), dst_7ish); assert_eq!(unwrapped_cast(Round(src_8)), dst_8); assert_eq!(unwrapped_cast(Round(src_9ish)), dst_9ish); assert_eq!(unwrapped_cast(Round(src_c)), dst_c); assert_eq!(unwrapped_cast(Round(src_fish)), dst_fish); assert_eq!(unwrapped_cast(Round(src_max)), dst_wmax); } fn float_to_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Round: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { float_to_nonwrapping_signed::(); float_to_wrapping_signed::(); round_to_nonwrapping_signed::(); round_to_wrapping_signed::(); } fn float_to_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Round: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { float_to_nonwrapping_unsigned::(); float_to_wrapping_unsigned::(); round_to_nonwrapping_unsigned::(); round_to_wrapping_unsigned::(); } #[test] fn float_to_int() { float_to_signed::(); float_to_signed::(); float_to_signed::(); float_to_signed::(); float_to_signed::(); float_to_signed::(); float_to_unsigned::(); float_to_unsigned::(); float_to_unsigned::(); float_to_unsigned::(); float_to_unsigned::(); float_to_unsigned::(); float_to_signed::(); float_to_signed::(); float_to_signed::(); float_to_signed::(); float_to_signed::(); float_to_signed::(); float_to_unsigned::(); float_to_unsigned::(); float_to_unsigned::(); float_to_unsigned::(); float_to_unsigned::(); float_to_unsigned::(); } #[test] fn rounding() { assert_eq!(cast::, i8>(Round(-0.5 + f64::EPSILON / 4.0)), 0); assert_eq!(cast::, i8>(Round(-0.5)), 0); assert_eq!(cast::, i8>(Round(-0.5 - f64::EPSILON / 2.0)), -1); assert_eq!(cast::, i8>(Round(-1.0 + f64::EPSILON / 2.0)), -1); assert_eq!(cast::, i8>(Round(-1.0)), -1); assert_eq!(cast::, i8>(Round(-1.0 - f64::EPSILON)), -1); assert_eq!(cast::, i8>(Round(-1.5 + f64::EPSILON)), -1); assert_eq!(cast::, i8>(Round(-1.5)), -2); assert_eq!(cast::, i8>(Round(-1.5 - f64::EPSILON)), -2); assert_eq!(cast::, i8>(Round(2.0 - f64::EPSILON)), 2); assert_eq!(cast::, i8>(Round(2.0)), 2); assert_eq!(cast::, i8>(Round(2.0 + f64::EPSILON * 2.0)), 2); assert_eq!(cast::, i8>(Round(2.5 - f64::EPSILON * 2.0)), 2); assert_eq!(cast::, i8>(Round(2.5)), 2); assert_eq!(cast::, i8>(Round(2.5 + f64::EPSILON * 2.0)), 3); assert_eq!(cast::, i8>(Round(3.5 - f64::EPSILON * 2.0)), 3); assert_eq!(cast::, i8>(Round(3.5)), 4); assert_eq!(cast::, i8>(Round(3.5 + f64::EPSILON * 2.0)), 4); assert_eq!(cast::, i128>(Round(0.5 - f64::EPSILON / 4.0)), 0); assert_eq!(cast::, i128>(Round(0.5)), 0); assert_eq!(cast::, i128>(Round(0.5 + f64::EPSILON / 2.0)), 1); assert_eq!(cast::, i128>(Round(1.0 - f64::EPSILON / 2.0)), 1); assert_eq!(cast::, i128>(Round(1.0)), 1); assert_eq!(cast::, i128>(Round(1.0 + f64::EPSILON)), 1); assert_eq!(cast::, i128>(Round(1.5 - f64::EPSILON)), 1); assert_eq!(cast::, i128>(Round(1.5)), 2); assert_eq!(cast::, i128>(Round(1.5 + f64::EPSILON)), 2); assert_eq!(cast::, i128>(Round(-2.0 + f64::EPSILON)), -2); assert_eq!(cast::, i128>(Round(-2.0)), -2); assert_eq!( cast::, i128>(Round(-2.0 - f64::EPSILON * 2.0)), -2 ); assert_eq!( cast::, i128>(Round(-2.5 + f64::EPSILON * 2.0)), -2 ); assert_eq!(cast::, i128>(Round(-2.5)), -2); assert_eq!( cast::, i128>(Round(-2.5 - f64::EPSILON * 2.0)), -3 ); assert_eq!( cast::, i128>(Round(-3.5 + f64::EPSILON * 2.0)), -3 ); assert_eq!(cast::, i128>(Round(-3.5)), -4); assert_eq!( cast::, i128>(Round(-3.5 - f64::EPSILON * 2.0)), -4 ); } #[test] #[should_panic(expected = "NaN")] fn nan_saturating_as_panic() { let _ = saturating_cast::(f32::NAN); } #[test] #[should_panic(expected = "NaN")] fn nan_overflowing_as_panic() { let _ = overflowing_cast::(f32::NAN); } #[test] #[should_panic(expected = "NaN")] fn nan_unwrapped_as_panic() { let _ = unwrapped_cast::(f32::NAN); } #[test] #[should_panic(expected = "infinite")] fn infinite_overflowing_as_panic() { let _ = overflowing_cast::(f32::INFINITY); } #[test] #[should_panic(expected = "infinite")] fn infinite_unwrapped_as_panic() { let _ = unwrapped_cast::(f32::INFINITY); } #[cfg(debug_assertions)] #[test] #[should_panic(expected = "overflow")] fn large_float_as_panic() { let _ = cast::(128.0); } #[cfg(not(debug_assertions))] #[test] fn large_float_as_wrap() { assert_eq!(cast::(128.0), -128); assert_eq!(cast::(-127.0), 129); } #[test] #[should_panic(expected = "overflow")] fn large_float_unwrapped_as_panic() { let _ = unwrapped_cast::(128.0); } #[test] fn display() { use std::format; let f = 1e-50 / 3.0; let r = Round(f); assert_eq!(format!("{}", f), format!("{}", r)); assert_eq!(format!("{:?}", f), format!("{:?}", r)); assert_eq!(format!("{:e}", f), format!("{:e}", r)); assert_eq!(format!("{:E}", f), format!("{:E}", r)); } az-1.2.1/src/tests/int_to_int.rs000064400000000000000000001605210072674642500147410ustar 00000000000000// Copyright © 2019–2021 Trevor Spiteri // This library is free software: you can redistribute it and/or // modify it under the terms of either // // * the Apache License, Version 2.0 or // * the MIT License // // at your option. // // You should have recieved copies of the Apache License and the MIT // License along with the library. If not, see // and // . use crate::{ cast, checked_cast, overflowing_cast, saturating_cast, tests::Int, unwrapped_cast, wrapping_cast, Cast, CheckedCast, OverflowingCast, SaturatingCast, UnwrappedCast, WrappingCast, }; use core::{mem, num::Wrapping}; fn bool_to_nonwrapping_int() where bool: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let zero = Dst::zero(); let one = Dst::one(); assert_eq!(cast(false), zero); assert_eq!(cast(true), one); assert_eq!(checked_cast(false), Some(zero)); assert_eq!(checked_cast(true), Some(one)); assert_eq!(saturating_cast(false), zero); assert_eq!(saturating_cast(true), one); assert_eq!(wrapping_cast(false), zero); assert_eq!(wrapping_cast(true), one); assert_eq!(overflowing_cast(false), (zero, false)); assert_eq!(overflowing_cast(true), (one, false)); assert_eq!(unwrapped_cast(false), zero); assert_eq!(unwrapped_cast(true), one); } fn bool_to_wrapping_int() where bool: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let zero = Wrapping(wrapping_cast(false)); let one = Wrapping(wrapping_cast(true)); assert_eq!(cast(false), zero); assert_eq!(cast(true), one); assert_eq!(checked_cast(false), Some(zero)); assert_eq!(checked_cast(true), Some(one)); assert_eq!(unwrapped_cast(false), zero); assert_eq!(unwrapped_cast(true), one); } fn bool_to_single_int() where bool: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { bool_to_nonwrapping_int::(); bool_to_wrapping_int::(); } #[test] fn bool_to_int() { bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); bool_to_single_int::(); } fn signed_to_smaller_nonwrapping_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits > dst_nbits); let src_z00 = Src::zero(); let src_m01 = !src_z00; let src_m08 = src_m01 << (dst_nbits - 1); let src_m09 = src_m08 + src_m01; let src_m10 = src_m01 << dst_nbits; let src_p01 = Src::one(); let src_p07 = !src_m08; let src_p08 = src_p07 + src_p01; let src_p0f = src_p07 + src_p08; let src_p10 = src_p01 << dst_nbits; let dst_z0 = Dst::zero(); let dst_m1 = !dst_z0; let dst_m8 = dst_m1 << (dst_nbits - 1); let dst_p1 = Dst::one(); let dst_p7 = !dst_m8; assert_eq!(cast(src_m08), dst_m8); assert_eq!(cast(src_m01), dst_m1); assert_eq!(cast(src_z00), dst_z0); assert_eq!(cast(src_p01), dst_p1); assert_eq!(cast(src_p07), dst_p7); assert_eq!(checked_cast(src_m10), None); assert_eq!(checked_cast(src_m09), None); assert_eq!(checked_cast(src_m08), Some(dst_m8)); assert_eq!(checked_cast(src_m01), Some(dst_m1)); assert_eq!(checked_cast(src_z00), Some(dst_z0)); assert_eq!(checked_cast(src_p01), Some(dst_p1)); assert_eq!(checked_cast(src_p07), Some(dst_p7)); assert_eq!(checked_cast(src_p08), None); assert_eq!(checked_cast(src_p0f), None); assert_eq!(checked_cast(src_p10), None); assert_eq!(saturating_cast(src_m10), dst_m8); assert_eq!(saturating_cast(src_m09), dst_m8); assert_eq!(saturating_cast(src_m08), dst_m8); assert_eq!(saturating_cast(src_m01), dst_m1); assert_eq!(saturating_cast(src_z00), dst_z0); assert_eq!(saturating_cast(src_p01), dst_p1); assert_eq!(saturating_cast(src_p07), dst_p7); assert_eq!(saturating_cast(src_p08), dst_p7); assert_eq!(saturating_cast(src_p0f), dst_p7); assert_eq!(saturating_cast(src_p10), dst_p7); assert_eq!(wrapping_cast(src_m10), dst_z0); assert_eq!(wrapping_cast(src_m09), dst_p7); assert_eq!(wrapping_cast(src_m08), dst_m8); assert_eq!(wrapping_cast(src_m01), dst_m1); assert_eq!(wrapping_cast(src_z00), dst_z0); assert_eq!(wrapping_cast(src_p01), dst_p1); assert_eq!(wrapping_cast(src_p07), dst_p7); assert_eq!(wrapping_cast(src_p08), dst_m8); assert_eq!(wrapping_cast(src_p0f), dst_m1); assert_eq!(wrapping_cast(src_p10), dst_z0); assert_eq!(overflowing_cast(src_m10), (dst_z0, true)); assert_eq!(overflowing_cast(src_m09), (dst_p7, true)); assert_eq!(overflowing_cast(src_m08), (dst_m8, false)); assert_eq!(overflowing_cast(src_m01), (dst_m1, false)); assert_eq!(overflowing_cast(src_z00), (dst_z0, false)); assert_eq!(overflowing_cast(src_p01), (dst_p1, false)); assert_eq!(overflowing_cast(src_p07), (dst_p7, false)); assert_eq!(overflowing_cast(src_p08), (dst_m8, true)); assert_eq!(overflowing_cast(src_p0f), (dst_m1, true)); assert_eq!(overflowing_cast(src_p10), (dst_z0, true)); assert_eq!(unwrapped_cast(src_m08), dst_m8); assert_eq!(unwrapped_cast(src_m01), dst_m1); assert_eq!(unwrapped_cast(src_z00), dst_z0); assert_eq!(unwrapped_cast(src_p01), dst_p1); assert_eq!(unwrapped_cast(src_p07), dst_p7); } fn signed_to_larger_same_nonwrapping_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits <= dst_nbits); let src_z0 = Src::zero(); let src_m1 = !src_z0; let src_m8 = src_m1 << (src_nbits - 1); let src_p1 = Src::one(); let src_p7 = !src_m8; let dst_z00 = Dst::zero(); let dst_m01 = !dst_z00; let dst_m08 = dst_m01 << (src_nbits - 1); let dst_p01 = Dst::one(); let dst_p07 = !dst_m08; assert_eq!(cast(src_m8), dst_m08); assert_eq!(cast(src_m1), dst_m01); assert_eq!(cast(src_z0), dst_z00); assert_eq!(cast(src_p1), dst_p01); assert_eq!(cast(src_p7), dst_p07); assert_eq!(checked_cast(src_m8), Some(dst_m08)); assert_eq!(checked_cast(src_m1), Some(dst_m01)); assert_eq!(checked_cast(src_z0), Some(dst_z00)); assert_eq!(checked_cast(src_p1), Some(dst_p01)); assert_eq!(checked_cast(src_p7), Some(dst_p07)); assert_eq!(saturating_cast(src_m8), dst_m08); assert_eq!(saturating_cast(src_m1), dst_m01); assert_eq!(saturating_cast(src_z0), dst_z00); assert_eq!(saturating_cast(src_p1), dst_p01); assert_eq!(saturating_cast(src_p7), dst_p07); assert_eq!(wrapping_cast(src_m8), dst_m08); assert_eq!(wrapping_cast(src_m1), dst_m01); assert_eq!(wrapping_cast(src_z0), dst_z00); assert_eq!(wrapping_cast(src_p1), dst_p01); assert_eq!(wrapping_cast(src_p7), dst_p07); assert_eq!(overflowing_cast(src_m8), (dst_m08, false)); assert_eq!(overflowing_cast(src_m1), (dst_m01, false)); assert_eq!(overflowing_cast(src_z0), (dst_z00, false)); assert_eq!(overflowing_cast(src_p1), (dst_p01, false)); assert_eq!(overflowing_cast(src_p7), (dst_p07, false)); assert_eq!(unwrapped_cast(src_m8), dst_m08); assert_eq!(unwrapped_cast(src_m1), dst_m01); assert_eq!(unwrapped_cast(src_z0), dst_z00); assert_eq!(unwrapped_cast(src_p1), dst_p01); assert_eq!(unwrapped_cast(src_p7), dst_p07); } fn signed_to_smaller_wrapping_signed() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits > dst_nbits); let src_z00 = Src::zero(); let src_m01 = !src_z00; let src_m08 = src_m01 << (dst_nbits - 1); let src_m09 = src_m08 + src_m01; let src_m10 = src_m01 << dst_nbits; let src_p01 = Src::one(); let src_p07 = !src_m08; let src_p08 = src_p07 + src_p01; let src_p0f = src_p07 + src_p08; let src_p10 = src_p01 << dst_nbits; let dst_z0 = Wrapping(wrapping_cast(src_z00)); let dst_m1 = Wrapping(wrapping_cast(src_m01)); let dst_m8 = Wrapping(wrapping_cast(src_m08)); let dst_p1 = Wrapping(wrapping_cast(src_p01)); let dst_p7 = Wrapping(wrapping_cast(src_p07)); assert_eq!(cast(src_m10), dst_z0); assert_eq!(cast(src_m09), dst_p7); assert_eq!(cast(src_m08), dst_m8); assert_eq!(cast(src_m01), dst_m1); assert_eq!(cast(src_z00), dst_z0); assert_eq!(cast(src_p01), dst_p1); assert_eq!(cast(src_p07), dst_p7); assert_eq!(cast(src_p08), dst_m8); assert_eq!(cast(src_p0f), dst_m1); assert_eq!(cast(src_p10), dst_z0); assert_eq!(checked_cast(src_m10), Some(dst_z0)); assert_eq!(checked_cast(src_m09), Some(dst_p7)); assert_eq!(checked_cast(src_m08), Some(dst_m8)); assert_eq!(checked_cast(src_m01), Some(dst_m1)); assert_eq!(checked_cast(src_z00), Some(dst_z0)); assert_eq!(checked_cast(src_p01), Some(dst_p1)); assert_eq!(checked_cast(src_p07), Some(dst_p7)); assert_eq!(checked_cast(src_p08), Some(dst_m8)); assert_eq!(checked_cast(src_p0f), Some(dst_m1)); assert_eq!(checked_cast(src_p10), Some(dst_z0)); assert_eq!(unwrapped_cast(src_m10), dst_z0); assert_eq!(unwrapped_cast(src_m09), dst_p7); assert_eq!(unwrapped_cast(src_m08), dst_m8); assert_eq!(unwrapped_cast(src_m01), dst_m1); assert_eq!(unwrapped_cast(src_z00), dst_z0); assert_eq!(unwrapped_cast(src_p01), dst_p1); assert_eq!(unwrapped_cast(src_p07), dst_p7); assert_eq!(unwrapped_cast(src_p08), dst_m8); assert_eq!(unwrapped_cast(src_p0f), dst_m1); assert_eq!(unwrapped_cast(src_p10), dst_z0); } fn signed_to_larger_same_wrapping_signed() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits <= dst_nbits); let src_z0 = Src::zero(); let src_m1 = !src_z0; let src_m8 = src_m1 << (src_nbits - 1); let src_p1 = Src::one(); let src_p7 = !src_m8; let dst_z00 = Wrapping(wrapping_cast(src_z0)); let dst_m01 = Wrapping(wrapping_cast(src_m1)); let dst_m08 = Wrapping(wrapping_cast(src_m8)); let dst_p01 = Wrapping(wrapping_cast(src_p1)); let dst_p07 = Wrapping(wrapping_cast(src_p7)); assert_eq!(cast(src_m8), dst_m08); assert_eq!(cast(src_m1), dst_m01); assert_eq!(cast(src_z0), dst_z00); assert_eq!(cast(src_p1), dst_p01); assert_eq!(cast(src_p7), dst_p07); assert_eq!(checked_cast(src_m8), Some(dst_m08)); assert_eq!(checked_cast(src_m1), Some(dst_m01)); assert_eq!(checked_cast(src_z0), Some(dst_z00)); assert_eq!(checked_cast(src_p1), Some(dst_p01)); assert_eq!(checked_cast(src_p7), Some(dst_p07)); assert_eq!(unwrapped_cast(src_m8), dst_m08); assert_eq!(unwrapped_cast(src_m1), dst_m01); assert_eq!(unwrapped_cast(src_z0), dst_z00); assert_eq!(unwrapped_cast(src_p1), dst_p01); assert_eq!(unwrapped_cast(src_p7), dst_p07); } fn signed_to_smaller_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { signed_to_smaller_nonwrapping_signed::(); signed_to_smaller_wrapping_signed::(); } fn signed_to_larger_same_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { signed_to_larger_same_nonwrapping_signed::(); signed_to_larger_same_wrapping_signed::(); } #[test] fn signed_to_signed() { signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_smaller_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_smaller_signed::(); signed_to_smaller_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); if cfg!(target_pointer_width = "16") { signed_to_smaller_signed::(); } else { signed_to_larger_same_signed::(); } signed_to_smaller_signed::(); signed_to_smaller_signed::(); signed_to_smaller_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); if cfg!(target_pointer_width = "16") || cfg!(target_pointer_width = "32") { signed_to_smaller_signed::(); } else { signed_to_larger_same_signed::(); } signed_to_smaller_signed::(); signed_to_smaller_signed::(); signed_to_smaller_signed::(); signed_to_smaller_signed::(); signed_to_larger_same_signed::(); if cfg!(target_pointer_width = "16") || cfg!(target_pointer_width = "32") || cfg!(target_pointer_width = "64") { signed_to_smaller_signed::(); } else { signed_to_larger_same_signed::(); } signed_to_smaller_signed::(); if cfg!(target_pointer_width = "16") { signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); } else if cfg!(target_pointer_width = "32") { signed_to_smaller_signed::(); signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); } else if cfg!(target_pointer_width = "64") { signed_to_smaller_signed::(); signed_to_smaller_signed::(); signed_to_larger_same_signed::(); } else if cfg!(target_pointer_width = "128") { signed_to_smaller_signed::(); signed_to_smaller_signed::(); signed_to_smaller_signed::(); } signed_to_larger_same_signed::(); signed_to_larger_same_signed::(); } fn signed_to_smaller_nonwrapping_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits > dst_nbits); let src_z00 = Src::zero(); let src_m01 = !src_z00; let src_m08 = src_m01 << (dst_nbits - 1); let src_m09 = src_m08 + src_m01; let src_m10 = src_m01 << dst_nbits; let src_p01 = Src::one(); let src_p07 = !src_m08; let src_p08 = src_p07 + src_p01; let src_p0f = src_p07 + src_p08; let src_p10 = src_p01 << dst_nbits; let dst_z0 = Dst::zero(); let dst_p1 = Dst::one(); let dst_p7 = !(!dst_z0 << (dst_nbits - 1)); let dst_p8 = dst_p7 + dst_p1; let dst_pf = dst_p7 + dst_p8; assert_eq!(cast(src_z00), dst_z0); assert_eq!(cast(src_p01), dst_p1); assert_eq!(cast(src_p07), dst_p7); assert_eq!(cast(src_p08), dst_p8); assert_eq!(cast(src_p0f), dst_pf); assert_eq!(checked_cast(src_m10), None); assert_eq!(checked_cast(src_m09), None); assert_eq!(checked_cast(src_m08), None); assert_eq!(checked_cast(src_m01), None); assert_eq!(checked_cast(src_z00), Some(dst_z0)); assert_eq!(checked_cast(src_p01), Some(dst_p1)); assert_eq!(checked_cast(src_p07), Some(dst_p7)); assert_eq!(checked_cast(src_p08), Some(dst_p8)); assert_eq!(checked_cast(src_p0f), Some(dst_pf)); assert_eq!(checked_cast(src_p10), None); assert_eq!(saturating_cast(src_m10), dst_z0); assert_eq!(saturating_cast(src_m09), dst_z0); assert_eq!(saturating_cast(src_m08), dst_z0); assert_eq!(saturating_cast(src_m01), dst_z0); assert_eq!(saturating_cast(src_z00), dst_z0); assert_eq!(saturating_cast(src_p01), dst_p1); assert_eq!(saturating_cast(src_p07), dst_p7); assert_eq!(saturating_cast(src_p08), dst_p8); assert_eq!(saturating_cast(src_p0f), dst_pf); assert_eq!(saturating_cast(src_p10), dst_pf); assert_eq!(wrapping_cast(src_m10), dst_z0); assert_eq!(wrapping_cast(src_m09), dst_p7); assert_eq!(wrapping_cast(src_m08), dst_p8); assert_eq!(wrapping_cast(src_m01), dst_pf); assert_eq!(wrapping_cast(src_z00), dst_z0); assert_eq!(wrapping_cast(src_p01), dst_p1); assert_eq!(wrapping_cast(src_p07), dst_p7); assert_eq!(wrapping_cast(src_p08), dst_p8); assert_eq!(wrapping_cast(src_p0f), dst_pf); assert_eq!(wrapping_cast(src_p10), dst_z0); assert_eq!(overflowing_cast(src_m10), (dst_z0, true)); assert_eq!(overflowing_cast(src_m09), (dst_p7, true)); assert_eq!(overflowing_cast(src_m08), (dst_p8, true)); assert_eq!(overflowing_cast(src_m01), (dst_pf, true)); assert_eq!(overflowing_cast(src_z00), (dst_z0, false)); assert_eq!(overflowing_cast(src_p01), (dst_p1, false)); assert_eq!(overflowing_cast(src_p07), (dst_p7, false)); assert_eq!(overflowing_cast(src_p08), (dst_p8, false)); assert_eq!(overflowing_cast(src_p0f), (dst_pf, false)); assert_eq!(overflowing_cast(src_p10), (dst_z0, true)); assert_eq!(unwrapped_cast(src_z00), dst_z0); assert_eq!(unwrapped_cast(src_p01), dst_p1); assert_eq!(unwrapped_cast(src_p07), dst_p7); assert_eq!(unwrapped_cast(src_p08), dst_p8); assert_eq!(unwrapped_cast(src_p0f), dst_pf); } fn signed_to_larger_same_nonwrapping_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits <= dst_nbits); let src_z0 = Src::zero(); let src_m1 = !src_z0; let src_m8 = src_m1 << (src_nbits - 1); let src_p1 = Src::one(); let src_p7 = !src_m8; let dst_z00 = Dst::zero(); let dst_p01 = Dst::one(); let dst_p07 = !(!dst_z00 << (src_nbits - 1)); let dst_pf8 = !dst_p07; let dst_pff = !dst_z00; assert_eq!(cast(src_z0), dst_z00); assert_eq!(cast(src_p1), dst_p01); assert_eq!(cast(src_p7), dst_p07); assert_eq!(checked_cast(src_m8), None); assert_eq!(checked_cast(src_m1), None); assert_eq!(checked_cast(src_z0), Some(dst_z00)); assert_eq!(checked_cast(src_p1), Some(dst_p01)); assert_eq!(checked_cast(src_p7), Some(dst_p07)); assert_eq!(saturating_cast(src_m8), dst_z00); assert_eq!(saturating_cast(src_m1), dst_z00); assert_eq!(saturating_cast(src_z0), dst_z00); assert_eq!(saturating_cast(src_p1), dst_p01); assert_eq!(saturating_cast(src_p7), dst_p07); assert_eq!(wrapping_cast(src_m8), dst_pf8); assert_eq!(wrapping_cast(src_m1), dst_pff); assert_eq!(wrapping_cast(src_z0), dst_z00); assert_eq!(wrapping_cast(src_p1), dst_p01); assert_eq!(wrapping_cast(src_p7), dst_p07); assert_eq!(overflowing_cast(src_m8), (dst_pf8, true)); assert_eq!(overflowing_cast(src_m1), (dst_pff, true)); assert_eq!(overflowing_cast(src_z0), (dst_z00, false)); assert_eq!(overflowing_cast(src_p1), (dst_p01, false)); assert_eq!(overflowing_cast(src_p7), (dst_p07, false)); assert_eq!(unwrapped_cast(src_z0), dst_z00); assert_eq!(unwrapped_cast(src_p1), dst_p01); assert_eq!(unwrapped_cast(src_p7), dst_p07); } fn signed_to_smaller_wrapping_unsigned() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits > dst_nbits); let src_z00 = Src::zero(); let src_m01 = !src_z00; let src_m08 = src_m01 << (dst_nbits - 1); let src_m09 = src_m08 + src_m01; let src_m10 = src_m01 << dst_nbits; let src_p01 = Src::one(); let src_p07 = !src_m08; let src_p08 = src_p07 + src_p01; let src_p0f = src_p07 + src_p08; let src_p10 = src_p01 << dst_nbits; let dst_z0 = Wrapping(wrapping_cast(src_z00)); let dst_p1 = Wrapping(wrapping_cast(src_p01)); let dst_p7 = Wrapping(wrapping_cast(src_p07)); let dst_p8 = Wrapping(wrapping_cast(src_p08)); let dst_pf = Wrapping(wrapping_cast(src_p0f)); assert_eq!(cast(src_m10), dst_z0); assert_eq!(cast(src_m09), dst_p7); assert_eq!(cast(src_m08), dst_p8); assert_eq!(cast(src_m01), dst_pf); assert_eq!(cast(src_z00), dst_z0); assert_eq!(cast(src_p01), dst_p1); assert_eq!(cast(src_p07), dst_p7); assert_eq!(cast(src_p08), dst_p8); assert_eq!(cast(src_p0f), dst_pf); assert_eq!(cast(src_p10), dst_z0); assert_eq!(checked_cast(src_m10), Some(dst_z0)); assert_eq!(checked_cast(src_m09), Some(dst_p7)); assert_eq!(checked_cast(src_m08), Some(dst_p8)); assert_eq!(checked_cast(src_m01), Some(dst_pf)); assert_eq!(checked_cast(src_z00), Some(dst_z0)); assert_eq!(checked_cast(src_p01), Some(dst_p1)); assert_eq!(checked_cast(src_p07), Some(dst_p7)); assert_eq!(checked_cast(src_p08), Some(dst_p8)); assert_eq!(checked_cast(src_p0f), Some(dst_pf)); assert_eq!(checked_cast(src_p10), Some(dst_z0)); assert_eq!(unwrapped_cast(src_m10), dst_z0); assert_eq!(unwrapped_cast(src_m09), dst_p7); assert_eq!(unwrapped_cast(src_m08), dst_p8); assert_eq!(unwrapped_cast(src_m01), dst_pf); assert_eq!(unwrapped_cast(src_z00), dst_z0); assert_eq!(unwrapped_cast(src_p01), dst_p1); assert_eq!(unwrapped_cast(src_p07), dst_p7); assert_eq!(unwrapped_cast(src_p08), dst_p8); assert_eq!(unwrapped_cast(src_p0f), dst_pf); assert_eq!(unwrapped_cast(src_p10), dst_z0); } fn signed_to_larger_same_wrapping_unsigned() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits <= dst_nbits); let src_z0 = Src::zero(); let src_m1 = !src_z0; let src_m8 = src_m1 << (src_nbits - 1); let src_p1 = Src::one(); let src_p7 = !src_m8; let dst_z00 = Wrapping(wrapping_cast(src_z0)); let dst_p01 = Wrapping(wrapping_cast(src_p1)); let dst_p07 = Wrapping(wrapping_cast(src_p7)); let dst_pf8 = Wrapping(wrapping_cast(src_m8)); let dst_pff = Wrapping(wrapping_cast(src_m1)); assert_eq!(cast(src_m8), dst_pf8); assert_eq!(cast(src_m1), dst_pff); assert_eq!(cast(src_z0), dst_z00); assert_eq!(cast(src_p1), dst_p01); assert_eq!(cast(src_p7), dst_p07); assert_eq!(checked_cast(src_m8), Some(dst_pf8)); assert_eq!(checked_cast(src_m1), Some(dst_pff)); assert_eq!(checked_cast(src_z0), Some(dst_z00)); assert_eq!(checked_cast(src_p1), Some(dst_p01)); assert_eq!(checked_cast(src_p7), Some(dst_p07)); assert_eq!(unwrapped_cast(src_m8), dst_pf8); assert_eq!(unwrapped_cast(src_m1), dst_pff); assert_eq!(unwrapped_cast(src_z0), dst_z00); assert_eq!(unwrapped_cast(src_p1), dst_p01); assert_eq!(unwrapped_cast(src_p7), dst_p07); } fn signed_to_smaller_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { signed_to_smaller_nonwrapping_unsigned::(); signed_to_smaller_wrapping_unsigned::(); } fn signed_to_larger_same_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { signed_to_larger_same_nonwrapping_unsigned::(); signed_to_larger_same_wrapping_unsigned::(); } #[test] fn signed_to_unsigned() { signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_smaller_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); if cfg!(target_pointer_width = "16") { signed_to_smaller_unsigned::(); } else { signed_to_larger_same_unsigned::(); } signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); if cfg!(target_pointer_width = "16") || cfg!(target_pointer_width = "32") { signed_to_smaller_unsigned::(); } else { signed_to_larger_same_unsigned::(); } signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); signed_to_larger_same_unsigned::(); if cfg!(target_pointer_width = "16") || cfg!(target_pointer_width = "32") || cfg!(target_pointer_width = "64") { signed_to_smaller_unsigned::(); } else { signed_to_larger_same_unsigned::(); } signed_to_smaller_unsigned::(); if cfg!(target_pointer_width = "16") { signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); } else if cfg!(target_pointer_width = "32") { signed_to_smaller_unsigned::(); signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); } else if cfg!(target_pointer_width = "64") { signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); signed_to_larger_same_unsigned::(); } else if cfg!(target_pointer_width = "128") { signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); signed_to_smaller_unsigned::(); } signed_to_larger_same_unsigned::(); signed_to_larger_same_unsigned::(); } fn unsigned_to_smaller_nonwrapping_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits > dst_nbits); let src_z00 = Src::zero(); let src_p01 = Src::one(); let src_p07 = !(!src_z00 << (dst_nbits - 1)); let src_p08 = src_p07 + src_p01; let src_p0f = src_p07 + src_p08; let src_p10 = src_p01 << dst_nbits; let dst_z0 = Dst::zero(); let dst_m1 = !dst_z0; let dst_m8 = dst_m1 << (dst_nbits - 1); let dst_p1 = Dst::one(); let dst_p7 = !dst_m8; assert_eq!(cast(src_z00), dst_z0); assert_eq!(cast(src_p01), dst_p1); assert_eq!(cast(src_p07), dst_p7); assert_eq!(checked_cast(src_z00), Some(dst_z0)); assert_eq!(checked_cast(src_p01), Some(dst_p1)); assert_eq!(checked_cast(src_p07), Some(dst_p7)); assert_eq!(checked_cast(src_p08), None); assert_eq!(checked_cast(src_p0f), None); assert_eq!(checked_cast(src_p10), None); assert_eq!(saturating_cast(src_z00), dst_z0); assert_eq!(saturating_cast(src_p01), dst_p1); assert_eq!(saturating_cast(src_p07), dst_p7); assert_eq!(saturating_cast(src_p08), dst_p7); assert_eq!(saturating_cast(src_p0f), dst_p7); assert_eq!(saturating_cast(src_p10), dst_p7); assert_eq!(wrapping_cast(src_z00), dst_z0); assert_eq!(wrapping_cast(src_p01), dst_p1); assert_eq!(wrapping_cast(src_p07), dst_p7); assert_eq!(wrapping_cast(src_p08), dst_m8); assert_eq!(wrapping_cast(src_p0f), dst_m1); assert_eq!(wrapping_cast(src_p10), dst_z0); assert_eq!(overflowing_cast(src_z00), (dst_z0, false)); assert_eq!(overflowing_cast(src_p01), (dst_p1, false)); assert_eq!(overflowing_cast(src_p07), (dst_p7, false)); assert_eq!(overflowing_cast(src_p08), (dst_m8, true)); assert_eq!(overflowing_cast(src_p0f), (dst_m1, true)); assert_eq!(overflowing_cast(src_p10), (dst_z0, true)); assert_eq!(unwrapped_cast(src_z00), dst_z0); assert_eq!(unwrapped_cast(src_p01), dst_p1); assert_eq!(unwrapped_cast(src_p07), dst_p7); } fn unsigned_to_same_nonwrapping_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits == dst_nbits); let src_z0 = Src::zero(); let src_p1 = Src::one(); let src_p7 = !(!src_z0 << (src_nbits - 1)); let src_p8 = src_p7 + src_p1; let src_pf = src_p7 + src_p8; let dst_z0 = Dst::zero(); let dst_m1 = !dst_z0; let dst_m8 = dst_m1 << (dst_nbits - 1); let dst_p1 = Dst::one(); let dst_p7 = !dst_m8; assert_eq!(cast(src_z0), dst_z0); assert_eq!(cast(src_p1), dst_p1); assert_eq!(cast(src_p7), dst_p7); assert_eq!(checked_cast(src_z0), Some(dst_z0)); assert_eq!(checked_cast(src_p1), Some(dst_p1)); assert_eq!(checked_cast(src_p7), Some(dst_p7)); assert_eq!(checked_cast(src_p8), None); assert_eq!(checked_cast(src_pf), None); assert_eq!(saturating_cast(src_z0), dst_z0); assert_eq!(saturating_cast(src_p1), dst_p1); assert_eq!(saturating_cast(src_p7), dst_p7); assert_eq!(saturating_cast(src_p8), dst_p7); assert_eq!(saturating_cast(src_pf), dst_p7); assert_eq!(wrapping_cast(src_z0), dst_z0); assert_eq!(wrapping_cast(src_p1), dst_p1); assert_eq!(wrapping_cast(src_p7), dst_p7); assert_eq!(wrapping_cast(src_p8), dst_m8); assert_eq!(wrapping_cast(src_pf), dst_m1); assert_eq!(overflowing_cast(src_z0), (dst_z0, false)); assert_eq!(overflowing_cast(src_p1), (dst_p1, false)); assert_eq!(overflowing_cast(src_p7), (dst_p7, false)); assert_eq!(overflowing_cast(src_p8), (dst_m8, true)); assert_eq!(overflowing_cast(src_pf), (dst_m1, true)); assert_eq!(unwrapped_cast(src_z0), dst_z0); assert_eq!(unwrapped_cast(src_p1), dst_p1); assert_eq!(unwrapped_cast(src_p7), dst_p7); } fn unsigned_to_larger_nonwrapping_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits < dst_nbits); let src_z0 = Src::zero(); let src_p1 = Src::one(); let src_p7 = !(!src_z0 << (src_nbits - 1)); let src_p8 = src_p7 + src_p1; let src_pf = src_p7 + src_p8; let dst_z00 = Dst::zero(); let dst_p01 = Dst::one(); let dst_p07 = !(!dst_z00 << (src_nbits - 1)); let dst_p08 = dst_p07 + dst_p01; let dst_p0f = dst_p07 + dst_p08; assert_eq!(cast(src_z0), dst_z00); assert_eq!(cast(src_p1), dst_p01); assert_eq!(cast(src_p7), dst_p07); assert_eq!(cast(src_p8), dst_p08); assert_eq!(cast(src_pf), dst_p0f); assert_eq!(checked_cast(src_z0), Some(dst_z00)); assert_eq!(checked_cast(src_p1), Some(dst_p01)); assert_eq!(checked_cast(src_p7), Some(dst_p07)); assert_eq!(checked_cast(src_p8), Some(dst_p08)); assert_eq!(checked_cast(src_pf), Some(dst_p0f)); assert_eq!(saturating_cast(src_z0), dst_z00); assert_eq!(saturating_cast(src_p1), dst_p01); assert_eq!(saturating_cast(src_p7), dst_p07); assert_eq!(saturating_cast(src_p8), dst_p08); assert_eq!(saturating_cast(src_pf), dst_p0f); assert_eq!(wrapping_cast(src_z0), dst_z00); assert_eq!(wrapping_cast(src_p1), dst_p01); assert_eq!(wrapping_cast(src_p7), dst_p07); assert_eq!(wrapping_cast(src_p8), dst_p08); assert_eq!(wrapping_cast(src_pf), dst_p0f); assert_eq!(overflowing_cast(src_z0), (dst_z00, false)); assert_eq!(overflowing_cast(src_p1), (dst_p01, false)); assert_eq!(overflowing_cast(src_p7), (dst_p07, false)); assert_eq!(overflowing_cast(src_p8), (dst_p08, false)); assert_eq!(overflowing_cast(src_pf), (dst_p0f, false)); assert_eq!(unwrapped_cast(src_z0), dst_z00); assert_eq!(unwrapped_cast(src_p1), dst_p01); assert_eq!(unwrapped_cast(src_p7), dst_p07); assert_eq!(unwrapped_cast(src_p8), dst_p08); assert_eq!(unwrapped_cast(src_pf), dst_p0f); } fn unsigned_to_smaller_wrapping_signed() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits > dst_nbits); let src_z00 = Src::zero(); let src_p01 = Src::one(); let src_p07 = !(!src_z00 << (dst_nbits - 1)); let src_p08 = src_p07 + src_p01; let src_p0f = src_p07 + src_p08; let src_p10 = src_p01 << dst_nbits; let dst_z0 = Wrapping(wrapping_cast(src_z00)); let dst_m1 = Wrapping(wrapping_cast(src_p0f)); let dst_m8 = Wrapping(wrapping_cast(src_p08)); let dst_p1 = Wrapping(wrapping_cast(src_p01)); let dst_p7 = Wrapping(wrapping_cast(src_p07)); assert_eq!(cast(src_z00), dst_z0); assert_eq!(cast(src_p01), dst_p1); assert_eq!(cast(src_p07), dst_p7); assert_eq!(cast(src_p08), dst_m8); assert_eq!(cast(src_p0f), dst_m1); assert_eq!(cast(src_p10), dst_z0); assert_eq!(checked_cast(src_z00), Some(dst_z0)); assert_eq!(checked_cast(src_p01), Some(dst_p1)); assert_eq!(checked_cast(src_p07), Some(dst_p7)); assert_eq!(checked_cast(src_p08), Some(dst_m8)); assert_eq!(checked_cast(src_p0f), Some(dst_m1)); assert_eq!(checked_cast(src_p10), Some(dst_z0)); assert_eq!(unwrapped_cast(src_z00), dst_z0); assert_eq!(unwrapped_cast(src_p01), dst_p1); assert_eq!(unwrapped_cast(src_p07), dst_p7); assert_eq!(unwrapped_cast(src_p08), dst_m8); assert_eq!(unwrapped_cast(src_p0f), dst_m1); assert_eq!(unwrapped_cast(src_p10), dst_z0); } fn unsigned_to_same_wrapping_signed() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits == dst_nbits); let src_z0 = Src::zero(); let src_p1 = Src::one(); let src_p7 = !(!src_z0 << (src_nbits - 1)); let src_p8 = src_p7 + src_p1; let src_pf = src_p7 + src_p8; let dst_z0 = Wrapping(wrapping_cast(src_z0)); let dst_m1 = Wrapping(wrapping_cast(src_pf)); let dst_m8 = Wrapping(wrapping_cast(src_p8)); let dst_p1 = Wrapping(wrapping_cast(src_p1)); let dst_p7 = Wrapping(wrapping_cast(src_p7)); assert_eq!(cast(src_z0), dst_z0); assert_eq!(cast(src_p1), dst_p1); assert_eq!(cast(src_p7), dst_p7); assert_eq!(cast(src_p8), dst_m8); assert_eq!(cast(src_pf), dst_m1); assert_eq!(checked_cast(src_z0), Some(dst_z0)); assert_eq!(checked_cast(src_p1), Some(dst_p1)); assert_eq!(checked_cast(src_p7), Some(dst_p7)); assert_eq!(checked_cast(src_p8), Some(dst_m8)); assert_eq!(checked_cast(src_pf), Some(dst_m1)); assert_eq!(unwrapped_cast(src_z0), dst_z0); assert_eq!(unwrapped_cast(src_p1), dst_p1); assert_eq!(unwrapped_cast(src_p7), dst_p7); assert_eq!(unwrapped_cast(src_p8), dst_m8); assert_eq!(unwrapped_cast(src_pf), dst_m1); } fn unsigned_to_larger_wrapping_signed() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits < dst_nbits); let src_z0 = Src::zero(); let src_p1 = Src::one(); let src_p7 = !(!src_z0 << (src_nbits - 1)); let src_p8 = src_p7 + src_p1; let src_pf = src_p7 + src_p8; let dst_z00 = Wrapping(wrapping_cast(src_z0)); let dst_p01 = Wrapping(wrapping_cast(src_p1)); let dst_p07 = Wrapping(wrapping_cast(src_p7)); let dst_p08 = Wrapping(wrapping_cast(src_p8)); let dst_p0f = Wrapping(wrapping_cast(src_pf)); assert_eq!(cast(src_z0), dst_z00); assert_eq!(cast(src_p1), dst_p01); assert_eq!(cast(src_p7), dst_p07); assert_eq!(cast(src_p8), dst_p08); assert_eq!(cast(src_pf), dst_p0f); assert_eq!(checked_cast(src_z0), Some(dst_z00)); assert_eq!(checked_cast(src_p1), Some(dst_p01)); assert_eq!(checked_cast(src_p7), Some(dst_p07)); assert_eq!(checked_cast(src_p8), Some(dst_p08)); assert_eq!(checked_cast(src_pf), Some(dst_p0f)); assert_eq!(unwrapped_cast(src_z0), dst_z00); assert_eq!(unwrapped_cast(src_p1), dst_p01); assert_eq!(unwrapped_cast(src_p7), dst_p07); assert_eq!(unwrapped_cast(src_p8), dst_p08); assert_eq!(unwrapped_cast(src_pf), dst_p0f); } fn unsigned_to_smaller_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { unsigned_to_smaller_nonwrapping_signed::(); unsigned_to_smaller_wrapping_signed::(); } fn unsigned_to_same_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { unsigned_to_same_nonwrapping_signed::(); unsigned_to_same_wrapping_signed::(); } fn unsigned_to_larger_signed() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { unsigned_to_larger_nonwrapping_signed::(); unsigned_to_larger_wrapping_signed::(); } #[test] fn unsigned_to_signed() { unsigned_to_same_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); unsigned_to_smaller_signed::(); unsigned_to_same_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); if cfg!(target_pointer_width = "16") { unsigned_to_same_signed::(); } else { unsigned_to_larger_signed::(); } unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_same_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); if cfg!(target_pointer_width = "16") { unsigned_to_smaller_signed::(); } else if cfg!(target_pointer_width = "32") { unsigned_to_same_signed::(); } else { unsigned_to_larger_signed::(); } unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_same_signed::(); unsigned_to_larger_signed::(); if cfg!(target_pointer_width = "16") || cfg!(target_pointer_width = "32") { unsigned_to_smaller_signed::(); } else if cfg!(target_pointer_width = "64") { unsigned_to_same_signed::(); } else { unsigned_to_larger_signed::(); } unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_same_signed::(); if cfg!(target_pointer_width = "16") || cfg!(target_pointer_width = "32") || cfg!(target_pointer_width = "64") { unsigned_to_smaller_signed::(); } else { unsigned_to_same_signed::(); } unsigned_to_smaller_signed::(); if cfg!(target_pointer_width = "16") { unsigned_to_same_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); } else if cfg!(target_pointer_width = "32") { unsigned_to_smaller_signed::(); unsigned_to_same_signed::(); unsigned_to_larger_signed::(); unsigned_to_larger_signed::(); } else if cfg!(target_pointer_width = "64") { unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_same_signed::(); unsigned_to_larger_signed::(); } else if cfg!(target_pointer_width = "128") { unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_smaller_signed::(); unsigned_to_same_signed::(); } unsigned_to_same_signed::(); } fn unsigned_to_smaller_nonwrapping_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits > dst_nbits); let src_z00 = Src::zero(); let src_p01 = Src::one(); let src_p07 = !(!src_z00 << (dst_nbits - 1)); let src_p08 = src_p07 + src_p01; let src_p0f = src_p07 + src_p08; let src_p10 = src_p01 << dst_nbits; let dst_z0 = Dst::zero(); let dst_p1 = Dst::one(); let dst_p7 = !(!dst_z0 << (dst_nbits - 1)); let dst_p8 = dst_p7 + dst_p1; let dst_pf = dst_p7 + dst_p8; assert_eq!(cast(src_z00), dst_z0); assert_eq!(cast(src_p01), dst_p1); assert_eq!(cast(src_p07), dst_p7); assert_eq!(cast(src_p08), dst_p8); assert_eq!(cast(src_p0f), dst_pf); assert_eq!(checked_cast(src_z00), Some(dst_z0)); assert_eq!(checked_cast(src_p01), Some(dst_p1)); assert_eq!(checked_cast(src_p07), Some(dst_p7)); assert_eq!(checked_cast(src_p08), Some(dst_p8)); assert_eq!(checked_cast(src_p0f), Some(dst_pf)); assert_eq!(checked_cast(src_p10), None); assert_eq!(saturating_cast(src_z00), dst_z0); assert_eq!(saturating_cast(src_p01), dst_p1); assert_eq!(saturating_cast(src_p07), dst_p7); assert_eq!(saturating_cast(src_p08), dst_p8); assert_eq!(saturating_cast(src_p0f), dst_pf); assert_eq!(saturating_cast(src_p10), dst_pf); assert_eq!(wrapping_cast(src_z00), dst_z0); assert_eq!(wrapping_cast(src_p01), dst_p1); assert_eq!(wrapping_cast(src_p07), dst_p7); assert_eq!(wrapping_cast(src_p08), dst_p8); assert_eq!(wrapping_cast(src_p0f), dst_pf); assert_eq!(wrapping_cast(src_p10), dst_z0); assert_eq!(overflowing_cast(src_z00), (dst_z0, false)); assert_eq!(overflowing_cast(src_p01), (dst_p1, false)); assert_eq!(overflowing_cast(src_p07), (dst_p7, false)); assert_eq!(overflowing_cast(src_p08), (dst_p8, false)); assert_eq!(overflowing_cast(src_p0f), (dst_pf, false)); assert_eq!(overflowing_cast(src_p10), (dst_z0, true)); assert_eq!(unwrapped_cast(src_z00), dst_z0); assert_eq!(unwrapped_cast(src_p01), dst_p1); assert_eq!(unwrapped_cast(src_p07), dst_p7); assert_eq!(unwrapped_cast(src_p08), dst_p8); assert_eq!(unwrapped_cast(src_p0f), dst_pf); } fn unsigned_to_larger_same_nonwrapping_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits <= dst_nbits); let src_z0 = Src::zero(); let src_p1 = Src::one(); let src_p7 = !(!src_z0 << (src_nbits - 1)); let src_p8 = src_p7 + src_p1; let src_pf = src_p7 + src_p8; let dst_z00 = Dst::zero(); let dst_p01 = Dst::one(); let dst_p07 = !(!dst_z00 << (src_nbits - 1)); let dst_p08 = dst_p07 + dst_p01; let dst_p0f = dst_p07 + dst_p08; assert_eq!(cast(src_z0), dst_z00); assert_eq!(cast(src_p1), dst_p01); assert_eq!(cast(src_p7), dst_p07); assert_eq!(cast(src_p8), dst_p08); assert_eq!(cast(src_pf), dst_p0f); assert_eq!(checked_cast(src_z0), Some(dst_z00)); assert_eq!(checked_cast(src_p1), Some(dst_p01)); assert_eq!(checked_cast(src_p7), Some(dst_p07)); assert_eq!(checked_cast(src_p8), Some(dst_p08)); assert_eq!(checked_cast(src_pf), Some(dst_p0f)); assert_eq!(saturating_cast(src_z0), dst_z00); assert_eq!(saturating_cast(src_p1), dst_p01); assert_eq!(saturating_cast(src_p7), dst_p07); assert_eq!(saturating_cast(src_p8), dst_p08); assert_eq!(saturating_cast(src_pf), dst_p0f); assert_eq!(wrapping_cast(src_z0), dst_z00); assert_eq!(wrapping_cast(src_p1), dst_p01); assert_eq!(wrapping_cast(src_p7), dst_p07); assert_eq!(wrapping_cast(src_p8), dst_p08); assert_eq!(wrapping_cast(src_pf), dst_p0f); assert_eq!(overflowing_cast(src_z0), (dst_z00, false)); assert_eq!(overflowing_cast(src_p1), (dst_p01, false)); assert_eq!(overflowing_cast(src_p7), (dst_p07, false)); assert_eq!(overflowing_cast(src_p8), (dst_p08, false)); assert_eq!(overflowing_cast(src_pf), (dst_p0f, false)); assert_eq!(unwrapped_cast(src_z0), dst_z00); assert_eq!(unwrapped_cast(src_p1), dst_p01); assert_eq!(unwrapped_cast(src_p7), dst_p07); assert_eq!(unwrapped_cast(src_p8), dst_p08); assert_eq!(unwrapped_cast(src_pf), dst_p0f); } fn unsigned_to_smaller_wrapping_unsigned() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits > dst_nbits); let src_z00 = Src::zero(); let src_p01 = Src::one(); let src_p07 = !(!src_z00 << (dst_nbits - 1)); let src_p08 = src_p07 + src_p01; let src_p0f = src_p07 + src_p08; let src_p10 = src_p01 << dst_nbits; let dst_z0 = Wrapping(wrapping_cast(src_z00)); let dst_p1 = Wrapping(wrapping_cast(src_p01)); let dst_p7 = Wrapping(wrapping_cast(src_p07)); let dst_p8 = Wrapping(wrapping_cast(src_p08)); let dst_pf = Wrapping(wrapping_cast(src_p0f)); assert_eq!(cast(src_z00), dst_z0); assert_eq!(cast(src_p01), dst_p1); assert_eq!(cast(src_p07), dst_p7); assert_eq!(cast(src_p08), dst_p8); assert_eq!(cast(src_p0f), dst_pf); assert_eq!(cast(src_p10), dst_z0); assert_eq!(checked_cast(src_z00), Some(dst_z0)); assert_eq!(checked_cast(src_p01), Some(dst_p1)); assert_eq!(checked_cast(src_p07), Some(dst_p7)); assert_eq!(checked_cast(src_p08), Some(dst_p8)); assert_eq!(checked_cast(src_p0f), Some(dst_pf)); assert_eq!(checked_cast(src_p10), Some(dst_z0)); assert_eq!(unwrapped_cast(src_z00), dst_z0); assert_eq!(unwrapped_cast(src_p01), dst_p1); assert_eq!(unwrapped_cast(src_p07), dst_p7); assert_eq!(unwrapped_cast(src_p08), dst_p8); assert_eq!(unwrapped_cast(src_p0f), dst_pf); assert_eq!(unwrapped_cast(src_p10), dst_z0); } fn unsigned_to_larger_same_wrapping_unsigned() where Src: WrappingCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { let src_nbits = mem::size_of::() * 8; let dst_nbits = mem::size_of::() * 8; assert!(src_nbits <= dst_nbits); let src_z0 = Src::zero(); let src_p1 = Src::one(); let src_p7 = !(!src_z0 << (src_nbits - 1)); let src_p8 = src_p7 + src_p1; let src_pf = src_p7 + src_p8; let dst_z00 = Wrapping(wrapping_cast(src_z0)); let dst_p01 = Wrapping(wrapping_cast(src_p1)); let dst_p07 = Wrapping(wrapping_cast(src_p7)); let dst_p08 = Wrapping(wrapping_cast(src_p8)); let dst_p0f = Wrapping(wrapping_cast(src_pf)); assert_eq!(cast(src_z0), dst_z00); assert_eq!(cast(src_p1), dst_p01); assert_eq!(cast(src_p7), dst_p07); assert_eq!(cast(src_p8), dst_p08); assert_eq!(cast(src_pf), dst_p0f); assert_eq!(checked_cast(src_z0), Some(dst_z00)); assert_eq!(checked_cast(src_p1), Some(dst_p01)); assert_eq!(checked_cast(src_p7), Some(dst_p07)); assert_eq!(checked_cast(src_p8), Some(dst_p08)); assert_eq!(checked_cast(src_pf), Some(dst_p0f)); assert_eq!(unwrapped_cast(src_z0), dst_z00); assert_eq!(unwrapped_cast(src_p1), dst_p01); assert_eq!(unwrapped_cast(src_p7), dst_p07); assert_eq!(unwrapped_cast(src_p8), dst_p08); assert_eq!(unwrapped_cast(src_pf), dst_p0f); } fn unsigned_to_smaller_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { unsigned_to_smaller_nonwrapping_unsigned::(); unsigned_to_smaller_wrapping_unsigned::(); } fn unsigned_to_larger_same_unsigned() where Src: Cast + CheckedCast + SaturatingCast + WrappingCast + OverflowingCast + UnwrappedCast + Cast> + CheckedCast> + UnwrappedCast>, Wrapping: Int, { unsigned_to_larger_same_nonwrapping_unsigned::(); unsigned_to_larger_same_wrapping_unsigned::(); } #[test] fn unsigned_to_unsigned() { unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); if cfg!(target_pointer_width = "16") { unsigned_to_smaller_unsigned::(); } else { unsigned_to_larger_same_unsigned::(); } unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); if cfg!(target_pointer_width = "16") || cfg!(target_pointer_width = "32") { unsigned_to_smaller_unsigned::(); } else { unsigned_to_larger_same_unsigned::(); } unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_larger_same_unsigned::(); if cfg!(target_pointer_width = "16") || cfg!(target_pointer_width = "32") || cfg!(target_pointer_width = "64") { unsigned_to_smaller_unsigned::(); } else { unsigned_to_larger_same_unsigned::(); } unsigned_to_smaller_unsigned::(); if cfg!(target_pointer_width = "16") { unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); } else if cfg!(target_pointer_width = "32") { unsigned_to_smaller_unsigned::(); unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); } else if cfg!(target_pointer_width = "64") { unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_larger_same_unsigned::(); } else if cfg!(target_pointer_width = "128") { unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); unsigned_to_smaller_unsigned::(); } unsigned_to_larger_same_unsigned::(); unsigned_to_larger_same_unsigned::(); } #[cfg(debug_assertions)] #[test] #[should_panic(expected = "overflow")] fn large_int_as_panic() { let _ = cast::(128); } #[cfg(not(debug_assertions))] #[test] fn large_int_as_wrap() { assert_eq!(cast::(128), -128); assert_eq!(cast::(-127), 129); } #[test] #[should_panic(expected = "overflow")] fn large_int_unwrapped_as_panic() { let _ = unwrapped_cast::(128); } az-1.2.1/src/tests/mod.rs000064400000000000000000000124510072674642500133500ustar 00000000000000// Copyright © 2019–2021 Trevor Spiteri // This library is free software: you can redistribute it and/or // modify it under the terms of either // // * the Apache License, Version 2.0 or // * the MIT License // // at your option. // // You should have recieved copies of the Apache License and the MIT // License along with the library. If not, see // and // . #![allow(clippy::float_cmp)] mod float_to_int; mod int_to_int; mod to_float; use crate::{ Az, CastFrom, CheckedAs, CheckedCastFrom, OverflowingAs, OverflowingCastFrom, Round, SaturatingAs, SaturatingCastFrom, UnwrappedAs, UnwrappedCastFrom, WrappingAs, WrappingCastFrom, }; use core::{ f32, f64, fmt::Debug, ops::{Add, Neg, Not, Shl, Shr, Sub}, }; trait Int where Self: Copy + Debug + Default + Ord, Self: Shl + Shr, Self: Not + Add + Sub, { #[inline] fn zero() -> Self { Self::default() } #[inline] fn one() -> Self { !(!Self::default() << 1) } } impl Int for I where I: Copy + Debug + Default + Ord, I: Shl + Shr, I: Not + Add + Sub, { } trait Float where Self: Copy + Debug + PartialOrd + From, Self: Neg + Add + Sub, { fn prec() -> usize; fn nan() -> Self; fn inf() -> Self; fn max() -> Self; fn int_shl(int: i8, shl: i8) -> Self; fn to_round(self) -> Round; } impl Float for f32 { fn prec() -> usize { 24 } fn nan() -> Self { f32::NAN } fn inf() -> Self { f32::INFINITY } fn max() -> Self { f32::MAX } fn int_shl(int: i8, shl: i8) -> Self { f32::from(int) * f32::from(shl).exp2() } fn to_round(self) -> Round { Round(self) } } impl Float for f64 { fn prec() -> usize { 53 } fn nan() -> Self { f64::NAN } fn inf() -> Self { f64::INFINITY } fn max() -> Self { f64::MAX } fn int_shl(int: i8, shl: i8) -> Self { f64::from(int) * f64::from(shl).exp2() } fn to_round(self) -> Round { Round(self) } } #[test] fn from() { assert_eq!(>::cast_from(1u8), 1i8); assert_eq!( >::checked_cast_from(1u8), Some(1i8) ); assert_eq!(>::checked_cast_from(255u8), None); assert_eq!( >::saturating_cast_from(1u8), 1i8 ); assert_eq!( >::saturating_cast_from(255u8), 127i8 ); assert_eq!(>::wrapping_cast_from(1u8), 1i8); assert_eq!( >::wrapping_cast_from(255u8), -1i8 ); assert_eq!( >::overflowing_cast_from(1u8), (1i8, false) ); assert_eq!( >::overflowing_cast_from(255u8), (-1i8, true) ); assert_eq!(>::unwrapped_cast_from(1u8), 1i8); } #[test] fn az() { assert_eq!(1.az::(), 1); assert_eq!((-1).checked_as::(), None); assert_eq!(1.checked_as::(), Some(1)); assert_eq!((-1).saturating_as::(), 0); assert_eq!(1000.saturating_as::(), 255); assert_eq!((-1).wrapping_as::(), 255); assert_eq!((-1).overflowing_as::(), (255, true)); assert_eq!(1.overflowing_as::(), (1, false)); assert_eq!(1.unwrapped_as::(), 1); } #[test] fn borrow_as() { use crate::{Cast, CheckedCast, OverflowingCast, SaturatingCast, UnwrappedCast, WrappingCast}; use core::borrow::Borrow; struct I(i32); impl Cast for &'_ I { fn cast(self) -> u32 { self.0.cast() } } impl CheckedCast for &'_ I { fn checked_cast(self) -> Option { self.0.checked_cast() } } impl SaturatingCast for &'_ I { fn saturating_cast(self) -> u32 { self.0.saturating_cast() } } impl WrappingCast for &'_ I { fn wrapping_cast(self) -> u32 { self.0.wrapping_cast() } } impl OverflowingCast for &'_ I { fn overflowing_cast(self) -> (u32, bool) { self.0.overflowing_cast() } } impl UnwrappedCast for &'_ I { fn unwrapped_cast(self) -> u32 { self.0.unwrapped_cast() } } let r = &I(12); assert_eq!(r.borrow().az::(), 12); assert_eq!(r.borrow().checked_as::(), Some(12)); assert_eq!(r.borrow().saturating_as::(), 12); assert_eq!(r.borrow().borrow().wrapping_as::(), 12); assert_eq!(r.borrow().overflowing_as::(), (12, false)); assert_eq!(r.borrow().unwrapped_as::(), 12); let r = &I(-5); assert_eq!(r.borrow().checked_as::(), None); assert_eq!(r.borrow().saturating_as::(), 0); assert_eq!(r.borrow().wrapping_as::(), 5u32.wrapping_neg()); assert_eq!( r.borrow().overflowing_as::(), (5u32.wrapping_neg(), true) ); } az-1.2.1/src/tests/to_float.rs000064400000000000000000000105650072674642500144040ustar 00000000000000// Copyright © 2019–2021 Trevor Spiteri // This library is free software: you can redistribute it and/or // modify it under the terms of either // // * the Apache License, Version 2.0 or // * the MIT License // // at your option. // // You should have recieved copies of the Apache License and the MIT // License along with the library. If not, see // and // . use crate::{cast, checked_cast, unwrapped_cast}; use core::{f32, f64}; macro_rules! slice_to_float { ($vals: expr) => { for &val in $vals { if val != val { assert!(cast::<_, f32>(val).is_nan()); assert!(checked_cast::<_, f32>(val).unwrap().is_nan()); assert!(unwrapped_cast::<_, f32>(val).is_nan()); assert!(cast::<_, f64>(val).is_nan()); assert!(checked_cast::<_, f64>(val).unwrap().is_nan()); assert!(unwrapped_cast::<_, f64>(val).is_nan()); } else { assert_eq!(cast::<_, f32>(val), val as f32); assert_eq!(checked_cast::<_, f32>(val), Some(val as f32)); assert_eq!(unwrapped_cast::<_, f32>(val), val as f32); assert_eq!(cast::<_, f64>(val), val as f64); assert_eq!(checked_cast::<_, f64>(val), Some(val as f64)); assert_eq!(unwrapped_cast::<_, f64>(val), val as f64); } } }; } #[test] fn to_float() { let small_i = &[ i8::min_value(), i8::min_value() + 1, -1, 0, 1, i8::max_value() - 1, i8::max_value(), ]; let small_u = &[0, 1, u8::max_value() - 1, u8::max_value()]; let small_f = &[ 0.0, f32::MIN_POSITIVE, 1.0, 1.5, 2.0, f32::MAX, f32::INFINITY, f32::NAN, ]; let big_i = &[ i128::min_value(), i128::min_value() + 1, -1, 0, 1, i128::max_value() - 1, i128::max_value(), ]; let big_u = &[0, 1, u128::max_value() - 1, u128::max_value()]; let big_f = &[ 0.0, f64::MIN_POSITIVE, 1.0, 1.5, 2.0, f64::MAX, f64::INFINITY, f64::NAN, ]; slice_to_float!(small_i); slice_to_float!(small_u); slice_to_float!(small_f); slice_to_float!(big_i); slice_to_float!(big_u); slice_to_float!(big_f); } #[test] fn specific_to_float() { // 0XFFFF_FE80 in 24 bits of precision becomes 0xFFFF_FE00 assert_eq!( cast::<_, f32>(0xFFFF_FE80u32), (24f32.exp2() - 2.0) * 8f32.exp2() ); // 0XFFFF_FE80..=0xFFFF_FF7F in 24 bits of precision become 0xFFFF_FF00 assert_eq!( cast::<_, f32>(0xFFFF_FE81u32), (24f32.exp2() - 1.0) * 8f32.exp2() ); assert_eq!( cast::<_, f32>(0xFFFF_FF00u32), (24f32.exp2() - 1.0) * 8f32.exp2() ); assert_eq!( cast::<_, f32>(0xFFFF_FF7Fu32), (24f32.exp2() - 1.0) * 8f32.exp2() ); // 0xFFFF_FF80 in 24 bits of precision becoms 0x1_0000_0000 assert_eq!(cast::<_, f32>(0xFFFF_FF80u32), 24f32.exp2() * 8f32.exp2()); assert_eq!(cast::<_, f32>(0xFFFF_FFFFu32), 24f32.exp2() * 8f32.exp2()); // Same as above, but source is 128-bit instead of 32-bit. // * (24f32.exp2() - 1.0) * 104f32.exp2() == f32::MAX // * 24f32.exp2() * 104f32.exp2() > f32::MAX, overflows to f32::INFINITY assert_eq!( cast::<_, f32>(0xFFFF_FE80_0000_0000_0000_0000_0000_0000u128), (24f32.exp2() - 2.0) * 104f32.exp2() ); assert_eq!( cast::<_, f32>(0xFFFF_FE80_0000_0000_0000_0000_0000_0001u128), f32::MAX ); assert_eq!( cast::<_, f32>(0xFFFF_FF00_0000_0000_0000_0000_0000_0000u128), f32::MAX ); assert_eq!( cast::<_, f32>(0xFFFF_FF7F_FFFF_FFFF_FFFF_FFFF_FFFF_FFFFu128), f32::MAX ); assert_eq!( cast::<_, f32>(0xFFFF_FF80_0000_0000_0000_0000_0000_0000u128), f32::INFINITY ); assert_eq!( cast::<_, f32>(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFFu128), f32::INFINITY ); assert_eq!(cast::<_, f32>(f64::MAX), f32::INFINITY); assert_eq!(cast::<_, f32>(-f64::MAX), f32::NEG_INFINITY); assert_eq!(cast::<_, f32>(-f64::MAX), f32::NEG_INFINITY); assert_eq!(cast::<_, f32>(f64::from(f32::MAX) + 1.0), f32::MAX); }