Last active
November 28, 2016 16:51
-
-
Save skade/c61dfcf5ca7f0ab43865db289c0b349c 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
trait Query<'a, 'b: 'a, X: 'b, Y: 'a> { | |
fn execute(&self, f: X) -> Y; | |
} | |
#[derive(Default)] | |
struct ToBeQueried { | |
inner: String | |
} | |
struct GetInnerSlice; | |
impl<'a, 'b: 'a> Query<'a, 'b, &'b ToBeQueried, &'a str> for GetInnerSlice { | |
fn execute(&self, f: &'b ToBeQueried) -> &'a str { | |
f.inner.as_ref() | |
} | |
} | |
struct QueryWrapper<Q> { | |
query: Q | |
} | |
impl<Q> QueryWrapper<Q> { | |
fn new(query: Q) -> Self { | |
QueryWrapper { query: query } | |
} | |
} | |
trait ExecuteQuery<'a, 'b: 'a, X: 'b, Y: 'a, Q: Query<'a, 'b, X, Y>> { | |
fn query(&self) -> &Q; | |
fn execute_query(&self, o: X) -> Y { | |
self.query().execute(o) | |
} | |
} | |
impl<'a, 'b: 'a, X: 'b, Y: 'a, Q: Query<'a, 'b, X, Y>> ExecuteQuery<'a, 'b, X, Y, Q> for QueryWrapper<Q> { | |
fn query(&self) -> &Q { | |
&self.query | |
} | |
} | |
fn apply_query<'a, 'b: 'a>(a: &'b ToBeQueried) -> &'a str { | |
let wrapper = QueryWrapper::new(GetInnerSlice); | |
wrapper.execute_query(a) | |
} | |
fn main() { | |
let mut string = ToBeQueried::default(); | |
string.inner = "foo".to_string(); | |
println!("{}", apply_query(&string)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment