Skip to content

Instantly share code, notes, and snippets.

@aib
Created February 13, 2021 14:18
Show Gist options
  • Save aib/09ac4f608a609c14412f3ac599115036 to your computer and use it in GitHub Desktop.
Save aib/09ac4f608a609c14412f3ac599115036 to your computer and use it in GitHub Desktop.
Bad trait suggestion of rustc 1.47.0
#[derive(Debug)]
#[derive(Copy, Clone)]
pub struct Vec3<T> {
pub x: T,
pub y: T,
pub z: T
}
impl<T> Vec3<T> {
pub fn new(x:T, y:T, z:T) -> Vec3<T> {
Vec3 { x: x, y: y, z: z }
}
}
impl <T: std::ops::Add<Output = T> + Copy> std::ops::Add<Vec3<T>> for Vec3<T> {
type Output = Vec3<T>;
fn add(self, rhs:Vec3<T>) -> Self::Output {
Vec3 { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z }
}
}
impl <T: std::ops::Mul<Output = T> + Copy> std::ops::Mul<T> for Vec3<T> {
type Output = Vec3<T>;
fn mul(self, rhs:T) -> Self::Output {
Vec3 { x: self.x * rhs, y: self.y * rhs, z: self.z * rhs }
}
}
pub fn travel <T: std::ops::Mul<Output = T>> (dir:Vec3<T>, t:T) -> Vec3<T> {
dir * t
}
fn main() {
println!("{:?}", travel(Vec3::new(1., 2., 3.), 15.));
}
/*
error[E0369]: cannot multiply `T` to `Vec3<T>`
--> src/main.rs:30:6
|
30 | dir * t
| --- ^ - T
| |
| Vec3<T>
|
help: consider further restricting this bound
|
29 | pub fn travel <T: std::ops::Mul<Output = T> + std::ops::Mul<Output = T>> (dir:Vec3<T>, t:T) -> Vec3<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment