There's a natural way to think about this if, for example, you want T to be a hash map. But last I checked you can't implement Fn manually on any struct.
What's the current best-practice work around? Options I'm currently considering are
Make a wrapper FnWrap around HashMap and implement something from the fn_traits crate on FnWrap< HashMap >.
Write my own Fn trait and implement HashMap.
If option 1. is the best, then would it make sense to submit a PR for FnWrap to fn_traits (I can always post an issue on their repo). Thanks!
I'm not sure what you want, I guess you want the generic to be std::collections::HashMap while T implements Fn(usize)->Option<usize>, but rustc disallowed you to do so.
If my understand is correct, the best practise is write a your own trait, implements it for all functions and HashMap.
trait MyTrait {
fn call(&self, input: usize) -> Option<usize>;
}
impl<F> MyTrait for F
where
F: Fn(usize) -> Option<usize>,
{
fn call(&self, input: usize) -> Option<usize> {
self(input)
}
}
impl<K, V> MyTrait for HashMap<K, V>
where
K: Eq + Hash,
{
// ...
}
Background/Motivation I often need to define structs which contain a std::tier::Map< I, F >, where the F is a function defined by a HashMap, a Vec, or a non-closure function. I typically find that I need to specify F concretely, but this is impossible if F is a closure.
What I take from your comments The best thing may be to write my own Fn trait. Of course, I can implement the Fn trait from fn_traits on any new types that I define, but I wouldn't be able to implement it on HashMap due to the orphan rule.