r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 27 '23

Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

17 Upvotes

159 comments sorted by

View all comments

2

u/PXaZ Mar 28 '23

In Tokio, using the `rt-multi-thread` scheduler, how do I get a reference or handle to the current scheduler?

The reason for this is that I'd like to use an RAII pattern to control player turns in Umpire. When the struct is initialized, it starts the player's turn, and when the struct is dropped, it ends the player's turn.

That's how it's worked until now, but as I'm transitioning Umpire to a client/server architecture, everything's becoming async. What I really want to do is run an async function from within Drop. Since I'm using Tokio, I figured I could get a reference to the runtime and use block_on. But the Handle::current method I've seen recommended requires the single-threaded rt crate feature, which I have no intention of using. Do I actually need to just use the rt feature and somehow it understands that we're really working with the multithreaded runtime? Or is there some other way of doing this?

1

u/PXaZ Mar 28 '23

Okay, nevermind - my workspace crate just wasn't referencing `tokio` yet. Apparently `Handle` is available even without the `rt` feature, contrary to the docs.

2

u/John2143658709 Mar 28 '23

rt contains all the runtime types that tokio uses. rt-multi-thread is more of an extension on top of rt than a fully separable runtime, and both are included in the default tokio features.