Expand description
Type conversion, success expected
This library exists to make fallible numeric type conversions easy, without
resorting to the as keyword.
Convis likeFrom, but supports fallible conversionsCastis toConvwhatIntois toFromConvApproxandCastApproxsupport fallible, approximate conversionConvFloatandCastFloatare similar, providing precise control over rounding
If you are wondering “why not just use as”, there are a few reasons:
- integer conversions may silently truncate or sign-extend which does not preserve value
- prior to Rust 1.45.0 float-to-int conversions were not fully defined; since this version they use saturating conversion (NaN converts to 0)
- you want some assurance (at least in debug builds) that the conversion will preserve values correctly
Why might you not want to use this library?
- You want saturating / truncating / other non-value-preserving conversion
- You want to convert non-numeric types (
Fromsupports a lot more conversions thanConvdoes)! - You want a thoroughly tested library (we’re not quite there yet)
§Error handling
All traits support two methods:
try_*methods return aResultand always fail if the correct conversion is not possible- other methods may panic or return incorrect results
In debug builds, methods not returning Result must panic on failure. As
with the overflow checks on Rust’s standard integer arithmetic, this is
considered a tool for finding logic errors. In release builds, these methods
are permitted to return defined but incorrect results similar to the as
keyword.
If the always_assert feature flag is set, assertions will be turned on in
all builds. Some additional feature flags are available for finer-grained
control (see Cargo.toml).
§no_std support
When the crate’s default features are disabled (and std is not enabled)
then the library supports no_std. In this case, ConvFloat and
CastFloat are only available if the libm optional dependency is
enabled.
Modules§
- traits
- Traits
Enums§
- Error
- Error types for conversions
Traits§
- Cast
- Like
Into, but forConv - Cast
Approx - Like
Into, but forConvApprox - Cast
Float - Like
Into, but forConvFloat - Conv
- Like
From, but supports fallible conversions - Conv
Approx - Like
From, but for approximate numerical conversions - Conv
Float - Nearest / floor / ceiling conversions from floating point types