| 1 | Easy-cast |
| 2 | ========= |
| 3 | |
| 4 | [](https://github.com/kas-gui/easy-cast/actions) |
| 5 | [](https://docs.rs/easy-cast) |
| 6 | |
| 7 | Type conversion, success expected |
| 8 | |
| 9 | This library is written to make numeric type conversions easy. Such |
| 10 | conversions usually fall into one of the following cases: |
| 11 | |
| 12 | - the conversion must preserve values exactly (use [`From`] or [`Into`] |
| 13 | or [`Conv`] or [`Cast`]) |
| 14 | - the conversion is expected to preserve values exactly, though this is |
| 15 | not ensured by the types in question (use [`Conv`] or [`Cast`]) |
| 16 | - the conversion could fail and must be checked at run-time (use |
| 17 | [`TryFrom`] or [`TryInto`] or [`Conv::try_conv`] or [`Cast::try_cast`]) |
| 18 | - the conversion is from floating point values to integers and should |
| 19 | round to the "nearest" integer (use [`ConvFloat`] or [`CastFloat`]) |
| 20 | - the conversion is from `f32` to `f64` or vice-versa; in this case use of |
| 21 | `as f32` / `as f64` is likely acceptable since `f32` has special |
| 22 | representations for non-finite values and conversion to `f64` is exact |
| 23 | - truncating conversion (modular arithmetic) is desired; in this case `as` |
| 24 | probably does exactly what you want |
| 25 | - saturating conversion is desired (less common; not supported here) |
| 26 | |
| 27 | If you are wondering "why not just use `as`", there are a few reasons: |
| 28 | |
| 29 | - integer conversions may silently truncate |
| 30 | - integer conversions to/from signed types silently reinterpret |
| 31 | - prior to Rust 1.45.0 float-to-int conversions were not fully defined; |
| 32 | since this version they use saturating conversion (NaN converts to 0) |
| 33 | - you want some assurance (at least in debug builds) that the conversion |
| 34 | will preserve values correctly without having to proof-read code |
| 35 | |
| 36 | When should you *not* use this library? |
| 37 | |
| 38 | - Only numeric conversions are supported |
| 39 | - Conversions from floats do not provide fine control of rounding modes |
| 40 | - This library has not been thoroughly tested correctness |
| 41 | |
| 42 | [`From`]: https://doc.rust-lang.org/stable/std/convert/trait.From.html |
| 43 | [`Into`]: https://doc.rust-lang.org/stable/std/convert/trait.Into.html |
| 44 | [`TryFrom`]: https://doc.rust-lang.org/stable/std/convert/trait.TryFrom.html |
| 45 | [`TryInto`]: https://doc.rust-lang.org/stable/std/convert/trait.TryInto.html |
| 46 | [`Conv`]: https://docs.rs/easy-cast/latest/easy_cast/trait.Conv.html |
| 47 | [`Cast`]: https://docs.rs/easy-cast/latest/easy_cast/trait.Cast.html |
| 48 | [`Conv::try_conv`]: https://docs.rs/easy-cast/latest/easy_cast/trait.Conv.html#tymethod.try_conv |
| 49 | [`Conv::try_cast`]: https://docs.rs/easy-cast/latest/easy_cast/trait.Conv.html#tymethod.try_cast |
| 50 | [`ConvFloat`]: https://docs.rs/easy-cast/latest/easy_cast/trait.ConvFloat.html |
| 51 | [`CastFloat`]: https://docs.rs/easy-cast/latest/easy_cast/trait.CastFloat.html |
| 52 | |
| 53 | ## Assertions |
| 54 | |
| 55 | All type conversions which are potentially fallible assert on failure in |
| 56 | debug builds. In release builds assertions may be omitted, thus making |
| 57 | incorrect conversions possible. |
| 58 | |
| 59 | If the `always_assert` feature flag is set, assertions will be turned on in |
| 60 | all builds. Some additional feature flags are available for finer-grained |
| 61 | control (see [Cargo.toml](Cargo.toml)). |
| 62 | |
| 63 | ## MSRV and no_std |
| 64 | |
| 65 | The Minumum Supported Rust Version is 1.32.0 (first release of Edition 2018). |
| 66 | |
| 67 | By default, `std` support is required. With default features disabled `no_std` |
| 68 | is supported, but the `ConvFloat` and `CastFloat` traits are unavailable. |
| 69 | Enabling the `libm` feature will re-enable these traits. |
| 70 | |
| 71 | |
| 72 | Copyright and Licence |
| 73 | ------- |
| 74 | |
| 75 | The [COPYRIGHT](COPYRIGHT) file includes a list of contributors who claim |
| 76 | copyright on this project. This list may be incomplete; new contributors may |
| 77 | optionally add themselves to this list. |
| 78 | |
| 79 | The easy-cast library is published under the terms of the Apache License, Version 2.0. |
| 80 | You may obtain a copy of this licence from the [LICENSE](LICENSE) file or on |
| 81 | the following webpage: <https://www.apache.org/licenses/LICENSE-2.0> |