Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Created September 15, 2016 19:18
Show Gist options
  • Save kindlychung/0657b6f826d8652ce219d9e436fcfd73 to your computer and use it in GitHub Desktop.
Save kindlychung/0657b6f826d8652ce219d9e436fcfd73 to your computer and use it in GitHub Desktop.
Compiling rustcallc v0.1.0 (file:///Users/kaiyin/Documents/workspace-eclipse-neon/rustcallc)
error[E0369]: binary operation `+` cannot be applied to type `<T as std::ops::Rem>::Output`
--> src/main.rs:24:9
|
24 | (*self % n) + n
| ^^^^^^^^^^^
|
note: an implementation of `std::ops::Add` might be missing for `<T as std::ops::Rem>::Output`
--> src/main.rs:24:9
|
24 | (*self % n) + n
| ^^^^^^^^^^^
error[E0308]: mismatched types
--> src/main.rs:45:31
|
45 | Mod::new(self.modulo, self.i + other.i)
| ^^^^^^^^^^^^^^^^ expected type parameter, found associated type
|
= note: expected type `T`
= note: found type `<T as std::ops::Add>::Output`
error[E0308]: mismatched types
--> src/main.rs:54:31
|
54 | Mod::new(self.modulo, self.i - other.i)
| ^^^^^^^^^^^^^^^^ expected type parameter, found associated type
|
= note: expected type `T`
= note: found type `<T as std::ops::Sub>::Output`
error[E0308]: mismatched types
--> src/main.rs:63:31
|
63 | Mod::new(self.modulo, self.i * other.i)
| ^^^^^^^^^^^^^^^^ expected type parameter, found associated type
|
= note: expected type `T`
= note: found type `<T as std::ops::Mul>::Output`
error: aborting due to 4 previous errors
error: Could not compile `rustcallc`.
To learn more, run the command again with --verbose.
use std::ops::Add;
use std::ops::Mul;
use std::ops::Sub;
use std::ops::Rem;
struct Mod<T>
where T: Modulo<T> + Mul<T> + Sub<T> + Add<T> + Rem<T>
{
modulo: T,
i: T,
}
trait Modulo<T>
where T: Add<T> + Rem<T>
{
fn modulo(&self, n: T) -> T;
}
impl<T> Modulo<T> for T
where T: Add<T> + Rem<T>
{
fn modulo(&self, n: T) -> T {
(*self % n) + n
}
}
impl<T> Mod<T>
where T: Modulo<T> + Mul<T> + Sub<T> + Add<T> + Rem<T>
{
fn new(modulo: T, i: T) -> Mod<T> {
let n = i.modulo(modulo);
Mod {
modulo: modulo,
i: n,
}
}
}
impl<T> Add for Mod<T>
where T: Modulo<T> + Mul<T> + Sub<T> + Add<T> + Rem<T>
{
type Output = Mod<T>;
fn add(self, other: Mod<T>) -> Mod<T> {
Mod::new(self.modulo, self.i + other.i)
}
}
impl<T> Sub for Mod<T>
where T: Modulo<T> + Mul<T> + Sub<T> + Add<T> + Rem<T>
{
type Output = Mod<T>;
fn sub(self, other: Mod<T>) -> Mod<T> {
Mod::new(self.modulo, self.i - other.i)
}
}
impl<T> Mul for Mod<T>
where T: Modulo<T> + Mul<T> + Sub<T> + Add<T> + Rem<T>
{
type Output = Mod<T>;
fn mul(self, other: Mod<T>) -> Mod<T> {
Mod::new(self.modulo, self.i * other.i)
}
}
fn main() {
let x = Mod::new(-5, 3);
let y = Mod::new(-5, 8);
println!("{}", (x + y).i);
let x = Mod::new(-5, 3);
let y = Mod::new(-5, 8);
println!("{}", (x - y).i);
let x = Mod::new(-5, 3);
let y = Mod::new(-5, 8);
println!("{}", (x * y).i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment