1use crate::muldiv::MulDiv;
2use crate::typenum::consts::Z0;
3use crate::typenum::{Integer, IsLess, B1, U10};
4use crate::{Fix, FromUnsigned, Pow};
5
6pub trait FixExt: Sized {
8 fn one() -> Self;
10}
11
12impl<Bits, Exp> FixExt for Fix<Bits, U10, Exp>
13where
14 Bits: FromUnsigned + Pow,
15 Exp: Integer + IsLess<Z0, Output = B1>,
16{
17 fn one() -> Self {
18 let base = Bits::from_unsigned::<U10>();
19 Fix::new(base.pow(Exp::to_i32().unsigned_abs()))
20 }
21}
22
23impl<Bits, Exp> Fix<Bits, U10, Exp>
24where
25 Self: FixExt,
26 Bits: MulDiv<Output = Bits>,
27{
28 pub fn checked_convert<ToExp>(self) -> Option<Fix<Bits, U10, ToExp>>
37 where
38 Fix<Bits, U10, ToExp>: FixExt,
39 {
40 let target_one = Fix::<Bits, U10, ToExp>::one();
41 let source_one = Self::one();
42 target_one.mul_div_floor(self, source_one)
43 }
44}