conv
This crate provides a number of conversion traits with more specific semantics than those provided by as
or From
/Into
.
The goal with the traits provided here is to be more specific about what generic code can rely on, as well as provide reasonably self-describing alternatives to the standard From
/Into
traits. For example, the although T: From<U>
might be satisfied in generic code, this says nothing about what kind of conversion that represents.
In addition, From
/Into
provide no facility for a conversion failing, meaning that implementations may need to choose between conversions that may not be valid, or panicking; neither option is appealing in general.
Links
Compatibility
conv
is compatible with Rust 1.2 and higher.
Examples
# extern crate conv;
# use *;
#
Change Log
v0.3.2
- Added integer ↔
char
conversions. - Added missing
isize
/usize
→f32
/f64
conversions. - Fixed the error type of
i64
→usize
for 64-bit targets.
v0.3.1
- Change to
unwrap_ok
for better codegen (thanks bluss). - Fix for Rust breaking change (code in question was dodgy anyway; thanks m4rw3r).
v0.3.0
- Added an
Error
constraint to allErr
associated types. This will break any user-defined conversions where theErr
type does not implementError
. - Renamed the
Overflow
andUnderflow
errors toPosOverflow
andNegOverflow
respectively. In the context of floating point conversions, "underflow" usually means the value was too close to zero to correctly represent.
v0.2.1
- Added
ConvUtil::into_as<Dst>
as a shortcut forInto::<Dst>::into
. - Added
#[inline]
attributes. - Added
Saturate::saturate
, which can saturateResult
s arising from over/underflow.
v0.2.0
- Changed all error types to include the original input as payload. This breaks pretty much everything. Sorry about that. On the bright side, there's now no downside to using the conversion traits for non-
Copy
types. - Added the normal rounding modes for float → int approximations:
RoundToNearest
,RoundToNegInf
,RoundToPosInf
, andRoundToZero
. ApproxWith
is now subsumed by a pair of extension traits (ConvUtil
andConvAsUtil
), that also have shortcuts forTryInto
andValueInto
so that you can specify the destination type on the method.