I want to implement a modulo operation for most Rem
types in Rust:
#![feature(specialization)]
use std::ops::{Add, Rem};
/// Define a modulo operation, in the mathematical sense.
/// This differs from Rem because the result is always non-negative.
pub trait Modulo<T> {
type Output;
#[inline]
fn modulo(self, other: T) -> Self::Output;
}
/// Implement modulo operation for types that implement Rem, Add and Clone.
// Add and Clone are needed to shift the value by U if it is below zero.
impl<U, T> Modulo<T> for U
where
T: Clone,
U: Rem<T>,
<U as Rem<T>>::Output: Add<T>,
<<U as Rem<T>>::Output as Add<T>>::Output: Rem<T>
{
default type Output = <<<U as Rem<T>>::Output as Add<T>>::Output as Rem<T>>::Output;
#[inline]
default fn modulo(self, other: T) -> Self::Output {
((self % other.clone()) + other.clone()) % other
}
}
This compiles fine without the default
s, but with the default
, I get
error[E0308]: mismatched types
--> main.rs:
|
| default fn modulo(self, other: T) -> Self::Output {
| ------------ expected `<U as Modulo<T>>::Output` because of return type
| ((self % other.clone()) + other.clone()) % other
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected Modulo::Output, found std::ops::Rem::Output
|
= note: expected type `<U as Modulo<T>>::Output`
found type `<<<U as std::ops::Rem<T>>::Output as std::ops::Add<T>>::Output as std::ops::Rem<T>>::Output`
I don't understand why that would happen. I need the default
s because I want to specialize it for Copy
types.
I'm using Rust 1.29.0-nightly.