-2

I'm trying to write a library that will connect to remote servers and exchange data. I did this in C++ using Boost::Asio and am trying to do the same with Rust.

One of the problems I have is mapping concepts from Asio, like async_write/read to Tokio, starting with the fact that seemingly all Tokio examples demand that I replace my main() with an async main(), while I would like to encapsulate all my async code in structures and associated implementations.

Is it possible to use Tokio without replacing main()? Is mio perhaps the only way?

ruipacheco
  • 15,025
  • 19
  • 82
  • 138
  • You can create a Tokio reactor and submit a future to handle, but what's the point? You're just writing the code manually that the proc macro writes for you. There seems to be a deeper misunderstanding here. – Sebastian Redl May 22 '20 at 12:18
  • Perhaps. With Asio I have a thread pool and make my event loop run on it, I don't need to replace main. With Rust I do and that's what's confusing to me. – ruipacheco May 22 '20 at 12:31
  • You can create a runtime manually:https://docs.rs/tokio/0.2.21/tokio/runtime/index.html which is what the tokio main macro is doing under the hood. It's just for an awful lot of apps, especially examples that's just boilerplate. – user1937198 May 22 '20 at 12:55
  • However, depending on the context of your library, it may be more idiomatic to provide a future based API, and then leave the app consumer to set up the runtime. – user1937198 May 22 '20 at 12:57
  • What's the difference between a Runtime and a Reactor? – ruipacheco May 22 '20 at 13:00
  • Can you turn the Runtime into an answer so I can accept it? – ruipacheco May 22 '20 at 13:01
  • 1
    The reactor is just managing the io calls into the os. The runtime is the complete package of thread pool to run tasks on, reactor, and timer management. – user1937198 May 22 '20 at 13:04
  • It looks like your question might be answered by the answers of [How do I synchronously return a value calculated in an asynchronous Future in stable Rust?](https://stackoverflow.com/q/52521201/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster May 22 '20 at 13:12
  • 1
    Does this answer your question? [How do I synchronously return a value calculated in an asynchronous Future in stable Rust?](https://stackoverflow.com/questions/52521201/how-do-i-synchronously-return-a-value-calculated-in-an-asynchronous-future-in-st) – E_net4 May 22 '20 at 13:23

1 Answers1

1

You can create a runtime manually using Runtime::new() which is what the tokio main macro is doing under the hood. It's just for an awful lot of apps, especially examples that's just boilerplate. So the macro automates the simple case.

However, depending on the context of your library, it may be more idiomatic to provide a future based API, and then leave the app consumer to set up the runtime.

user1937198
  • 4,987
  • 4
  • 20
  • 31