Hi, I'm trying to implement a trait in my code:
pub trait AtomicDb {
type State;
type Mutation;
type Error;
fn get_state(&self) -> &Self::State;
fn mutate(&mut self, mutations: Vec<Self::Mutation>) -> Result<(), Self::Error>;
}
As follows:
struct MockDb<A: Clone> {
state: FunderState<A>,
}
impl<A: Clone + 'static> AtomicDb for MockDb<A> {
type State = FunderState<A>;
type Mutation = FunderMutation<A>;
type Error = ();
fn get_state(&self) -> &FunderState<A> {
&self.state
}
fn mutate(&mut self, mutations: Vec<FunderMutation<A>>) -> Result<(), ()> {
for mutation in mutations {
self.state.mutate(&mutation);
}
Ok(())
}
}
This code currently compiles, but I think that the A: 'static requirement could be relaxed. I tried to change it to something else, like this:
impl<'a, A: Clone + 'a> AtomicDb for MockDb<A> {
type State = FunderState<A>;
type Mutation = FunderMutation<A>;
type Error = ();
fn get_state(&'a self) -> &'a FunderState<A> {
&self.state
}
fn mutate(&'a mut self, mutations: Vec<FunderMutation<A>>) -> Result<(), ()> {
for mutation in mutations {
self.state.mutate(&mutation);
}
Ok(())
}
}
And I got the following error messages:
error[E0308]: method not compatible with trait
--> components/funder/src/tests.rs:113:5
|
113 | fn get_state(&'a self) -> &'a FunderState<A> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `fn(&tests::MockDb<A>) -> &state::FunderState<A>`
found type `fn(&'a tests::MockDb<A>) -> &'a state::FunderState<A>`
note: the lifetime 'a as defined on the impl at 108:6...
--> components/funder/src/tests.rs:108:6
|
108 | impl<'a, A: Clone + 'a> AtomicDb for MockDb<A> {
| ^^
note: ...does not necessarily outlive the anonymous lifetime #1 defined on the method body at 113:5
--> components/funder/src/tests.rs:113:5
|
113 | / fn get_state(&'a self) -> &'a FunderState<A> {
114 | | &self.state
115 | | }
| |_____^
error[E0308]: method not compatible with trait
--> components/funder/src/tests.rs:117:5
|
117 | fn mutate(&'a mut self, mutations: Vec<FunderMutation<A>>) -> Result<(), ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `fn(&mut tests::MockDb<A>, std::vec::Vec<state::FunderMutation<A>>) -> std::result::Result<(), ()>`
found type `fn(&'a mut tests::MockDb<A>, std::vec::Vec<state::FunderMutation<A>>) -> std::result::Result<(), ()>`
note: the anonymous lifetime #1 defined on the method body at 117:5...
--> components/funder/src/tests.rs:117:5
|
117 | / fn mutate(&'a mut self, mutations: Vec<FunderMutation<A>>) -> Result<(), ()> {
118 | | for mutation in mutations {
119 | | self.state.mutate(&mutation);
120 | | }
121 | | Ok(())
122 | | }
| |_____^
note: ...does not necessarily outlive the lifetime 'a as defined on the impl at 108:6
--> components/funder/src/tests.rs:108:6
|
108 | impl<'a, A: Clone + 'a> AtomicDb for MockDb<A> {
What would be the correct way to relax the 'static requirement?