add unwrapped_{to,from}_fixed to {To,From}Fixed traits
They have a default implementation for backwards compatibility.
This commit is contained in:
parent
a030d7c5d1
commit
0c764ca53d
|
@ -86,6 +86,10 @@ The conversions supported cover the following cases.
|
|||
[`checked_sub`][f-cs-1-6], [`checked_mul_int`][f-cmi-1-6],
|
||||
[`checked_shl`][f-cshl-1-6], [`checked_shr`][f-cshr-1-6],
|
||||
[`checked_abs`][f-cab-1-6]
|
||||
* The [`unwrapped_to_fixed`][f-utf-1-6] method was added to the
|
||||
[`ToFixed`][f-tf-1-6] trait.
|
||||
* The [`unwrapped_from_fixed`][f-uff-1-6] method was added to the
|
||||
[`FromFixed`][f-ff-1-6] trait.
|
||||
|
||||
[f-cab-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_abs
|
||||
[f-cad-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_add
|
||||
|
@ -94,6 +98,10 @@ The conversions supported cover the following cases.
|
|||
[f-cs-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_sub
|
||||
[f-cshl-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_shl
|
||||
[f-cshr-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_shr
|
||||
[f-ff-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.FromFixed.html
|
||||
[f-tf-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.ToFixed.html
|
||||
[f-uff-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.FromFixed.html#tymethod.unwrapped_from_fixed
|
||||
[f-utf-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.ToFixed.html#tymethod.unwrapped_to_fixed
|
||||
|
||||
### Version 1.5.0 news (2020-11-05)
|
||||
|
||||
|
|
|
@ -14,6 +14,10 @@ Version 1.6.0 (unreleased)
|
|||
[`checked_sub`][f-cs-1-6], [`checked_mul_int`][f-cmi-1-6],
|
||||
[`checked_shl`][f-cshl-1-6], [`checked_shr`][f-cshr-1-6],
|
||||
[`checked_abs`][f-cab-1-6]
|
||||
* The [`unwrapped_to_fixed`][f-utf-1-6] method was added to the
|
||||
[`ToFixed`][f-tf-1-6] trait.
|
||||
* The [`unwrapped_from_fixed`][f-uff-1-6] method was added to the
|
||||
[`FromFixed`][f-ff-1-6] trait.
|
||||
|
||||
[f-cab-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_abs
|
||||
[f-cad-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_add
|
||||
|
@ -22,6 +26,10 @@ Version 1.6.0 (unreleased)
|
|||
[f-cs-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_sub
|
||||
[f-cshl-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_shl
|
||||
[f-cshr-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_shr
|
||||
[f-ff-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.FromFixed.html
|
||||
[f-tf-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.ToFixed.html
|
||||
[f-uff-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.FromFixed.html#tymethod.unwrapped_from_fixed
|
||||
[f-utf-1-6]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.ToFixed.html#tymethod.unwrapped_to_fixed
|
||||
|
||||
Version 1.5.0 (2020-11-05)
|
||||
==========================
|
||||
|
|
|
@ -1760,6 +1760,26 @@ pub trait FromFixed {
|
|||
fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool)
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
/// Converts from a fixed-point number, panicking if the value
|
||||
/// does not fit.
|
||||
///
|
||||
/// Any extra fractional bits are discarded, which rounds towards −∞.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the value does not fit, even when debug assertions
|
||||
/// are not enabled.
|
||||
#[inline]
|
||||
fn unwrapped_from_fixed<F: Fixed>(src: F) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
match Self::overflowing_from_fixed(src) {
|
||||
(val, false) => val,
|
||||
(_, true) => panic!("overflow"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// This trait provides checked conversions to fixed-point numbers.
|
||||
|
@ -1850,6 +1870,28 @@ pub trait ToFixed {
|
|||
/// [finite]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_finite
|
||||
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
|
||||
fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool);
|
||||
|
||||
/// Converts to a fixed-point number, panicking if it does not fit.
|
||||
///
|
||||
/// Any extra fractional bits are discarded, which rounds towards −∞.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `self` is a floating-point number that is not
|
||||
/// [finite] or if the value does not fit, even if debug
|
||||
/// assertions are not enabled.
|
||||
///
|
||||
/// [finite]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_finite
|
||||
#[inline]
|
||||
fn unwrapped_to_fixed<F: Fixed>(self) -> F
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
match self.overflowing_to_fixed() {
|
||||
(val, false) => val,
|
||||
(_, true) => panic!("overflow"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToFixed for bool {
|
||||
|
|
Loading…
Reference in New Issue