Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.