Debian Codegraph @

third_party/rust/tokio-util/src/io/sync_bridge.rs

raw
1 use std::io::{Read, Write};
2 use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
3
4 /// Use a [`tokio::io::AsyncRead`] synchronously as a [`std::io::Read`] or
5 /// a [`tokio::io::AsyncWrite`] as a [`std::io::Write`].
6 #[derive(Debug)]
7 pub struct SyncIoBridge<T> {
8 src: T,
9 rt: tokio::runtime::Handle,
10 }
11
12 impl<T: AsyncRead + Unpin> Read for SyncIoBridge<T> {
13 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
14 let src = &mut self.src;
15 self.rt.block_on(AsyncReadExt::read(src, buf))
16 }
17
18 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> std::io::Result<usize> {
19 let src = &mut self.src;
20 self.rt.block_on(src.read_to_end(buf))
21 }
22
23 fn read_to_string(&mut self, buf: &mut String) -> std::io::Result<usize> {
24 let src = &mut self.src;
25 self.rt.block_on(src.read_to_string(buf))
26 }
27
28 fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
29 let src = &mut self.src;
30 // The AsyncRead trait returns the count, synchronous doesn't.
31 let _n = self.rt.block_on(src.read_exact(buf))?;
32 Ok(())
33 }
34 }
35
36 impl<T: AsyncWrite + Unpin> Write for SyncIoBridge<T> {
37 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
38 let src = &mut self.src;
39 self.rt.block_on(src.write(buf))
40 }
41
42 fn flush(&mut self) -> std::io::Result<()> {
43 let src = &mut self.src;
44 self.rt.block_on(src.flush())
45 }
46
47 fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
48 let src = &mut self.src;
49 self.rt.block_on(src.write_all(buf))
50 }
51
52 fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
53 let src = &mut self.src;
54 self.rt.block_on(src.write_vectored(bufs))
55 }
56 }
57
58 // Because https://doc.rust-lang.org/std/io/trait.Write.html#method.is_write_vectored is at the time
59 // of this writing still unstable, we expose this as part of a standalone method.
60 impl<T: AsyncWrite> SyncIoBridge<T> {
61 /// Determines if the underlying [`tokio::io::AsyncWrite`] target supports efficient vectored writes.
62 ///
63 /// See [`tokio::io::AsyncWrite::is_write_vectored`].
64 pub fn is_write_vectored(&self) -> bool {
65 self.src.is_write_vectored()
66 }
67 }
68
69 impl<T: Unpin> SyncIoBridge<T> {
70 /// Use a [`tokio::io::AsyncRead`] synchronously as a [`std::io::Read`] or
71 /// a [`tokio::io::AsyncWrite`] as a [`std::io::Write`].
72 ///
73 /// When this struct is created, it captures a handle to the current thread's runtime with [`tokio::runtime::Handle::current`].
74 /// It is hence OK to move this struct into a separate thread outside the runtime, as created
75 /// by e.g. [`tokio::task::spawn_blocking`].
76 ///
77 /// Stated even more strongly: to make use of this bridge, you *must* move
78 /// it into a separate thread outside the runtime. The synchronous I/O will use the
79 /// underlying handle to block on the backing asynchronous source, via
80 /// [`tokio::runtime::Handle::block_on`]. As noted in the documentation for that
81 /// function, an attempt to `block_on` from an asynchronous execution context
82 /// will panic.
83 ///
84 /// # Wrapping `!Unpin` types
85 ///
86 /// Use e.g. `SyncIoBridge::new(Box::pin(src))`.
87 ///
88 /// # Panic
89 ///
90 /// This will panic if called outside the context of a Tokio runtime.
91 pub fn new(src: T) -> Self {
92 Self::new_with_handle(src, tokio::runtime::Handle::current())
93 }
94
95 /// Use a [`tokio::io::AsyncRead`] synchronously as a [`std::io::Read`] or
96 /// a [`tokio::io::AsyncWrite`] as a [`std::io::Write`].
97 ///
98 /// This is the same as [`SyncIoBridge::new`], but allows passing an arbitrary handle and hence may
99 /// be initially invoked outside of an asynchronous context.
100 pub fn new_with_handle(src: T, rt: tokio::runtime::Handle) -> Self {
101 Self { src, rt }
102 }
103 }