Implementing traits on closures

I've run into an interesting issue while planning out a futures executor design. I'm planning out an executor that is similar to python's SCOOP, with the idea that futures can be moved from one compute resource to another as resources become available. I can create my own implementations of Future that also implement serde's serialization interfaces, but that only works up until someone wants to pass in an async closure. As far as I know, it isn't possible to implement a trait on a closure today. I searched the archives, and the closest topic to this that I've been able to find is this one, but I didn't see an RFC come out of it.

So, have I missed the blindingly obvious, or is there currently no way to implement traits on closures? Is it possible to modify the language to permit this?

1 Like

Nope, there is no way to implement traits on just closures. One way around this is to newtype it, but that has an ergonomic cost.

i.e.

struct Closure<F>(pub F);

impl<F: FnOnce()> Trait for Closure<F> {
}
1 Like

Yeah, that's what I was afraid of (it was what was suggested in the other topic that I linked to). Thank you for confirming it for me.

Does anyone know how difficult it would be to implement this capability? This is the first time I've run across the need for it, so for me personally it's a low priority, but if it were really easy to implement it might be worth it.

I did propose a way forward in the linked thread, but I'm not sure how difficult it would be to implement.