2019-01-25 23:35:16 +00:00
|
|
|
//! Test that you can implement a query using a `dyn Trait` setup.
|
|
|
|
|
|
|
|
#[salsa::database(DynTraitStorage)]
|
|
|
|
#[derive(Default)]
|
|
|
|
struct DynTraitDatabase {
|
|
|
|
runtime: salsa::Runtime<DynTraitDatabase>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl salsa::Database for DynTraitDatabase {
|
2019-09-27 09:49:15 +00:00
|
|
|
fn salsa_runtime(&self) -> &salsa::Runtime<Self> {
|
2019-01-25 23:35:16 +00:00
|
|
|
&self.runtime
|
|
|
|
}
|
2019-09-27 09:49:15 +00:00
|
|
|
|
|
|
|
fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> {
|
|
|
|
&mut self.runtime
|
|
|
|
}
|
2019-01-25 23:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::query_group(DynTraitStorage)]
|
|
|
|
trait DynTrait {
|
|
|
|
#[salsa::input]
|
|
|
|
fn input(&self, x: u32) -> u32;
|
|
|
|
|
|
|
|
fn output(&self, x: u32) -> u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn output(db: &dyn DynTrait, x: u32) -> u32 {
|
|
|
|
db.input(x) * 2
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dyn_trait() {
|
|
|
|
let mut query = DynTraitDatabase::default();
|
|
|
|
query.set_input(22, 23);
|
|
|
|
assert_eq!(query.output(22), 46);
|
|
|
|
}
|