The following is the code and what the error said. I do not paste the all code, because I think the solution may be simple. If nessacery I could paste the whole code.
trait ForeCalc {
type A;
type B;
fn calc(&self, di: &'_ Di, data: Self::A) -> Self::B;
}
pub trait Ta {
type A<'a>: where Self: 'a;
type B;
fn from_di<'c, 'a>(&'c self, di: &'a Di) -> Self::A<'a>;
fn from_da<'e, 'a>(&'e self, da: Self::A<'a>) -> Self::B;
}
impl<'a, K> ForeCalc for K
where
K: Ta<A<'a> = Vec<f32>, B = Vec<f32>> + 'static,
{
type A = i32;
type B = Vec<f32>;
fn calc(&self, di: &'_ Di, _data: Self::A) -> Self::B
{
self.from_di(di)
}
}
Error: lifetime may not live long enough
โญโ[command_42:1:1]
โ
9 โ impl<'a, K> ForeCalc for K
ยท โโฌ
ยท โฐโโ lifetime `'a` defined here
ยท
15 โ fn calc(&self, di: &'_ Di, _data: Self::A) -> Self::B
ยท โฌ
ยท โฐโโ let's call the lifetime of this reference `'1`
ยท
17 โ self.from_di(di)
ยท โโโโโโโโโฌโโโโโโโ
ยท โฐโโโโโโโโโ argument requires that `'1` must outlive `'a`
โโโโโฏ
To diagnose this, weโll need to see the definitions of trait Ta and fn from_di at least. If you can, post enough code so that we can recreate the problem on the playground.
The problem is that your bound on K only specifies the associated type A for the specific lifetime 'a, which might be different than the lifetime of the reference &Di. You need to instead require that for every possible lifetime 'a,A<'a>=Vec<f32>:
impl<K> ForeCalc for K
where
K: for<'a> Ta<A<'a> = Vec<f32>, B = Vec<f32>> + 'static,
{
type A = i32;
type B = Vec<f32>;
fn calc(&self, di: & Di, _data: Self::A) -> Self::B
{
self.from_di(di)
}
}
use crate::Outer;
impl ForeCalc for Outer::Rank {
type A = Vec<f32>;
type B = Vec<f32>;
fn calc(&self, di: &'_ Di, data: Self::A) -> Self::B {
self.rank(data)
}
}
impl<K> ForeCalc for K
where
K: for<'a> Ta<A<'a> = Vec<f32>, B = Vec<f32>> + 'static,
{
type A = i32;
type B = Vec<f32>;
fn calc(&self, di: & Di, _data: Self::A) -> Self::B
{
self.from_di(di)
}
}
[E0119] Error: conflicting implementations of trait `ForeCalc` for type `bt::aa::Rank`
โญโ[command_56:1:1]
โ
1 โ impl ForeCalc for Rank {
ยท โโโโโโโโโโโโฌโโโโโโโโโโ
ยท โฐโโโโโโโโโโโโ first implementation here
ยท
9 โ impl<K> ForeCalc for K
ยท โโโโโโโโโโโโฌโโโโโโโโโโ
ยท โฐโโโโโโโโโโโโ conflicting implementation for `bt::aa::Rank`
โโโโฏ
When Rank comes from other crate, the compilter can not know if the Rank is implemented the traitForeCalc. So, if Rank has been implemented ForeCalc, then
impl<K> ForeCalc for K
where
K: for<'a> Ta<A<'a> = Vec<f32>, B = Vec<f32>> + 'static,
this where make a duplicate implementing on Rank.
But when Rank comes from local file, the compiler does know weather the Rank has been implemted ForeCalc.
If this guess is true, what can I do to avoid it?