I have a struct Statistic<A, B>
which is used to fold over lots of A
s and then return a B
.
This Statistic
happens to be a bifunctor and I'd like to implement map
over the first argument. (Or rather co-map)
fn map(f: Box<Fn(A1) -> A2>, s: impl Statistic<A2, B>) -> impl Statistic<A1, B>
This will store both f
and s
in a struct and when new data arrives (of type A1
) it can apply f
to get a A2
value and then feed it into s
.
This works fine. But having to box all functions before passing them makes for a cumbersome API. I would prefer to box the functions myself.
fn map(f: impl Fn(A1) -> A2, s: impl Statistic<A2, B>) -> impl Statistic<A1, B>
But when I try to do this, I get a problem with lifetimes. Can you help me with that? I have been adding and removing lifetimes in mostly random places but nothing good came out of it.
Here is my working code:
// Working version
pub fn map<A1, A2, B, S>(preprocessor: Box<Fn(A1) -> A2>,
statistic: S)
-> MapStatistic<A1, A2, B, S>
where S: Statistic<A2, B>
{
MapStatistic::new(preprocessor, statistic)
}
// error[E0310]: the parameter type `F` may not live long enough
pub fn map<A1, A2, B, S, F>(preprocessor: F, statistic: S)
-> MapStatistic<A1, A2, B, S>
where S: Statistic<A2, B>,
F: Fn(A1) -> A2
{
MapStatistic::new(Box::new(preprocessor), statistic)
}
Btw, most of the time is spend calculating the A
values to feed into it so I'm not to worried about performance. (Between a few and a few hundred milliseconds each)