Expand description
§Converting values
This library exists to make numeric type conversions easy and
generic without resorting to the as keyword.
- Use
CastandConvinstead ofIntoandFromfor exact conversions - Use
CastApproxandConvApproxfor approximate conversions (rounding mode is implementation-defined just likeas) - Use
CastToandConvTofor conversions with a specific rounding mode (see § Rounding modes).
If this sounds like a lot of traits, consider the above are all essentially
syntactic sugar for ConvTo (see
§ Implementing traits).
§Quick example
use easy_cast::{Cast, Conv, CastApprox, CastTo, Nearest};
let _: i32 = 15_usize.cast(); // exact conversion
let _ = usize::conv(20_u32); // exact conversion
let _: f32 = u32::MAX.cast_approx(); // approximates to 2^32
let _: i32 = 11.9_f32.cast_to(Nearest); // rounds to 12§Rounding modes
The Rounding trait (used with CastTo and ConvTo) supports
genericity over rounding modes:
Exactspecifies that no rounding is allowed (loss of precision is an error)Approxspecifies that rounding is allowed. The rounding mode used is a property of the implementation, but usually aligns withasnumeric casts.Trunc,Floor,CeilandNearestallow more precise control over rounding
All rounding modes require that the result is close to the input value. For
a more precise definition, see
§ Limits of approximation.
§Error handling
Unlike From or TryFrom, this library’s traits are implemented
regardless of fallibility. All conversion traits have an associated Error
type which is expected to be one of:
std::convert::Infalliblefor infallible conversionsRangeErrorfor conversions which may fail due to domain errorsErrorfor conversions which may fail due to domain or loss-of-precision errors.
Further, all traits have two methods:
- A
try_method (e.g.Cast::try_cast) which returns aResult - A “derived” method (e.g.
Cast::cast) with § Fallback behaviour
§Fallback behaviour
In debug builds, the “derived” method must panic on failure. This is also
the case if the always_assert feature flag is enabled (for this library’s
implementations).
Otherwise (in release builds without extra assertions enabled), more
flexible behaviour of the “derived” methods is allowed. The implementations
provided by easy-cast mostly reduce to as numeric casts (with extra
rounding where required).
§Implementing traits
Implement conversions which cannot lose precision using ConvExact.
Implement all other conversions using ConvTo for one or several
Rounding modes.
Modules§
- traits
- Traits
Macros§
- impl_
via_ from - Implement
ConvExactinfallibly over aFromimplementation - impl_
via_ identity - Implement an identity “conversion” via
ConvExactinfallibly
Structs§
- Approx
- Approximate conversion
- Ceil
libmorstd - Round towards positive infinity (ceiling)
- Exact
- Exact conversion only
- Floor
libmorstd - Round towards negative infinity (floor)
- Nearest
libmorstd - Round to the nearest representable value
- Range
Error - Source value lies outside of target type’s range
- Trunc
- Truncation towards zero
Enums§
- Error
- Error types for conversions
Traits§
- Cast
- Like
Into, but forConv - Cast
Approx - Like
Into, but forConvApprox - CastTo
- Generic “into” conversion trait with specified rounding mode
- Conv
- Like
From, but supports fallible conversions - Conv
Approx - Like
From, but for approximate numerical conversions - Conv
Exact - Generic “from” conversion trait for exact conversions
- ConvTo
- Generic “from” conversion trait with specified rounding mode
- Rounding
- Rounding mode