So according to the latest comments https://github.com/rust-lang/rust/issues/29625, implementing FnOnce/Fn/FnMut for my structs is legit (i.e. I can expect it to stabilize at some point... right?).
Meanwhile, let's try taking things one step further - maybe I can make all implementations of a trait callable:
33 | extern "rust-call" fn call_once (self, args: ()) -> Result<()> {
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn my_mod::MyTrait + 'static)`
= help: unsized fn params are gated as an unstable feature
Adding a Sized bound to MyTrait just makes things worse:
the trait 'my_mod::MyTrait' cannot be made into an object
Enabling #![feature(unsized_locals)] does nothing.
So, are callable trait objects (and/or making structs callable by implementing traits for them) possible at all?
I hope so, but there's no guarantee. The traits may also change shape until stabilized.
I don't know of blanket implementation that would work, but you can implement for Box<dyn MyTrait> and &dyn MyTrait (and the latter would indirectly allow other containers, albeit not in a consuming manner).
@quinedot: it works with the Box. How does heap allocation relate to dispatch though? I was hoping I would be able to directly call the structs that implement the trait, see playground
For that you probably do want FnOnce implemented on the objects, so you don't have to coerce to the trait object. As you noted, the orphan rules prevent you from doing this yourself, but you could add a FnOnce supertrait to MyTrait.