-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The bane of my existence: Supporting both async and sync code in Rust #4
Comments
Thank you, this was educative. I subscribed to this issue with comments, hoping that sometime in the future, if a good solution to the problem appears, you'll update the blog post and notify commenters here.
The Fixing I'd likely continue using the Just to give some perspective, in the MongoDB Java Driver we use the Good ol' copy-pasting approach, only it's much worse than what you had in RSpotify: the async implementations and even declarations have to be different from the sync ones because there is no 1 Virtual threads were added in Java SE 21, and they make our asynchronous API more or less worthless for programmers who use Java SE 21. But we can't just get rid of this API, because there are enough clients who use Java SE 8 😭 |
This might be a stupid thing, as I'm only starting now to learn Rust, but wouldn't it be possible for you to provide only the sync APIs and let the clients implement the async part on top, if they want/need to? |
@siancu It is technically possible. For example, one may turn a synchronous API into an asynchronous one using |
FYI I wrote a crate that approaches the sync vs async challenge a slightly different way based on the likely direction of the keyword generic initiative: |
@scouten The documentation of your crate says
("can", not "must"), but of the two examples it provides, both have a synchronous original function with an |
@stIncMale in the use cases I encountered, there was often a long part of the function that was shared between sync and async cases and then a tiny portion that required That said, I would welcome a PR that patches in/out the |
@mr0rng, that's great to know. I wonder what @marioortizmanero will say if he tries to use |
I'm new to Rust so maybe I'm being overly simplistic, but I think you were too quick to discard the |
The article is really great! |
The whole issue with So I lifted the futures-rs block_on implementation into a separate crate, with minimal code and dependency tree. You should be able to call it on any future from a syncronous context, with no need to create or manage a runtime. (Like futures-rs, it creates a thread local to receive completion notifications.) Admittedly, I haven't tested it. But it compiles fine, so... |
I decided to give it a shot myself, I think I found a neat solution using path specifiers for modules. |
I had a similar problem with the |
I've been struggling writing both blocking and async code in an |
Original post: https://nullderef.com/blog/rust-async-sync
You can use this issue to leave any comments.
The text was updated successfully, but these errors were encountered: