diff options
author | Andreas Grois <soulsource@users.noreply.github.com> | 2022-11-04 21:59:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-04 21:59:01 +0100 |
commit | 3254d705f91b9440457c1b9322c3a3edfd4b217e (patch) | |
tree | 73d80a5875523dee4a0bd98ebcb3b6f9bb0603bf /src/passwordmaker/base_conversion/remainders.rs | |
parent | daa045ab422062692705ff63c1a5618c9bef9801 (diff) | |
parent | c385c90148178ad38b17491d45f5dcc2cbe06c9c (diff) |
Merge pull request #1 from soulsource/feature/heap-allocation-free-base-conversion
Feature/heap allocation free base conversion
While skipping the heap allocation is nice, the real gain is the option to early-out of password generation once the desired length has been reached.
Diffstat (limited to 'src/passwordmaker/base_conversion/remainders.rs')
-rw-r--r-- | src/passwordmaker/base_conversion/remainders.rs | 74 |
1 files changed, 0 insertions, 74 deletions
diff --git a/src/passwordmaker/base_conversion/remainders.rs b/src/passwordmaker/base_conversion/remainders.rs deleted file mode 100644 index 93570a1..0000000 --- a/src/passwordmaker/base_conversion/remainders.rs +++ /dev/null @@ -1,74 +0,0 @@ -/// Adds `calc_remainders(divisor)` method to types that have some implementation of the Division trait. -pub(super) trait CalcRemainders<T, D>{ - fn calc_remainders(self, divisor : D) -> Remainders<T,D>; -} - -/// Implement `Division` to enable the `calc_remainders()` method for your type. -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<T, D> CalcRemainders<T, D> for T - where T:Division<D> -{ - fn calc_remainders(self, divisor : D) -> Remainders<T, D> { - Remainders::new(self,divisor) - } -} - -pub(super) struct Remainders<T, U>{ - value : Option<T>, - divisor : U, -} - -impl<U, T:Division<U>> Remainders<T, U> { - fn new(value : T, divisor : U) -> Self { - let value = if value.is_zero() { None } else { Some(value) }; - Remainders { - value, - divisor, - } - } -} - -impl<U, T:Division<U>> Iterator for Remainders<T,U>{ - type Item=U; - - fn next(&mut self) -> Option<Self::Item> { - if let Some(v) = self.value.take() { - let DivisionResult{result, remainder} = v.divide(&self.divisor); - self.value = if result.is_zero() { None } else { Some(result) }; - Some(remainder) - } else { - None - } - } -} - -pub(super) struct DivisionResult<T:Division<U>, U> { - pub result : T, - pub remainder : U, -} - -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() - } -}
\ No newline at end of file |