dashu_int/modular/
fmt.rs

1//! Formatting modular rings and modular numbers.
2
3use super::repr::Reduced;
4use core::fmt::{self, Binary, Debug, Display, Formatter, LowerHex, Octal, UpperHex};
5
6macro_rules! impl_fmt_for_modulo {
7    ($t:ident) => {
8        impl $t for Reduced<'_> {
9            fn fmt(&self, f: &mut Formatter) -> fmt::Result {
10                $t::fmt(&self.residue(), f)?;
11                f.write_str(" (mod ")?;
12                $t::fmt(&self.modulus(), f)?;
13                f.write_str(")")
14            }
15        }
16    };
17}
18
19impl_fmt_for_modulo!(Display);
20impl_fmt_for_modulo!(Binary);
21impl_fmt_for_modulo!(Octal);
22impl_fmt_for_modulo!(LowerHex);
23impl_fmt_for_modulo!(UpperHex);
24
25impl Debug for Reduced<'_> {
26    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
27        let residue = self.residue();
28        let modulus = self.modulus();
29        if f.alternate() {
30            f.debug_struct("Reduced")
31                .field("residue", &residue)
32                .field("modulus", &modulus)
33                .finish()
34        } else {
35            Debug::fmt(&residue, f)?;
36            f.write_str(" (mod ")?;
37            Debug::fmt(&self.modulus(), f)?;
38            f.write_str(")")
39        }
40    }
41}