There is the Executor interface in Java, and my expectation that there is a trait in Rust.
Nope, at least not in std
. Traits in Rust are not used like interfaces Java. Concrete types are used rather than interfaces in many cases, because Rust (generally) prefers monomorphization over dynamic dispatch. So you will find much heavier use of interfaces in Java.
Thanks for the clarification.
The general role is conceptually present in Rust, with the same name even, but since futures are not "running" until polled there's no need to separate a "task" interface that starts the future or a way to abstract the thing that starts it. Just pass the Future instance around until it gets to the executor's spawn()
API (or equivalent)
A trait that plays a similar intended role to Executor
in Java is futures::task::Spawn
(for async code). However, I have not seen any async runtimes other than futures
’ own actually implement the Spawn
trait; they all just have spawn()
inherent methods or functions somewhere. Thread pools, similarly, have their own particular ways to run tasks on them.
Executor
uses Runnable
, which is blocking, and it's meant to start regular OS threads.
Rust has impl FnOnce() + Send
or impl FnMut() + Send
for Runnable
, and Executor.execute
is std::thread::spawn
or thread::Builder
if you must have an instance. In Rust the thread spawning is implemented using concrete types, since there was no need for any extra abstraction layer there.
Let me digest what you said and see if I can come to a similar solution in Rust.