Last active
August 29, 2015 14:03
-
-
Save bvssvni/fed140df257f1f629a19 to your computer and use it in GitHub Desktop.
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
src/main.rs:108:17: 108:18 error: `d` does not live long enough | |
src/main.rs:108 let d = d.trans(10.0, 10.0); | |
^ | |
src/main.rs:104:11: 117:2 note: reference must be valid for the block at 104:10... | |
src/main.rs:104 fn main() { | |
src/main.rs:105 let c = Context::new(); | |
src/main.rs:106 { | |
src/main.rs:107 let d = c.trans(20.0, 40.0); | |
src/main.rs:108 let d = d.trans(10.0, 10.0); | |
src/main.rs:109 let transform = d.transform.get(); | |
... | |
src/main.rs:106:5: 112:6 note: ...but borrowed value is only valid for the block at 106:4 | |
src/main.rs:106 { | |
src/main.rs:107 let d = c.trans(20.0, 40.0); | |
src/main.rs:108 let d = d.trans(10.0, 10.0); | |
src/main.rs:109 let transform = d.transform.get(); | |
src/main.rs:110 assert_eq!(transform[2], 30.0); | |
src/main.rs:111 assert_eq!(transform[5], 50.0); | |
... | |
error: aborting due to previous error | |
make: *** [bin/main] Error 101 |
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
pub type Matrix2d = [f64, ..6]; | |
pub enum Field<'a, T> { | |
Value(T), | |
Borrowed(&'a T), | |
} | |
impl<'a, T> Field<'a, T> { | |
pub fn get(&'a self) -> &'a T { | |
match *self { | |
Value(ref val) => val, | |
Borrowed(rval) => rval, | |
} | |
} | |
} | |
pub struct Context<'a> { | |
pub view: Field<'a, Matrix2d>, | |
pub transform: Field<'a, Matrix2d>, | |
} | |
impl<'a> | |
Clone | |
for Context<'a> { | |
fn clone(&self) -> Context<'static> { | |
Context { | |
view: Value(*self.view.get()), | |
transform: Value(*self.transform.get()), | |
} | |
} | |
} | |
impl<'a> | |
Context<'a> { | |
pub fn new() -> Context { | |
Context { | |
view: Value( | |
[1.0, 0.0, 0.0, | |
0.0, 1.0, 0.0] | |
), | |
transform: Value( | |
[1.0, 0.0, 0.0, | |
0.0, 1.0, 0.0] | |
), | |
} | |
} | |
} | |
pub trait RelativeTransform2d<'a> { | |
fn trans(&'a self, x: f64, y: f64) -> Self; | |
} | |
/* | |
// WORKS WHEN IMPLEMENTING TRAIT DIRECTLY | |
impl<'a> RelativeTransform2d<'a> for Context<'a> { | |
fn trans(&'a self, x: f64, y: f64) -> Context<'a> { | |
Context { | |
view: Borrowed(self.view.get()), | |
transform: Value( | |
[1.0, 0.0, x, | |
0.0, 1.0, y] | |
), | |
} | |
} | |
} | |
*/ | |
pub trait CanTransform<'a, T, U> { | |
fn transform(&'a self, value: U) -> T; | |
} | |
impl<'a> | |
CanTransform<'a, Context<'a>, Matrix2d> | |
for Context<'a> { | |
fn transform(&'a self, value: Matrix2d) -> Context<'a> { | |
Context { | |
view: Borrowed(self.view.get()), | |
transform: Value(value), | |
} | |
} | |
} | |
impl<'a, T: CanTransform<'a, T, Matrix2d> | |
> RelativeTransform2d<'a> for T { | |
fn trans(&'a self, x: f64, y: f64) -> T { | |
let trans = [1.0, 0.0, x, | |
0.0, 1.0, y]; | |
self.transform(trans) | |
} | |
} | |
fn main() { | |
let c = Context::new(); | |
{ | |
let d = c.trans(20.0, 40.0); | |
let d = d.trans(10.0, 10.0); | |
let transform = d.transform.get(); | |
assert_eq!(transform[2], 30.0); | |
assert_eq!(transform[5], 50.0); | |
} | |
let transform = c.transform.get(); | |
assert_eq!(transform[2], 0.0); | |
assert_eq!(transform[5], 0.0); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment