Hi, I have a problem that reduces to this (playground):
struct S<'a> {
data: &'a i32,
}
struct S1<'a> {
inner: S<'a>,
}
struct S2<'a> {
inner: S<'a>,
}
impl<'a> From<S<'a>> for S1<'a> {
fn from(inner: S<'a>) -> Self {
Self{inner}
}
}
impl<'a> From<S<'a>> for S2<'a> {
fn from(inner: S<'a>) -> Self {
Self{inner}
}
}
fn run<T>(func: impl Fn(T))
where
for<'a> T: From<S<'a>>
{
}
fn work_with_s1<'a>(_s: S1<'a>) { }
fn work_with_s2<'a>(_s: S2<'a>) { }
fn main() {
run(work_with_s1);
run(work_with_s2);
}
error:
error: implementation of `From` is not general enough
--> src/main.rs:35:5
|
35 | run(work_with_s1);
| ^^^ implementation of `From` is not general enough
|
= note: `From<S<'0>>` would have to be implemented for the type `S1<'_>`, for any lifetime `'0`...
= note: ...but `From<S<'1>>` is actually implemented for the type `S1<'1>`, for some specific lifetime `'1`
I read from the error message that trait From
only implemented for a specific lifetime, but fn run
want it for any lifetime.
The question is: How can I modify the constrain on fn run
to only accept From<S>
just for a specific lifetime?