reimplement From<bool> for fixed-point numbers

This commit is contained in:
Trevor Spiteri 2019-02-04 12:46:42 +01:00
parent 004077a002
commit af9cda1078
3 changed files with 63 additions and 3 deletions

View File

@ -39,6 +39,12 @@ numeric primitives are implemented. That is, you can use [`From`] or
## Whats new
### Version 0.3.1 news (unreleased)
* Reimplement [`From<bool>`][`From`] for all fixed-point types which
can represent the integer 1. This was inadvertently removed in
0.3.0.
### Version 0.3.0 news (2019-02-03)
#### Highlights

View File

@ -5,6 +5,12 @@ modification, are permitted in any medium without royalty provided the
copyright notice and this notice are preserved. This file is offered
as-is, without any warranty. -->
Version 0.3.1 (unreleased)
==========================
* Reimplement `From<bool>` for all fixed-point types which can
represent the integer 1. This was inadvertently removed in 0.3.0.
Version 0.3.0 (2019-02-03)
==========================

View File

@ -14,7 +14,7 @@
// <https://opensource.org/licenses/MIT>.
use core::ops::{Add, Sub};
use frac::{IsGreaterOrEqual, IsLessOrEqual, True, Unsigned, U0, U1, U128, U16, U32, U64, U8};
use frac::{IsGreaterOrEqual, IsLessOrEqual, True, Unsigned, U0, U1, U128, U16, U2, U32, U64, U8};
#[cfg(feature = "f16")]
use half::f16;
use {
@ -191,6 +191,46 @@ int_to_fixed! { (u64, i64, U64) -> (FixedU128, FixedI128, U128) }
int_to_fixed! { (u128, i128) -> (FixedU128, FixedI128) }
macro_rules! bool_to_fixed {
($DstU:ident, $DstI:ident, $DstBits:ident) => {
// Condition: FracDst <= $DstBits - 1
impl<FracDst> From<bool> for $DstU<FracDst>
where
FracDst: Unsigned
+ IsLessOrEqual<$DstBits, Output = True>
+ IsLessOrEqual<<$DstBits as Sub<U1>>::Output, Output = True>,
{
#[inline]
fn from(src: bool) -> $DstU<FracDst> {
let unshifted = $DstU::<FracDst>::from_bits(src.into()).to_bits();
let shift = FracDst::U32;
$DstU::<FracDst>::from_bits(unshifted << shift)
}
}
// Condition: FracDst <= $DstBits - 2
impl<FracDst> From<bool> for $DstI<FracDst>
where
FracDst: Unsigned
+ IsLessOrEqual<$DstBits, Output = True>
+ IsLessOrEqual<<$DstBits as Sub<U2>>::Output, Output = True>,
{
#[inline]
fn from(src: bool) -> $DstI<FracDst> {
let unshifted = $DstI::<FracDst>::from_bits(src.into()).to_bits();
let shift = FracDst::U32;
$DstI::<FracDst>::from_bits(unshifted << shift)
}
}
};
}
bool_to_fixed! { FixedU8, FixedI8, U8 }
bool_to_fixed! { FixedU16, FixedI16, U16 }
bool_to_fixed! { FixedU32, FixedI32, U32 }
bool_to_fixed! { FixedU64, FixedI64, U64 }
bool_to_fixed! { FixedU128, FixedI128, U128 }
macro_rules! fixed_to_int {
(($SrcU:ident, $SrcI:ident) -> ($DstU:ident, $DstI:ident)) => {
impl From<$SrcU<U0>> for $DstU {
@ -390,6 +430,14 @@ mod tests {
}
}
#[test]
fn from_bool() {
assert_eq!(FixedI8::<frac::U6>::from(true), 1);
assert_eq!(FixedI8::<frac::U6>::from(false), 0);
assert_eq!(FixedI128::<frac::U64>::from(true), 1);
assert_eq!(FixedU128::<frac::U127>::from(true), 1);
}
#[test]
fn signed_from_float() {
type Fix = FixedI8<frac::U4>;