Clean way to declare stand-alone closure

Hello,

I want to provide stand-alone definitions for closure types, along the lines of this below. But by factoring out the closure definition into its own trait, I lose the compiler's ability to infer the types of the generics, even though they are declared in the function's use of the closure.

Is it possible to get the best of both worlds?

/// I want to provide nice documentation about how to use the closure type,
/// on its own, away from the function that takes it as an arg.
trait MyClosure<OneT, TwoT> : Fn(&OneT, &[TwoT]) {}

//This complains
//fn fancy_func<ClosureT : MyClosure<usize, usize>>(closure : ClosureT) {

//This works
fn fancy_func<ClosureT : Fn(&usize, &[usize])>(closure : ClosureT) {
    closure(&1, &[1, 2, 3]);
}

fn main() {
    fancy_func(|my_index, my_slice| println!("{}", my_slice[*my_index]));
}

Thank you.

I should be clear, my goal is for the calling function to get the benefit of the type inference, without needing to explicitly specify the types for the closure parameters.

So this works:

trait MyClosure<OneT, TwoT> : Fn(&OneT, &[TwoT]) {}
impl <T : Fn(&OneT, &[TwoT]), OneT, TwoT>MyClosure<OneT, TwoT> for T {}

fn fancy_func<ClosureT : MyClosure<usize, usize>>(closure : ClosureT) {
    closure(&1, &[1, 2, 3]);
}

fn main() {
    fancy_func(|my_index : &usize, my_slice : &[usize]| println!("{}", my_slice[*my_index]));
}

But this doesn't, and ideally I would like it it.

trait MyClosure<OneT, TwoT> : Fn(&OneT, &[TwoT]) {}
impl <T : Fn(&OneT, &[TwoT]), OneT, TwoT>MyClosure<OneT, TwoT> for T {}

fn fancy_func<ClosureT : MyClosure<usize, usize>>(closure : ClosureT) {
    closure(&1, &[1, 2, 3]);
}

fn main() {
    fancy_func(|my_index, my_slice| println!("{}", my_slice[*my_index]));
}

So I'm wondering if there is a nice clean way to get back the type inference from the very first example.

Thanks again.

Rats. I thought trait_aliasing might be what I need, but I couldn't make it infer the argument types any better than by using trait bounds. Then I saw this, so it appears to be a known issue. :frowning:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=e637bc855969fbac3c6f75c60d0070a1

from: