Hi, I've an issue with a definition for a lifetime for a trait... supposing that Extern
is an external not modifiable struct (with all related functions), this is the example code generated for a minimal sample in Playground here.
Actual code:
use anyhow::Error;
struct Extern {}
impl Extern {
fn something<T, E, F>(f: F) -> Result<T, E>
where
F: FnOnce(&mut Self) -> Result<T, E>,
E: From<Error>,
{
f(&mut Extern {})
}
}
trait TestInterface<'a> {
fn new(test: &'a Test, test_mut_reference: &'a mut Extern) -> Self;
fn something<S, T, E, F>(&mut self, f: F) -> Result<T, E>
where
F: FnOnce(S) -> Result<T, E>,
E: From<Error>,
for<'b> S: TestInterface<'b>;
}
struct Test {}
impl<'a> TestInterface<'a> for Test {
fn new(_test: &'a Test, _test_mut_reference: &'a mut Extern) -> Self {
Self {}
}
fn something<S, T, E, F>(&mut self, f: F) -> Result<T, E>
where
F: FnOnce(S) -> Result<T, E>,
E: From<Error>,
for<'b> S: TestInterface<'b>,
{
Extern::something::<T, E, _>(|t| f(S::new(self, t)))
}
}
struct TestWithTest<'a> {
test: &'a Test,
test_mut_reference: &'a mut Extern,
}
impl<'a> TestInterface<'a> for TestWithTest<'a> {
fn new(test: &'a Test, test_mut_reference: &'a mut Extern) -> Self {
Self {
test,
test_mut_reference
}
}
fn something<S, T, E, F>(&mut self, f: F) -> Result<T, E>
where
F: FnOnce(S) -> Result<T, E>,
E: From<Error>,
for<'b> S: TestInterface<'b>,
{
f(S::new(self.test, self.test_mut_reference))
}
}
fn main() {
let mut test = Test {};
let _: Result<_, Error> = test.something(|_: TestWithTest| Ok(()));
}
Could someone help me understanding what the compiler is trying to say and how to fix this error?
Output error is:
error: implementation of `TestInterface` is not general enough
--> src/main.rs:68:31
|
68 | let _: Result<_, Error> = test.something(|_: TestWithTest| Ok(()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `TestInterface` is not general enough
|
= note: `TestInterface<'0>` would have to be implemented for the type `TestWithTest<'_>`, for any lifetime `'0`...
= note: ...but `TestInterface<'1>` is actually implemented for the type `TestWithTest<'1>`, for some specific lifetime `'1`
error: could not compile `playground` due to previous error
Thanks in advance