Created
June 3, 2014 23:05
-
-
Save SiegeLord/6bb8ba3d50b4926aa027 to your computer and use it in GitHub Desktop.
Auto-ref woes
This file contains hidden or 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
#[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