This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I've just refactored a bunch of my code, changing 2 files | |
$ run tests | |
# one test is broken, time to isolate | |
$ git add . && git commit -m WIP # commit my files | |
$ WIP=$(git rev-parse --short HEAD) # save the commit hash | |
$ git reset --hard HEAD~1 # reset my branch to the last known working state | |
$ git checkout $WIP -- file_1 # see if this file cause the issue | |
$ run tests | |
# tests pass, must be a different file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::{Add, Div, Mul, Neg, Sub}; | |
struct Tuple(i32, i32, i32, i32); | |
impl Neg for Tuple { | |
type Output = Self; | |
fn neg(self) -> Self::Output { | |
let Self(x, y, z, w) = self; | |
Self(-x, -y, -z, -w) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::{Add, Div, Mul, Neg, Sub}; | |
struct Tuple<T>(T, T, T, T); | |
impl<T: Neg<Output = T>> Neg for Tuple<T> { | |
type Output = Self; | |
fn neg(self) -> Self::Output { | |
let Self(x, y, z, w) = self; | |
Self(-x, -y, -z, -w) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Vector<T> { | |
tuple: Tuple<T>, | |
} | |
struct Point<T> { | |
tuple: Tuple<T>, | |
} | |
impl<T: From<u8>> Vector<T> { | |
fn new(x: T, y: T, z: T) -> Self { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl<T: Mul<Output = T> + Copy> Mul<T> for Tuple<T> { | |
type Output = Self; | |
fn mul(self, rhs: T) -> Self::Output { | |
let Self(x, y, z, w) = self; | |
Self(x * rhs, y * rhs, z * rhs, w * rhs) | |
} | |
} | |
impl<T: Div<Output = T> + Copy> Div<T> for Tuple<T> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl<T: Mul<Output = T> + Copy> Mul<T> for Vector<T> { | |
type Output = Self; | |
fn mul(self, rhs: T) -> Self::Output { | |
Self { | |
tuple: self.tuple * rhs, | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Vector<f64> { | |
fn magnitude(&self) -> f64 { | |
let Tuple(x, y, z, _) = self.tuple; | |
(x.powi(2) + y.powi(2) + z.powi(2)).sqrt() | |
} | |
fn norm(&self) -> Self { | |
*self * (1.0 / self.magnitude()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Neg<Output = T> + From<u8> + Copy> Vector<T> { | |
fn dot(&self, rhs: Self) -> T { | |
let (Tuple(x1, y1, z1, _), Tuple(x2, y2, z2, _)) = (self.tuple, rhs.tuple); | |
(x1 * x2) + (y1 * y1) + (z1 * z2) | |
} | |
fn cross(&self, rhs: Self) -> Self { | |
let (Tuple(x1, y1, z1, _), Tuple(x2, y2, z2, _)) = (self.tuple, rhs.tuple); | |
Self::new( | |
(y1 * z2) - (z1 * y2), |