Skip to content

Instantly share code, notes, and snippets.

@SiegeLord
Created June 3, 2014 23:05
Show Gist options
  • Save SiegeLord/6bb8ba3d50b4926aa027 to your computer and use it in GitHub Desktop.
Save SiegeLord/6bb8ba3d50b4926aa027 to your computer and use it in GitHub Desktop.
Auto-ref woes
#[deriving(Show)]
struct Matrix
{
data: Vec<f32>
}
trait ToMat
{
fn to_mat(self) -> Matrix;
}
impl<'l> ToMat for &'l Matrix
{
fn to_mat(self) -> Matrix
{
println!("Clone");
Matrix{ data: self.data.clone() }
}
}
impl ToMat for Matrix
{
fn to_mat(self) -> Matrix
{
self
}
}
impl
Mul<f32, Matrix>
for Matrix
{
fn mul(&self, f: &f32) -> Matrix
{
let mut v2 = self.to_mat();
for v in v2.data.mut_iter()
{
*v *= *f;
}
v2
}
}
impl<'l>
Mul<f32, Matrix>
for &'l Matrix
{
fn mul(&self, f: &f32) -> Matrix
{
let mut v2 = self.to_mat();
for v in v2.data.mut_iter()
{
*v *= *f;
}
v2
}
}
fn main()
{
let m = Matrix{ data: vec![1.0f32, 2.0, 3.0] };
let m2 = m * 2.0 * 5.0 * 3.0;
println!("{}", m2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment