Skip to content

Commit

Permalink
Avoid clone in sign normalization too
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jun 23, 2022
1 parent 3d80360 commit 8506da1
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,24 @@ impl<T: Clone + Integer> Ratio<T> {
let g: T = self.numer.gcd(&self.denom);

// FIXME(#5992): assignment operator overloads
// self.numer /= g;
// T: Clone + Integer != T: Clone + NumAssign
let numer = std::mem::replace(&mut self.numer, T::zero());
self.numer = numer / g.clone();
// self.numer = self.numer.clone() / g.clone();

// FIXME(#5992): assignment operator overloads
#[inline]
fn replace_with<T: Zero>(x: &mut T, f: impl FnOnce(T) -> T) {
let y = core::mem::replace(x, T::zero());
*x = f(y);
}

// self.numer /= g;
replace_with(&mut self.numer, |x| x / g.clone());

// self.denom /= g;
// T: Clone + Integer != T: Clone + NumAssign
let denom = std::mem::replace(&mut self.denom, T::zero());
self.denom = denom / g;
// self.denom = self.denom.clone() / g;
replace_with(&mut self.denom, |x| x / g);

// keep denom positive!
if self.denom < T::zero() {
self.numer = T::zero() - self.numer.clone();
self.denom = T::zero() - self.denom.clone();
replace_with(&mut self.numer, |x| T::zero() - x);
replace_with(&mut self.denom, |x| T::zero() - x);
}
}

Expand Down

0 comments on commit 8506da1

Please sign in to comment.