Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an extra method to (Checked)Euclid trait to get both quotien and rem #291

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/ops/euclid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ pub trait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = Self> {
/// assert_eq!(Euclid::rem_euclid(&-a, &-b), 1);
/// ```
fn rem_euclid(&self, v: &Self) -> Self;

/// Returns both the euclidian division quotien and remainer
cuviper marked this conversation as resolved.
Show resolved Hide resolved
///
/// By default, it internaly calls both `Euclid::div_euclid` and `Euclid::rem_euclid`,
/// but it can be overwritten in order to implement some optimization.
cuviper marked this conversation as resolved.
Show resolved Hide resolved
fn div_with_rem_euclid(&self, v: &Self) -> (Self, Self) {
(self.div_euclid(v), self.rem_euclid(v))
}
}

macro_rules! euclid_forward_impl {
Expand Down Expand Up @@ -174,6 +182,14 @@ pub trait CheckedEuclid: Euclid {
/// Finds the euclid remainder of dividing two numbers, checking for underflow, overflow and
/// division by zero. If any of that happens, `None` is returned.
fn checked_rem_euclid(&self, v: &Self) -> Option<Self>;

/// Returns both the checked euclidian division quotien and remainer
cuviper marked this conversation as resolved.
Show resolved Hide resolved
///
/// By default, it internaly calls both `Euclid::div_euclid` and `Euclid::rem_euclid`,
/// but it can be overwritten in order to implement some optimization.
cuviper marked this conversation as resolved.
Show resolved Hide resolved
fn checked_div_with_rem_euclid(&self, v: &Self) -> Option<(Self, Self)> {
Some((self.checked_div_euclid(v)?, self.checked_rem_euclid(v)?))
}
}

macro_rules! checked_euclid_forward_impl {
Expand Down