-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
util/io: Add SyncIoBridge
#4146
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not create a single type with both impls? Then it is easier to bridge a type with both traits.
5040268
to
e64500f
Compare
Good suggestion, done! |
e3bef63
to
913b94e
Compare
tokio-util/src/io/sync_bridge.rs
Outdated
/// Use a [`tokio::io::AsyncRead`] synchronously as a [`std::io::Read`] or | ||
/// a [`tokio::io::AsyncWrite`] as a [`std::io::Write`]. | ||
#[derive(Debug)] | ||
pub struct SyncIOBridge<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: in general, idiomatic Rust naming treats acronyms (like "IO") as one "word", rather than capitalizing every letter: https://doc.rust-lang.org/1.0.0/style/style/naming/README.html#general-conventions-[rfc-#430]
So, this should be
pub struct SyncIOBridge<T> { | |
pub struct SyncIoBridge<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, take it or leave it, but we could probably just drop the Io
from the name of this, since it's already in the io
module? i'm fine either way though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was uncertain about the Io
. What makes me lean towards leaving it in is that there's also e.g. the whole tokio::sync
module which is about synchronization as opposed to "synchronous". I don't expect this type to be frequently used, and it's probably worth being a bit more verbose to highlight the conversion? But if someone feels strongly I'm obviously fine changing!
Maybe we could use the term "blocking", e.g. BlockingIoWrapper
?
Or actually, there's already some precendent in tokio for from_std so we could call it StdIoWrapper
(or StdIoBridge
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Blocking
or Sync
is better than Std
here. I don't particularly mind including Io
in the name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK...keeping this as SyncIoBridge
then just from inertia basically. But happy to change if someone else feels strongly.
15d2f0a
to
e729ec2
Compare
I'm working on some code which heavily uses `spawn_blocking` to run synchronous code, and it took me a while to find and understand the relevant APIs and patterns here. Let's link to the channel blocking APIs, and provide a small example. I plan to mention tokio-rs#4146 here too once that merges.
I'm working on some code which heavily uses `spawn_blocking` to run synchronous code, and it took me a while to find and understand the relevant APIs and patterns here. Let's link to the channel blocking APIs, and provide a small example. I plan to mention tokio-rs#4146 here too once that merges.
cb4f217
to
c5f145d
Compare
When you make changes to this PR, please avoid force pushing as it makes it difficult for me to see what changed since last time I looked. Do not worry about there being lots of commits — they will be squashed when we merge the PR. |
c5f145d
to
e82099b
Compare
OK, though I think I had some local changes before I saw this, so there is a final force here, but subsequent update commits. |
The CI failure is due to you being very far behind the master branch. |
I'm doing quite a bit of stuff inside `spawn_blocking` because I need to use some large synchronous APIs. I found it not entirely obvious how to "bridge" the world of async I/O with sync I/O. Since we have a handy "tokio-util" module with an "io" feature, these small helpers seem like a good fit hopefully! Extracted these from my code in https://github.com/ostreedev/ostree-rs-ext/blob/main/lib/src/async_util.rs
Rebased 🏄 |
(This is really the first time I've tried to debug issues in a multi-crate git repository with crosscutting features) OK, I was misreading the CI before, at least one failure reproduces here with: I initially tried sprinkling So this fixes it for me:
but would that be OK? Or would it be necessary to add a new tokio-util feature that in turn enables the |
We could add an |
Necessary for the sync bridge work. I don't know whether this is OK.
Now that I looked it is easier, we just need to have the
That said, happy to do that instead if the |
Ahh. Right, we also need |
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
If CI keeps failing for unrelated reasons, you may want to merge in master to your PR. |
I figured out what the problem is. #4189 |
Ah nice, thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good!
If you want, you could amend the Bridging with sync code topics page with a section on this utility. |
Oh wow, I somehow completely missed that before. Should I update #4150 to link to that too? |
Sure. |
We can only land this after a new tokio is released with tokio-rs/tokio#4146
xref #4150 (comment) |
We can only land this after a new tokio is released with tokio-rs/tokio#4146
From time to time I like to explicitly say "thank you" in a Github comment. A lot of time Github conversations can be about bug reports, people upset about regressions, contentious debates, etc. This was my first real PR to this repository and you all took the time to work with me, patiently found issues and help me learn new things, and it was overall a great experience. It definitely led to me feeling positive about contributing to tokio again! So: thank you! 🙏 |
That's great! I'm happy that you found it to be a good experience. |
Motivation
I'm doing quite a bit of stuff inside
spawn_blocking
becauseI need to use some large synchronous APIs.
I found it not entirely obvious how to "bridge" the world of
async I/O with sync I/O.
Since we have a handy "tokio-util" module with an "io" feature,
these small helpers seem like a good fit hopefully!
Solution
Extracted these from my code in https://github.com/ostreedev/ostree-rs-ext/blob/main/lib/src/async_util.rs