| 1 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result#, Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#}; |
| 2 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#; |
| 3 | |
| 4 | pub struct Superscriptrust-analyzer cargo mendeleev 0.8.1 superscript/Superscript#(u32); |
| 5 | |
| 6 | impl Superscriptrust-analyzer cargo mendeleev 0.8.1 superscript/Superscript# { |
| 7 | pub fn newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Superscript]new().(number: u32) -> Selfrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Superscript] { |
| 8 | Selfrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Superscript](number) |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Superscriptrust-analyzer cargo mendeleev 0.8.1 superscript/Superscript# { |
| 13 | fn fmtrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Superscript][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 14 | let Selfrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Superscript][Display](number) = self; |
| 15 | for digit in Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().number) { |
| 16 | let c = to_superscript_utf8rust-analyzer cargo mendeleev 0.8.1 superscript/to_superscript_utf8().(digit).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 17 | f.write_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'_>`][Write]write_char().(c)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 18 | } |
| 19 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | fn to_superscript_utf8rust-analyzer cargo mendeleev 0.8.1 superscript/to_superscript_utf8().(digit: u32) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<char> { |
| 24 | match digit { |
| 25 | 0 => '⁰', |
| 26 | 1 => '¹', |
| 27 | 2 => '²', |
| 28 | 3 => '³', |
| 29 | 4 => '⁴', |
| 30 | 5 => '⁵', |
| 31 | 6 => '⁶', |
| 32 | 7 => '⁷', |
| 33 | 8 => '⁸', |
| 34 | 9 => '⁹', |
| 35 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()., |
| 36 | } |
| 37 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 38 | } |
| 39 | |
| 40 | struct Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits# { |
| 41 | numberrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#number.: u32, |
| 42 | dividerrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#divider.: u32, |
| 43 | radixrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#radix.: u32, |
| 44 | } |
| 45 | |
| 46 | impl Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits# { |
| 47 | fn newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(number: u32) -> Selfrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits] { |
| 48 | let radix = 10; |
| 49 | let mut divider = 1; |
| 50 | while divider rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le().number { |
| 51 | divider = divider.saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]saturating_mul().(radix); |
| 52 | } |
| 53 | divider = divider.saturating_divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]saturating_div().(radix).maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#max().(1); |
| 54 | Selfrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits] { |
| 55 | numberrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#number., |
| 56 | dividerrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#divider., |
| 57 | radixrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#radix., |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits# { |
| 63 | type Itemrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits][Iterator]Item# = u32; |
| 64 | |
| 65 | fn nextrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 66 | let digit = self.numberrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#number..checked_divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]checked_div().(self.dividerrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#divider.); |
| 67 | self.numberrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#number. = self.numberrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#number..checked_remrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]checked_rem().(self.dividerrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#divider.).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0); |
| 68 | self.dividerrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#divider. = self.dividerrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#divider..saturating_divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]saturating_div().(self.radixrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#radix.); |
| 69 | digit |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #[cfg(test)] |
| 74 | mod testsrust-analyzer cargo mendeleev 0.8.1 superscript/tests/ { |
| 75 | use superrust-analyzer cargo mendeleev 0.8.1 superscript/::*; |
| 76 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#; |
| 77 | |
| 78 | macro_rules! comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare! { |
| 79 | ($digits:expr, $expected:expr) => { |
| 80 | let mut digits = $digits; |
| 81 | let digits = core::array::from_fn(|_| digits.next().unwrap()); |
| 82 | assert_eq!(digits, $expected); |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 87 | fn iterate_digitsrust-analyzer cargo mendeleev 0.8.1 superscript/tests/iterate_digits().() { |
| 88 | comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare!!(Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(0), [0]); |
| 89 | comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare!!(Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(1), [1]); |
| 90 | comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare!!(Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(5), [5]); |
| 91 | comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare!!(Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(10), [1, 0]); |
| 92 | comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare!!(Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(99), [9, 9]); |
| 93 | comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare!!(Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(100), [1, 0, 0]); |
| 94 | comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare!!(Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(101), [1, 0, 1]); |
| 95 | comparerust-analyzer cargo mendeleev 0.8.1 superscript/tests/compare!!(Digitsrust-analyzer cargo mendeleev 0.8.1 superscript/Digits#::newrust-analyzer cargo mendeleev 0.8.1 superscript/impl#[Digits]new().(1234), [1, 2, 3, 4]); |
| 96 | } |
| 97 | } |