az/
float.rs

1// Copyright © 2019–2021 Trevor Spiteri
2
3// This library is free software: you can redistribute it and/or
4// modify it under the terms of either
5//
6//   * the Apache License, Version 2.0 or
7//   * the MIT License
8//
9// at your option.
10//
11// You should have recieved copies of the Apache License and the MIT
12// License along with the library. If not, see
13// <https://www.apache.org/licenses/LICENSE-2.0> and
14// <https://opensource.org/licenses/MIT>.
15
16use crate::{cast, Cast, CheckedCast, Round, UnwrappedCast};
17use core::fmt::{Debug, Display, Formatter, LowerExp, Result as FmtResult, UpperExp};
18
19macro_rules! convert {
20    ($($Src:ty),* => $Dst:ty) => { $(
21        impl Cast<$Dst> for $Src {
22            #[inline]
23            fn cast(self) -> $Dst {
24                self as $Dst
25            }
26        }
27
28        impl CheckedCast<$Dst> for $Src {
29            #[inline]
30            fn checked_cast(self) -> Option<$Dst> {
31                Some(cast(self))
32            }
33        }
34
35        impl UnwrappedCast<$Dst> for $Src {
36            #[inline]
37            fn unwrapped_cast(self) -> $Dst {
38                cast(self)
39            }
40        }
41    )* };
42}
43
44convert! { i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64 => f32 }
45convert! { i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64 => f64 }
46
47impl<T: Display> Display for Round<T> {
48    fn fmt(&self, f: &mut Formatter) -> FmtResult {
49        Display::fmt(&self.0, f)
50    }
51}
52
53impl<T: Debug> Debug for Round<T> {
54    fn fmt(&self, f: &mut Formatter) -> FmtResult {
55        Debug::fmt(&self.0, f)
56    }
57}
58
59impl<T: LowerExp> LowerExp for Round<T> {
60    fn fmt(&self, f: &mut Formatter) -> FmtResult {
61        LowerExp::fmt(&self.0, f)
62    }
63}
64
65impl<T: UpperExp> UpperExp for Round<T> {
66    fn fmt(&self, f: &mut Formatter) -> FmtResult {
67        UpperExp::fmt(&self.0, f)
68    }
69}