108: int: document PrimInt trait r=cuviper a=dvdhrm
This documents the PrimInt trait, explains its intentions and features
as well as mentions its origins for future reference.
Closes#11.
Co-authored-by: David Rheinsberg <david.rheinsberg@gmail.com>
90: Fix CheckedShl/CheckedShr documentation r=cuviper a=samueltardieu
Fix#57 and more:
- CheckedShl was hinting that None was returned on overflow rather than
on too large a rhs.
- Ditto for CheckedShr.
- CheckedShr documentation erroneously indicated that a left shift was
going to be performed instead of a right shift.
Co-authored-by: Samuel Tardieu <sam@rfc1149.net>
Fix#57 and more:
- CheckedShl was hinting that None was returned on overflow rather than
on too large a rhs.
- Ditto for CheckedShr.
- CheckedShr documentation erroneously indicated that a left shift was
going to be performed instead of a right shift.
81: Add wrapping shifts r=cuviper a=edmccard
Add traits `WrappingShl` and `WrappingShr` corresponding to the
standard library `wrapping_shl` and `wrapping_shr` methods. Implement
the trait on all primitive integer types as well as on `Wrapping`.
Co-authored-by: Ed McCardell <edmccard@hotmail.com>
Add traits `WrappingShl` and `WrappingShr` corresponding to the
standard library `wrapping_shl` and `wrapping_shr` methods. Implement
the trait on all primitive integer types as well as on `Wrapping`.
73: Avoid `as` casts in default FromPrimitive methods r=cuviper a=cuviper
Particularly, the default `from_f64` used `n as i64`, which has
undefined behavior on overflow, kind of defeating the purpose here.
Now we use a checked `to_i64()` for this, and even try `to_u64()` as a
fallback for completeness.
Also make similar changes to default `ToPrimitive`, but at least it
didn't have the same casting problem.
Co-authored-by: Josh Stone <cuviper@gmail.com>
Particularly, the default `from_f64` used `n as i64`, which has
undefined behavior on overflow, kind of defeating the purpose here.
Now we use a checked `to_i64()` for this, and even try `to_u64()` as a
fallback for completeness.
(All of the primitive implementations already do better, at least.)
This includes new conditional methods `ToPrimitive::{to_i128,to_u128}`
and `FromPrimitive::{from_i128,from_u128}`. Since features can only be
additive, these methods must not cause a breaking change to anyone when
enabled -- thus they have a default implementation that converts through
64-bit values. Types that can do better with a full 128-bit integer,
like bigint or floating-point, will probably want to override these.