Abstracting over threadpool and current_thread runtimes (tokio)

I have a library that spawns a bunch of tasks. The library is very lightweight; it opens only a few TCP connections and spends 99.9% of its time idle, so scaling is not important. The library works fine with both threadpool and current_thread runtimes, and I want to provide an API that works with both. For this I need to abstract over two things:

  • spawn function to use: tokio::spawn or tokio::current_thread::spawn
  • "shared mutable reference" type: Arc<Mutex<..>> or Rc<RefCell<..>>

A simple generalization of both of these APIs (spawn functions and ref types) would mean adding Send constraint to my API methods even though I don't need Send when current_thread runtime is used.

So I need something more sophisticated, but I'm not sure what. Any help would be appreciated.

1 Like

For the former there is tokio::executor::TypedExecutor. For the latter I feel like I've seen a crate experimenting with it, but I don't remember what it was called and can't find anything after a quick search.

It depends on the exact things you are trying to do, but you might be out of luck. Rust isn't very flexible over Send. Some discussion here:

There was a bunch of discussion about how to spawn from libraries here, maybe it interests you:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.