diff options
author | Andreas Grois <andi@grois.info> | 2022-10-18 07:56:16 +0200 |
---|---|---|
committer | Andreas Grois <andi@grois.info> | 2022-10-18 07:56:16 +0200 |
commit | b1f4d48e956c6b6599b666248e5aa157b9660dca (patch) | |
tree | 84b334f4800cc4c6f77a303c7a59956b4c9d41a1 /src/passwordmaker/base_conversion/division.rs | |
parent | 61669cc48f3b460cab05b9b51f87a0cd08d51302 (diff) |
First draft of (untested) iterative conversion.
Diffstat (limited to 'src/passwordmaker/base_conversion/division.rs')
-rw-r--r-- | src/passwordmaker/base_conversion/division.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/passwordmaker/base_conversion/division.rs b/src/passwordmaker/base_conversion/division.rs new file mode 100644 index 0000000..c6fc911 --- /dev/null +++ b/src/passwordmaker/base_conversion/division.rs @@ -0,0 +1,32 @@ +/// A trait that combines std::ops::Div and std::ops::Rem, as those can often be computed together. +pub(super) trait Division<D> where Self:Sized { + /// does in-place arbitrary-length division. Returns remainder. + fn divide(self, divisor : &D) -> DivisionResult<Self, D>; + fn is_zero(&self) -> bool; +} + +/// Or mark your type as `UseGenericDivision` to just use `/` and `%` operators for types. Makes only sense for integers. +pub(super) trait UseGenericDivision : Clone + + for <'a> std::ops::Div<&'a Self, Output = Self> + + for <'a> std::ops::Rem<&'a Self, Output = Self> + + Default + + Eq {} + + impl<U> Division<U> for U + where U: UseGenericDivision +{ + fn divide(self, divisor : &Self) -> DivisionResult<Self, Self> { + DivisionResult { + result: self.clone().div(divisor), + remainder: self.rem(divisor) + } + } + fn is_zero(&self) -> bool { + *self == Self::default() + } +} + +pub(super) struct DivisionResult<T, U> { + pub result : T, + pub remainder : U, +}
\ No newline at end of file |