pub trait ForeCalc {
type A;
type B;
fn calc(&self, di: &Di, data: Self::A) -> Self::B;
}
impl ForeCalc for Rank {
type A = &[f32];
type B = Vec<f32>;
fn calc(&self, _di: &Di, data: Self::A) -> Self::B {
self.rank(data)
}
}
I need to annotate that the lifetime of associated type A = &[f32]' in trait ForeCalc is shorter than the lifetime of paramter _di: &Di from trait ForeCalc method calc.
The code may look like this:
impl<'a> ForeCalc for Rank {
type A = &'a [f32];
type B = Vec<f32>;
fn calc(&self, _di: &'a Di, data: Self::A) -> Self::B {
self.rank(data)
}
}
But I do not know how to write it down. What should I do?
You don't need to specify associated type if you don't use it. This works just fine.
impl<T> CalcId for T
where
T: ForeCalc + std::fmt::Debug,
{
fn id(&self) -> String {
format!("{:?}", self)
}
}
fn main() {
println!("{}", Rank(2, 2).id());
}
&[f32] is not a single type, instead, for every different lifetime 'a, &'a [f32] is a different type. So there's no type A satisfy for<'l> ForeCalc<A<'l> = A>