1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
//! **Development in progress**
//!
//! Type-level rational numbers based on [typenum].
//!
//! [typenum]: https://crates.io/crates/typenum
#![no_std]
#![cfg_attr(test, recursion_limit = "256")]
use core::marker::PhantomData;
use typenum::Gcd;
#[doc(no_inline)]
pub use typenum::consts::*;
#[doc(no_inline)]
pub use typenum::{Equal, Greater, Less, NInt, PInt};
mod uint;
pub use uint::NonZeroUnsigned;
mod int;
pub use int::Integer;
mod marker;
pub use marker::{NonNegative, NonZero, NotOne};
/// Type-level integers usable as denominators of type-level rational numbers.
///
/// # Examples
///
/// ```
/// use typerat::*;
///
/// fn is_denominator<D: Denominator>() -> bool {
/// true
/// }
///
/// assert!(is_denominator::<P1>());
/// assert!(is_denominator::<P2>());
/// ```
///
/// ```compile_fail
/// # use typerat::*;
/// #
/// # fn is_denominator<D: Denominator>() -> bool {
/// # true
/// # }
/// #
/// assert!(is_denominator::<Z0>());
/// ```
///
/// ```compile_fail
/// # use typerat::*;
/// #
/// # fn is_denominator<D: Denominator>() -> bool {
/// # true
/// # }
/// #
/// assert!(is_denominator::<N1>());
/// ```
pub trait Denominator: Integer + NonNegative + NonZero {}
impl<D> Denominator for D where D: Integer + NonNegative + NonZero {}
/// Type-level integers usable as numerators of type-level rational numbers with denominator `D`.
///
/// # Examples
///
/// ```
/// use typerat::*;
///
/// fn is_numerator_for_denominator_2<N: Numerator<P2>>() -> bool {
/// true
/// }
///
/// assert!(is_numerator_for_denominator_2::<P1>());
/// assert!(is_numerator_for_denominator_2::<N1>());
/// assert!(is_numerator_for_denominator_2::<P3>());
/// assert!(is_numerator_for_denominator_2::<N3>());
/// ```
///
/// ```compile_fail
/// # use typerat::*;
/// #
/// # fn is_numerator_for_denominator_2<N: Numerator<P2>>() -> bool {
/// # true
/// # }
/// #
/// assert!(is_numerator_for_denominator_2::<P2>());
/// ```
///
/// ```compile_fail
/// # use typerat::*;
/// #
/// # fn is_numerator_for_denominator_2<N: Numerator<P2>>() -> bool {
/// # true
/// # }
/// #
/// assert!(is_numerator_for_denominator_2::<N2>());
/// ```
///
/// ```compile_fail
/// # use typerat::*;
/// #
/// # fn is_numerator_for_denominator_2<N: Numerator<P2>>() -> bool {
/// # true
/// # }
/// #
/// assert!(is_numerator_for_denominator_2::<P4>());
/// ```
pub trait Numerator<D = P1>: Integer + Gcd<D, Output = P1> {}
impl<N, D> Numerator<D> for N
where
N: Integer + Gcd<D, Output = P1>,
D: Denominator,
{
}
/// `Q<N, D>` represents a type-level rational number with numerator `N` and denominator `D`.
#[derive(Debug)]
pub struct Q<N, D = P1>(PhantomData<(N, D)>)
where
N: Numerator<D>,
D: Denominator;
impl<N, D> Q<N, D>
where
N: Numerator<D>,
D: Denominator,
{
const SELF: Self = Q(PhantomData);
/// Constructs a new instance of this type-level rational number.
pub const fn new() -> Self {
Self::SELF
}
/// Borrows an instance of this type-level rational number.
pub const fn borrow<'a>() -> &'a Self {
&Self::SELF
}
}
impl<N, D> Clone for Q<N, D>
where
N: Numerator<D>,
D: Denominator,
{
fn clone(&self) -> Self {
*self
}
}
impl<N, D> Copy for Q<N, D>
where
N: Numerator<D>,
D: Denominator,
{
}
impl<N, D> Default for Q<N, D>
where
N: Numerator<D>,
D: Denominator,
{
fn default() -> Self {
Self::new()
}
}
mod rational;
pub use rational::Rational;
mod ops;
pub use ops::Recip;
mod cmp;
pub use cmp::{Cmp, Ordering};
mod fmt;
#[cfg(test)]
mod tests {
use super::*;
use char_buf::CharBuf;
use core::fmt::Write;
macro_rules! format {
($($arg:tt)*) => {
{
let mut w = CharBuf::<1024>::new();
write!(w, $($arg)*).unwrap();
w
}
};
}
#[test]
fn test_q() {
let a = Q::<P1, P2>::default();
let b = a.clone();
let c: PhantomData<(P1, P2)> = PhantomData;
let d = Q::<P1, P2>::borrow();
assert_eq!(a, b);
assert_eq!(&a, d);
assert_eq!(format!("{:?}", a), format!("Q({:?})", c));
}
}