inline very simple "where"s

This commit is contained in:
Trevor Spiteri 2019-08-07 00:31:05 +02:00
parent d0faad2c0a
commit 3d8eaaac64
9 changed files with 128 additions and 478 deletions

View File

@ -95,15 +95,12 @@ macro_rules! fmt_radix2_helper {
fmt_radix2_helper! { u8 u16 u32 u64 u128 }
fn fmt_radix2_helper<F>(
fn fmt_radix2_helper<F: FmtRadix2Helper>(
frac_bits: u32,
(is_neg, mut int, mut frac): (bool, F, F),
radix: &dyn Radix2,
fmt: &mut Formatter,
) -> FmtResult
where
F: FmtRadix2Helper,
{
) -> FmtResult {
let int_bits = F::NBITS - frac_bits;
let digit_bits: u32 = radix.digit_bits();
// 128 binary digits, one radix point, one leading zero
@ -275,14 +272,11 @@ macro_rules! fmt_dec_helper {
fmt_dec_helper! { u8 u16 u32 u64 u128 }
fn fmt_dec_helper<F>(
fn fmt_dec_helper<F: FmtDecHelper>(
frac_bits: u32,
(is_neg, mut int, mut frac): (bool, F, F),
fmt: &mut Formatter,
) -> FmtResult
where
F: FmtDecHelper,
{
) -> FmtResult {
let int_bits = F::NBITS - frac_bits;
// 40 int digits
// + 128 frac digits

View File

@ -339,10 +339,7 @@ assert_eq!(two_point_75.to_string(), \"2.8\");
Frac: Unsigned + IsLessOrEqual<$Len, Output = True>,
{
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.to_bits().hash(state);
}
}

View File

@ -89,10 +89,7 @@ assert_eq!(Dst::from_fixed(src >> 4), Dst::from_bits(1));
[`wrapping_from_fixed`]: #method.wrapping_from_fixed
";
#[inline]
pub fn from_fixed<F>(val: F) -> $Fixed<Frac>
where
F: Fixed,
{
pub fn from_fixed<F: Fixed>(val: F) -> $Fixed<Frac> {
SealedFixed::from_fixed(val)
}
);
@ -127,10 +124,7 @@ assert_eq!((src >> 4u32).to_fixed::<Dst>(), Dst::from_bits(1));
[`wrapping_to_fixed`]: #method.wrapping_to_fixed
";
#[inline]
pub fn to_fixed<F>(self) -> F
where
F: Fixed,
{
pub fn to_fixed<F: Fixed>(self) -> F {
SealedFixed::from_fixed(self)
}
);
@ -180,10 +174,7 @@ assert_eq!(Fix::from_int(",
[`wrapping_from_int`]: #method.wrapping_from_int
";
#[inline]
pub fn from_int<I>(val: I) -> $Fixed<Frac>
where
I: Int,
{
pub fn from_int<I: Int>(val: I) -> $Fixed<Frac> {
SealedInt::to_fixed(val)
}
);
@ -236,10 +227,7 @@ assert_eq!(",
[`wrapping_to_int`]: #method.wrapping_to_int
";
#[inline]
pub fn to_int<I>(self) -> I
where
I: Int,
{
pub fn to_int<I: Int>(self) -> I {
SealedInt::from_fixed(self)
}
);
@ -288,10 +276,7 @@ assert_eq!(Fix::from_float(",
[finite]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_finite
";
#[inline]
pub fn from_float<F>(val: F) -> $Fixed<Frac>
where
F: Float,
{
pub fn from_float<F: Float>(val: F) -> $Fixed<Frac> {
SealedFloat::to_fixed(val)
}
);
@ -328,10 +313,7 @@ assert_eq!(max_fixed.to_float::<f32>(), std::f32::INFINITY);
[`f32`]: https://doc.rust-lang.org/nightly/std/primitive.f32.html
[`f64`]: https://doc.rust-lang.org/nightly/std/primitive.f64.html
";
pub fn to_float<F>(self) -> F
where
F: Float,
{
pub fn to_float<F: Float>(self) -> F {
SealedFixed::to_float(self)
}
);
@ -361,10 +343,7 @@ assert!(Dst::checked_from_fixed(too_large).is_none());
[`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None
";
#[inline]
pub fn checked_from_fixed<F>(val: F) -> Option<$Fixed<Frac>>
where
F: Fixed,
{
pub fn checked_from_fixed<F: Fixed>(val: F) -> Option<$Fixed<Frac>> {
SealedFixed::checked_from_fixed(val)
}
);
@ -395,10 +374,7 @@ assert!(Src::max_value().checked_to_fixed::<TooFewIntBits>().is_none());
[`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None
";
#[inline]
pub fn checked_to_fixed<F>(self) -> Option<F>
where
F: Fixed,
{
pub fn checked_to_fixed<F: Fixed>(self) -> Option<F> {
SealedFixed::checked_from_fixed(self)
}
);
@ -447,10 +423,7 @@ assert!(Fix::checked_from_int(too_small).is_none());
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
";
#[inline]
pub fn checked_from_int<I>(val: I) -> Option<$Fixed<Frac>>
where
I: Int,
{
pub fn checked_from_int<I: Int>(val: I) -> Option<$Fixed<Frac>> {
SealedInt::checked_to_fixed(val)
}
);
@ -508,10 +481,7 @@ assert!(AllInt::",
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
";
#[inline]
pub fn checked_to_int<I>(self) -> Option<I>
where
I: Int,
{
pub fn checked_to_int<I: Int>(self) -> Option<I> {
SealedInt::checked_from_fixed(self)
}
);
@ -552,10 +522,7 @@ assert!(Fix::checked_from_float(std::f64::NAN).is_none());
[`f64`]: https://doc.rust-lang.org/nightly/std/primitive.f64.html
";
#[inline]
pub fn checked_from_float<F>(val: F) -> Option<$Fixed<Frac>>
where
F: Float,
{
pub fn checked_from_float<F: Float>(val: F) -> Option<$Fixed<Frac>> {
SealedFloat::checked_to_fixed(val)
}
);
@ -591,10 +558,7 @@ assert_eq!(Dst::saturating_from_fixed(too_small), Dst::min_value());
```
";
#[inline]
pub fn saturating_from_fixed<F>(val: F) -> $Fixed<Frac>
where
F: Fixed,
{
pub fn saturating_from_fixed<F: Fixed>(val: F) -> $Fixed<Frac> {
SealedFixed::saturating_from_fixed(val)
}
);
@ -623,10 +587,7 @@ assert_eq!(saturated, TooFewIntBits::max_value());
```
";
#[inline]
pub fn saturating_to_fixed<F>(self) -> F
where
F: Fixed,
{
pub fn saturating_to_fixed<F: Fixed>(self) -> F {
SealedFixed::saturating_from_fixed(self)
}
);
@ -674,10 +635,7 @@ assert_eq!(Fix::saturating_from_int(too_small), Fix::min_value());
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
";
#[inline]
pub fn saturating_from_int<I>(val: I) -> $Fixed<Frac>
where
I: Int,
{
pub fn saturating_from_int<I: Int>(val: I) -> $Fixed<Frac> {
SealedInt::saturating_to_fixed(val)
}
);
@ -743,10 +701,7 @@ assert_eq!(",
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
";
#[inline]
pub fn saturating_to_int<I>(self) -> I
where
I: Int,
{
pub fn saturating_to_int<I: Int>(self) -> I {
SealedInt::saturating_from_fixed(self)
}
);
@ -785,10 +740,7 @@ assert_eq!(Fix::saturating_from_float(f64::NEG_INFINITY), Fix::min_value());
[NaN]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_nan
";
#[inline]
pub fn saturating_from_float<F>(val: F) -> $Fixed<Frac>
where
F: Float,
{
pub fn saturating_from_float<F: Float>(val: F) -> $Fixed<Frac> {
SealedFloat::saturating_to_fixed(val)
}
);
@ -825,10 +777,7 @@ assert_eq!(Dst::wrapping_from_fixed(too_large), wrapped);
```
";
#[inline]
pub fn wrapping_from_fixed<F>(val: F) -> $Fixed<Frac>
where
F: Fixed,
{
pub fn wrapping_from_fixed<F: Fixed>(val: F) -> $Fixed<Frac> {
SealedFixed::wrapping_from_fixed(val)
}
);
@ -858,10 +807,7 @@ assert_eq!(Src::max_value().wrapping_to_fixed::<TooFewIntBits>(), wrapped);
```
";
#[inline]
pub fn wrapping_to_fixed<F>(self) -> F
where
F: Fixed,
{
pub fn wrapping_to_fixed<F: Fixed>(self) -> F {
SealedFixed::wrapping_from_fixed(self)
}
);
@ -909,10 +855,7 @@ assert_eq!(Fix::wrapping_from_int(large), wrapped);
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
";
#[inline]
pub fn wrapping_from_int<I>(val: I) -> $Fixed<Frac>
where
I: Int,
{
pub fn wrapping_from_int<I: Int>(val: I) -> $Fixed<Frac> {
SealedInt::wrapping_to_fixed(val)
}
);
@ -978,10 +921,7 @@ assert_eq!(",
[`usize`]: https://doc.rust-lang.org/nightly/std/primitive.usize.html
";
#[inline]
pub fn wrapping_to_int<I>(self) -> I
where
I: Int,
{
pub fn wrapping_to_int<I: Int>(self) -> I {
SealedInt::wrapping_from_fixed(self)
}
);
@ -1030,10 +970,7 @@ assert_eq!(Fix::wrapping_from_float(large), wrapped);
[finite]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_finite
";
#[inline]
pub fn wrapping_from_float<F>(val: F) -> $Fixed<Frac>
where
F: Float,
{
pub fn wrapping_from_float<F: Float>(val: F) -> $Fixed<Frac> {
SealedFloat::wrapping_to_fixed(val)
}
);
@ -1077,10 +1014,7 @@ assert_eq!(Dst::overflowing_from_fixed(too_large), (wrapped, true));
[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
";
#[inline]
pub fn overflowing_from_fixed<F>(val: F) -> ($Fixed<Frac>, bool)
where
F: Fixed,
{
pub fn overflowing_from_fixed<F: Fixed>(val: F) -> ($Fixed<Frac>, bool) {
SealedFixed::overflowing_from_fixed(val)
}
);
@ -1114,10 +1048,7 @@ assert_eq!(Src::max_value().overflowing_to_fixed::<TooFewIntBits>(), (wrapped, t
```
";
#[inline]
pub fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: Fixed,
{
pub fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
SealedFixed::overflowing_from_fixed(self)
}
);
@ -1170,10 +1101,7 @@ assert_eq!(Fix::overflowing_from_int(large), (wrapped, true));
[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
";
#[inline]
pub fn overflowing_from_int<I>(val: I) -> ($Fixed<Frac>, bool)
where
I: Int,
{
pub fn overflowing_from_int<I: Int>(val: I) -> ($Fixed<Frac>, bool) {
SealedInt::overflowing_to_fixed(val)
}
);
@ -1239,10 +1167,7 @@ assert_eq!(does_not_fit.overflowing_to_int::<",
[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
";
#[inline]
pub fn overflowing_to_int<I>(self) -> (I, bool)
where
I: Int,
{
pub fn overflowing_to_int<I: Int>(self) -> (I, bool) {
SealedInt::overflowing_from_fixed(self)
}
);
@ -1296,10 +1221,7 @@ assert_eq!(Fix::overflowing_from_float(large), (wrapped, true));
[tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
";
#[inline]
pub fn overflowing_from_float<F>(val: F) -> ($Fixed<Frac>, bool)
where
F: Float,
{
pub fn overflowing_from_float<F: Float>(val: F) -> ($Fixed<Frac>, bool) {
SealedFloat::overflowing_to_fixed(val)
}
);

View File

@ -48,49 +48,30 @@ pub trait SealedFixed: Copy + Debug + Default + Display + Eq + Hash + Ord {
const INT_LSB: u128 = Self::INT_MASK ^ (Self::INT_MASK << 1);
#[inline]
fn from_fixed<F>(val: F) -> Self
where
F: Fixed,
{
fn from_fixed<F: Fixed>(val: F) -> Self {
let (wrapped, overflow) = SealedFixed::overflowing_from_fixed(val);
debug_assert!(!overflow, "{} overflows", val);
let _ = overflow;
wrapped
}
#[inline]
fn checked_from_fixed<F>(val: F) -> Option<Self>
where
F: Fixed,
{
fn checked_from_fixed<F: Fixed>(val: F) -> Option<Self> {
match SealedFixed::overflowing_from_fixed(val) {
(_, true) => None,
(wrapped, false) => Some(wrapped),
}
}
fn saturating_from_fixed<F>(fixed: F) -> Self
where
F: Fixed;
fn saturating_from_fixed<F: Fixed>(fixed: F) -> Self;
#[inline]
fn wrapping_from_fixed<F>(val: F) -> Self
where
F: Fixed,
{
fn wrapping_from_fixed<F: Fixed>(val: F) -> Self {
let (wrapped, _) = SealedFixed::overflowing_from_fixed(val);
wrapped
}
fn overflowing_from_fixed<F>(fixed: F) -> (Self, bool)
where
F: Fixed;
fn overflowing_from_fixed<F: Fixed>(fixed: F) -> (Self, bool);
fn saturating_from_float<F>(float: F) -> Self
where
F: SealedFloat;
fn overflowing_from_float<F>(float: F) -> (Self, bool)
where
F: SealedFloat;
fn to_float<F>(self) -> F
where
F: SealedFloat;
fn saturating_from_float<F: SealedFloat>(float: F) -> Self;
fn overflowing_from_float<F: SealedFloat>(float: F) -> (Self, bool);
fn to_float<F: SealedFloat>(self) -> F;
fn from_sbits(bits: Self::SBits) -> Self;
fn to_sbits(self) -> Self::SBits;
@ -119,10 +100,7 @@ macro_rules! sealed_fixed {
type SBits = $Bits;
#[inline]
fn saturating_from_fixed<F>(val: F) -> Self
where
F: Fixed,
{
fn saturating_from_fixed<F: Fixed>(val: F) -> Self {
let (value, _, overflow) = val.to_sbits().to_fixed_dir_overflow(
F::FRAC_NBITS as i32,
Self::FRAC_NBITS,
@ -157,10 +135,7 @@ macro_rules! sealed_fixed {
}
#[inline]
fn overflowing_from_fixed<F>(val: F) -> (Self, bool)
where
F: Fixed,
{
fn overflowing_from_fixed<F: Fixed>(val: F) -> (Self, bool) {
let (value, _, mut overflow) = val.to_sbits().to_fixed_dir_overflow(
F::FRAC_NBITS as i32,
Self::FRAC_NBITS,
@ -189,10 +164,7 @@ macro_rules! sealed_fixed {
}
#[inline]
fn saturating_from_float<F>(val: F) -> Self
where
F: SealedFloat,
{
fn saturating_from_float<F: SealedFloat>(val: F) -> Self {
if val.is_nan() {
panic!("NaN");
}
@ -228,10 +200,7 @@ macro_rules! sealed_fixed {
$Fixed::from_bits(bits)
}
#[inline]
fn overflowing_from_float<F>(val: F) -> (Self, bool)
where
F: SealedFloat,
{
fn overflowing_from_float<F: SealedFloat>(val: F) -> (Self, bool) {
if !val.is_finite() {
panic!("{} is not finite", val);
}
@ -260,10 +229,7 @@ macro_rules! sealed_fixed {
}
#[inline]
fn to_float<F>(self) -> F
where
F: SealedFloat,
{
fn to_float<F: SealedFloat>(self) -> F {
let (neg, abs) = self.to_bits().neg_abs();
SealedFloat::from_neg_abs(neg, u128::from(abs), Self::FRAC_NBITS, Self::INT_NBITS)
}

View File

@ -44,20 +44,14 @@ pub trait SealedFloat: Copy + Debug + Display {
fn from_parts(sign: bool, exp: i32, mant: Self::Bits) -> Self;
#[inline]
fn to_fixed<F>(self) -> F
where
F: Fixed,
{
fn to_fixed<F: Fixed>(self) -> F {
let (wrapped, overflow) = Self::overflowing_to_fixed(self);
debug_assert!(!overflow, "{} overflows", self);
let _ = overflow;
wrapped
}
#[inline]
fn checked_to_fixed<F>(self) -> Option<F>
where
F: Fixed,
{
fn checked_to_fixed<F: Fixed>(self) -> Option<F> {
if !self.is_finite() {
return None;
}
@ -67,25 +61,16 @@ pub trait SealedFloat: Copy + Debug + Display {
}
}
#[inline]
fn saturating_to_fixed<F>(self) -> F
where
F: Fixed,
{
fn saturating_to_fixed<F: Fixed>(self) -> F {
SealedFixed::saturating_from_float(self)
}
#[inline]
fn wrapping_to_fixed<F>(self) -> F
where
F: Fixed,
{
fn wrapping_to_fixed<F: Fixed>(self) -> F {
let (wrapped, _) = Self::overflowing_to_fixed(self);
wrapped
}
#[inline]
fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: Fixed,
{
fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
SealedFixed::overflowing_from_float(self)
}

View File

@ -35,74 +35,48 @@ pub trait SealedInt: Copy + Ord + Debug + Display {
const MSB: Self;
#[inline]
fn from_fixed<F>(val: F) -> Self
where
F: Fixed,
{
fn from_fixed<F: Fixed>(val: F) -> Self {
let (wrapped, overflow) = Self::overflowing_from_fixed(val);
debug_assert!(!overflow, "{} overflows", val);
let _ = overflow;
wrapped
}
#[inline]
fn checked_from_fixed<F>(val: F) -> Option<Self>
where
F: Fixed,
{
fn checked_from_fixed<F: Fixed>(val: F) -> Option<Self> {
match Self::overflowing_from_fixed(val) {
(_, true) => None,
(wrapped, false) => Some(wrapped),
}
}
fn saturating_from_fixed<F>(val: F) -> Self
where
F: Fixed;
fn saturating_from_fixed<F: Fixed>(val: F) -> Self;
#[inline]
fn wrapping_from_fixed<F>(val: F) -> Self
where
F: Fixed,
{
fn wrapping_from_fixed<F: Fixed>(val: F) -> Self {
let (wrapped, _) = Self::overflowing_from_fixed(val);
wrapped
}
fn overflowing_from_fixed<F>(val: F) -> (Self, bool)
where
F: Fixed;
fn overflowing_from_fixed<F: Fixed>(val: F) -> (Self, bool);
#[inline]
fn to_fixed<F>(self) -> F
where
F: Fixed,
{
fn to_fixed<F: Fixed>(self) -> F {
let (wrapped, overflow) = Self::overflowing_to_fixed(self);
debug_assert!(!overflow, "{} overflows", self);
let _ = overflow;
wrapped
}
#[inline]
fn checked_to_fixed<F>(self) -> Option<F>
where
F: Fixed,
{
fn checked_to_fixed<F: Fixed>(self) -> Option<F> {
match Self::overflowing_to_fixed(self) {
(_, true) => None,
(wrapped, false) => Some(wrapped),
}
}
fn saturating_to_fixed<F>(self) -> F
where
F: Fixed;
fn saturating_to_fixed<F: Fixed>(self) -> F;
#[inline]
fn wrapping_to_fixed<F>(self) -> F
where
F: Fixed,
{
fn wrapping_to_fixed<F: Fixed>(self) -> F {
let (wrapped, _) = Self::overflowing_to_fixed(self);
wrapped
}
fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: Fixed;
fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool);
fn min_value() -> Self;
fn max_value() -> Self;
@ -146,34 +120,22 @@ macro_rules! sealed_int {
}
#[inline]
fn saturating_from_fixed<F>(val: F) -> Self
where
F: Fixed,
{
fn saturating_from_fixed<F: Fixed>(val: F) -> Self {
let saturated = Self::ReprFixed::saturating_from_fixed(val);
IntRepr::from_int_repr(saturated.to_bits())
}
#[inline]
fn overflowing_from_fixed<F>(val: F) -> (Self, bool)
where
F: Fixed,
{
fn overflowing_from_fixed<F: Fixed>(val: F) -> (Self, bool) {
let (wrapped, overflow) = Self::ReprFixed::overflowing_from_fixed(val);
(IntRepr::from_int_repr(wrapped.to_bits()), overflow)
}
#[inline]
fn saturating_to_fixed<F>(self) -> F
where
F: Fixed,
{
fn saturating_to_fixed<F: Fixed>(self) -> F {
SealedFixed::saturating_from_fixed(self.to_repr_fixed())
}
#[inline]
fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: Fixed
{
fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
SealedFixed::overflowing_from_fixed(self.to_repr_fixed())
}

View File

@ -30,10 +30,7 @@ macro_rules! serde_fixed {
where
Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let bits = self.to_bits();
let mut state = serializer.serialize_struct($Name, 1)?;
state.serialize_field("bits", &bits)?;
@ -45,10 +42,7 @@ macro_rules! serde_fixed {
where
Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct FixedVisitor;
impl<'de> Visitor<'de> for FixedVisitor {
@ -59,20 +53,14 @@ macro_rules! serde_fixed {
formatter.write_str($Name)
}
fn visit_seq<V>(self, mut seq: V) -> Result<$TBits, V::Error>
where
V: SeqAccess<'de>,
{
fn visit_seq<V: SeqAccess<'de>>(self, mut seq: V) -> Result<$TBits, V::Error> {
let bits = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(0, &self))?;
Ok(bits)
}
fn visit_map<V>(self, mut map: V) -> Result<$TBits, V::Error>
where
V: MapAccess<'de>,
{
fn visit_map<V: MapAccess<'de>>(self, mut map: V) -> Result<$TBits, V::Error> {
let mut bits = None;
while let Some(key) = map.next_key()? {
match key {
@ -114,10 +102,7 @@ enum Field {
}
impl<'de> Deserialize<'de> for Field {
fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
where
D: Deserializer<'de>,
{
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Field, D::Error> {
struct FieldVisitor;
impl<'de> Visitor<'de> for FieldVisitor {
@ -127,10 +112,7 @@ impl<'de> Deserialize<'de> for Field {
formatter.write_str("`bits`")
}
fn visit_str<E>(self, value: &str) -> Result<Field, E>
where
E: de::Error,
{
fn visit_str<E: de::Error>(self, value: &str) -> Result<Field, E> {
match value {
"bits" => Ok(Field::Bits),
_ => Err(de::Error::unknown_field(value, FIELDS)),

View File

@ -655,33 +655,26 @@ pub trait FromFixed {
/// [`wrapping_from_fixed`] instead.
///
/// [`wrapping_from_fixed`]: #method.wrapping_from_fixed
fn from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed;
fn from_fixed<F: sealed::Fixed>(val: F) -> Self;
/// Converts from a fixed-point number if it fits, otherwise returns [`None`].
///
/// Any extra fractional bits are truncated.
///
/// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None
fn checked_from_fixed<F>(val: F) -> Option<Self>
fn checked_from_fixed<F: sealed::Fixed>(val: F) -> Option<Self>
where
F: sealed::Fixed,
Self: Sized;
/// Converts from a fixed-point number, saturating if it does not fit.
///
/// Any extra fractional bits are truncated.
fn saturating_from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed;
fn saturating_from_fixed<F: sealed::Fixed>(val: F) -> Self;
/// Converts from a fixed-point number, wrapping if it does not fit.
///
/// Any extra fractional bits are truncated.
fn wrapping_from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed;
fn wrapping_from_fixed<F: sealed::Fixed>(val: F) -> Self;
/// Converts from a fixed-point number.
///
@ -693,9 +686,8 @@ pub trait FromFixed {
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
fn overflowing_from_fixed<F>(val: F) -> (Self, bool)
fn overflowing_from_fixed<F: sealed::Fixed>(val: F) -> (Self, bool)
where
F: sealed::Fixed,
Self: Sized;
}
@ -736,32 +728,24 @@ pub trait ToFixed {
/// [`wrapping_to_fixed`] instead.
///
/// [`wrapping_to_fixed`]: #method.wrapping_to_fixed
fn to_fixed<F>(self) -> F
where
F: sealed::Fixed;
fn to_fixed<F: sealed::Fixed>(self) -> F;
/// Converts to a fixed-point number if it fits, otherwise returns [`None`].
///
/// Any extra fractional bits are truncated.
///
/// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None
fn checked_to_fixed<F>(self) -> Option<F>
where
F: sealed::Fixed;
fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F>;
/// Converts to a fixed-point number, saturating if it does not fit.
///
/// Any extra fractional bits are truncated.
fn saturating_to_fixed<F>(self) -> F
where
F: sealed::Fixed;
fn saturating_to_fixed<F: sealed::Fixed>(self) -> F;
/// Converts to a fixed-point number, wrapping if it does not fit.
///
/// Any extra fractional bits are truncated.
fn wrapping_to_fixed<F>(self) -> F
where
F: sealed::Fixed;
fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F;
/// Converts from a fixed-point number.
///
@ -773,45 +757,28 @@ pub trait ToFixed {
///
/// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
/// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html
fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: sealed::Fixed;
fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool);
}
impl ToFixed for bool {
#[inline]
fn to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn to_fixed<F: sealed::Fixed>(self) -> F {
ToFixed::to_fixed(u8::from(self))
}
#[inline]
fn checked_to_fixed<F>(self) -> Option<F>
where
F: sealed::Fixed,
{
fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F> {
ToFixed::checked_to_fixed(u8::from(self))
}
#[inline]
fn saturating_to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn saturating_to_fixed<F: sealed::Fixed>(self) -> F {
ToFixed::saturating_to_fixed(u8::from(self))
}
#[inline]
fn wrapping_to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F {
ToFixed::wrapping_to_fixed(u8::from(self))
}
#[inline]
fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: sealed::Fixed,
{
fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool) {
ToFixed::overflowing_to_fixed(u8::from(self))
}
}
@ -820,76 +787,46 @@ macro_rules! impl_int {
($Int:ident) => {
impl FromFixed for $Int {
#[inline]
fn from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn from_fixed<F: sealed::Fixed>(val: F) -> Self {
SealedInt::from_fixed(val)
}
#[inline]
fn checked_from_fixed<F>(val: F) -> Option<Self>
where
F: sealed::Fixed,
{
fn checked_from_fixed<F: sealed::Fixed>(val: F) -> Option<Self> {
SealedInt::checked_from_fixed(val)
}
#[inline]
fn saturating_from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn saturating_from_fixed<F: sealed::Fixed>(val: F) -> Self {
SealedInt::saturating_from_fixed(val)
}
#[inline]
fn wrapping_from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn wrapping_from_fixed<F: sealed::Fixed>(val: F) -> Self {
SealedInt::wrapping_from_fixed(val)
}
#[inline]
fn overflowing_from_fixed<F>(val: F) -> (Self, bool)
where
F: sealed::Fixed,
{
fn overflowing_from_fixed<F: sealed::Fixed>(val: F) -> (Self, bool) {
SealedInt::overflowing_from_fixed(val)
}
}
impl ToFixed for $Int {
#[inline]
fn to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn to_fixed<F: sealed::Fixed>(self) -> F {
SealedInt::to_fixed(self)
}
#[inline]
fn checked_to_fixed<F>(self) -> Option<F>
where
F: sealed::Fixed,
{
fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F> {
SealedInt::checked_to_fixed(self)
}
#[inline]
fn saturating_to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn saturating_to_fixed<F: sealed::Fixed>(self) -> F {
SealedInt::saturating_to_fixed(self)
}
#[inline]
fn wrapping_to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F {
SealedInt::wrapping_to_fixed(self)
}
#[inline]
fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: sealed::Fixed,
{
fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool) {
SealedInt::overflowing_to_fixed(self)
}
}
@ -913,76 +850,46 @@ macro_rules! impl_float {
($Float:ty) => {
impl FromFixed for $Float {
#[inline]
fn from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn from_fixed<F: sealed::Fixed>(val: F) -> Self {
val.to_float()
}
#[inline]
fn checked_from_fixed<F>(val: F) -> Option<Self>
where
F: sealed::Fixed,
{
fn checked_from_fixed<F: sealed::Fixed>(val: F) -> Option<Self> {
Some(val.to_float())
}
#[inline]
fn saturating_from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn saturating_from_fixed<F: sealed::Fixed>(val: F) -> Self {
val.to_float()
}
#[inline]
fn wrapping_from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn wrapping_from_fixed<F: sealed::Fixed>(val: F) -> Self {
val.to_float()
}
#[inline]
fn overflowing_from_fixed<F>(val: F) -> (Self, bool)
where
F: sealed::Fixed,
{
fn overflowing_from_fixed<F: sealed::Fixed>(val: F) -> (Self, bool) {
(val.to_float(), false)
}
}
impl ToFixed for $Float {
#[inline]
fn to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn to_fixed<F: sealed::Fixed>(self) -> F {
SealedFloat::to_fixed(self)
}
#[inline]
fn checked_to_fixed<F>(self) -> Option<F>
where
F: sealed::Fixed,
{
fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F> {
SealedFloat::checked_to_fixed(self)
}
#[inline]
fn saturating_to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn saturating_to_fixed<F: sealed::Fixed>(self) -> F {
SealedFloat::saturating_to_fixed(self)
}
#[inline]
fn wrapping_to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F {
SealedFloat::wrapping_to_fixed(self)
}
#[inline]
fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: sealed::Fixed,
{
fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool) {
SealedFloat::overflowing_to_fixed(self)
}
}
@ -1110,38 +1017,23 @@ macro_rules! impl_fixed {
Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>,
{
#[inline]
fn from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn from_fixed<F: sealed::Fixed>(val: F) -> Self {
SealedFixed::from_fixed(val)
}
#[inline]
fn checked_from_fixed<F>(val: F) -> Option<Self>
where
F: sealed::Fixed,
{
fn checked_from_fixed<F: sealed::Fixed>(val: F) -> Option<Self> {
SealedFixed::checked_from_fixed(val)
}
#[inline]
fn saturating_from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn saturating_from_fixed<F: sealed::Fixed>(val: F) -> Self {
SealedFixed::saturating_from_fixed(val)
}
#[inline]
fn wrapping_from_fixed<F>(val: F) -> Self
where
F: sealed::Fixed,
{
fn wrapping_from_fixed<F: sealed::Fixed>(val: F) -> Self {
SealedFixed::wrapping_from_fixed(val)
}
#[inline]
fn overflowing_from_fixed<F>(val: F) -> (Self, bool)
where
F: sealed::Fixed,
{
fn overflowing_from_fixed<F: sealed::Fixed>(val: F) -> (Self, bool) {
SealedFixed::overflowing_from_fixed(val)
}
}
@ -1151,38 +1043,23 @@ macro_rules! impl_fixed {
Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>,
{
#[inline]
fn to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn to_fixed<F: sealed::Fixed>(self) -> F {
SealedFixed::from_fixed(self)
}
#[inline]
fn checked_to_fixed<F>(self) -> Option<F>
where
F: sealed::Fixed,
{
fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F> {
SealedFixed::checked_from_fixed(self)
}
#[inline]
fn saturating_to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn saturating_to_fixed<F: sealed::Fixed>(self) -> F {
SealedFixed::saturating_from_fixed(self)
}
#[inline]
fn wrapping_to_fixed<F>(self) -> F
where
F: sealed::Fixed,
{
fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F {
SealedFixed::wrapping_from_fixed(self)
}
#[inline]
fn overflowing_to_fixed<F>(self) -> (F, bool)
where
F: sealed::Fixed,
{
fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool) {
SealedFixed::overflowing_from_fixed(self)
}
}

View File

@ -48,14 +48,9 @@ use core::{
/// assert_eq!(Fix::min_value(), (max + delta).0);
/// ```
#[repr(transparent)]
pub struct Wrapping<F>(pub F)
where
F: Fixed;
pub struct Wrapping<F: Fixed>(pub F);
impl<F> Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> Wrapping<F> {
/// Wrapping ceil. Rounds to the next integer towards +∞, wrapping
/// on overflow.
///
@ -128,94 +123,64 @@ where
}
}
impl<F> Clone for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> Clone for Wrapping<F> {
#[inline]
fn clone(&self) -> Wrapping<F> {
Wrapping(self.0)
}
}
impl<F> Copy for Wrapping<F> where F: Fixed {}
impl<F: Fixed> Copy for Wrapping<F> {}
impl<F> Default for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> Default for Wrapping<F> {
#[inline]
fn default() -> Wrapping<F> {
Wrapping(F::default())
}
}
impl<F> Hash for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> Hash for Wrapping<F> {
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
fn hash<H: Hasher>(&self, state: &mut H) {
(self.0).hash(state);
}
}
impl<F> Debug for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> Debug for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
<F as Debug>::fmt(&self.0, f)
}
}
impl<F> Display for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> Display for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
<F as Display>::fmt(&self.0, f)
}
}
impl<F> From<F> for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> From<F> for Wrapping<F> {
#[inline]
fn from(src: F) -> Wrapping<F> {
Wrapping(src)
}
}
impl<F> Eq for Wrapping<F> where F: Fixed {}
impl<F> PartialEq<Wrapping<F>> for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> Eq for Wrapping<F> {}
impl<F: Fixed> PartialEq<Wrapping<F>> for Wrapping<F> {
#[inline]
fn eq(&self, other: &Wrapping<F>) -> bool {
(self.0).eq(&other.0)
}
}
impl<F> Ord for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> Ord for Wrapping<F> {
#[inline]
fn cmp(&self, other: &Wrapping<F>) -> Ordering {
(self.0).cmp(&other.0)
}
}
impl<F> PartialOrd<Wrapping<F>> for Wrapping<F>
where
F: Fixed,
{
impl<F: Fixed> PartialOrd<Wrapping<F>> for Wrapping<F> {
#[inline]
fn partial_cmp(&self, other: &Wrapping<F>) -> Option<Ordering> {
(self.0).partial_cmp(&other.0)