]> git.proxmox.com Git - pxar.git/commitdiff
update to tokio 1.0
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 4 Dec 2020 10:39:55 +0000 (11:39 +0100)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 29 Dec 2020 12:58:38 +0000 (13:58 +0100)
unfortunately, futures::io::AsyncRead and tokio::io::AsyncRead no longer
share a do_poll_read signature, so we need to adapt one to the other
(and also no longer generate some wrapper implementations via macro).

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Cargo.toml
src/accessor/aio.rs
src/decoder/aio.rs

index 24b5489c2aaa2a78951ab5dffb5b88f61a2d93d5..875de7afb68478c61a40eb0459e9cb97bd31108e 100644 (file)
@@ -47,7 +47,7 @@ siphasher = "0.3"
 
 anyhow = { version = "1.0", optional = true }
 futures = { version = "0.3.1", optional = true }
-tokio = { version = "0.2.10", optional = true, default-features = false }
+tokio = { version = "1.0", optional = true, default-features = false }
 
 [target.'cfg(target_os = "linux")'.dependencies]
 libc = "0.2"
@@ -65,8 +65,7 @@ async-example = [
     "futures-io",
     "tokio-io",
     "tokio-fs",
-    "tokio/rt-threaded",
-    "tokio/io-driver",
+    "tokio/rt-multi-thread",
     "tokio/macros",
 ]
 
index a1aaa08aae77caef0547ab070e9876c8bdfdb3f9..dd017aee4a1c131a84013c199d546206773c6e15 100644 (file)
@@ -410,9 +410,10 @@ impl<T: Clone + ReadAt> tokio::io::AsyncRead for FileContents<T> {
     fn poll_read(
         self: Pin<&mut Self>,
         cx: &mut Context,
-        buf: &mut [u8],
-    ) -> Poll<io::Result<usize>> {
-        Self::do_poll_read(self, cx, buf)
+        buf: &mut tokio::io::ReadBuf,
+    ) -> Poll<io::Result<()>> {
+        Self::do_poll_read(self, cx, &mut buf.initialize_unfilled())
+            .map_ok(|bytes| { buf.set_filled(bytes); () })
     }
 }
 
index e7152b31eff1d4441bd109ea8e11092f5538c608..1a5f5ea7687a5909c4f5673c5308d5f99ea13df3 100644 (file)
@@ -136,61 +136,72 @@ mod stream {
 #[cfg(feature = "futures-io")]
 pub use stream::DecoderStream;
 
-macro_rules! async_io_impl {
-    (
-        #[cfg( $($attr:tt)+ )]
-        mod $mod:ident {
-            $(#[$docs:meta])*
-            $name:ident : $trait:path ;
-        }
-    ) => {
-        #[cfg( $($attr)+ )]
-        mod $mod {
-            use std::io;
-            use std::pin::Pin;
-            use std::task::{Context, Poll};
-
-            $(#[$docs])*
-            pub struct $name<T> {
-                inner: T,
-            }
+#[cfg(feature = "futures-io")]
+mod fut {
+    use std::io;
+    use std::pin::Pin;
+    use std::task::{Context, Poll};
 
-            impl<T: $trait> $name<T> {
-                pub fn new(inner: T) -> Self {
-                    Self { inner }
-                }
-            }
+    /// Read adapter for `futures::io::AsyncRead`
+    pub struct FuturesReader<T> {
+        inner: T,
+    }
 
-            impl<T: $trait> crate::decoder::SeqRead for $name<T> {
-                fn poll_seq_read(
-                    self: Pin<&mut Self>,
-                    cx: &mut Context,
-                    buf: &mut [u8],
-                ) -> Poll<io::Result<usize>> {
-                    unsafe {
-                        self.map_unchecked_mut(|this| &mut this.inner)
-                            .poll_read(cx, buf)
-                    }
-                }
+    impl<T: futures::io::AsyncRead> FuturesReader<T> {
+        pub fn new(inner: T) -> Self {
+            Self { inner }
+        }
+    }
+
+    impl<T: futures::io::AsyncRead> crate::decoder::SeqRead for FuturesReader<T> {
+        fn poll_seq_read(
+            self: Pin<&mut Self>,
+            cx: &mut Context,
+            buf: &mut [u8],
+        ) -> Poll<io::Result<usize>> {
+            unsafe {
+                self.map_unchecked_mut(|this| &mut this.inner)
+                    .poll_read(cx, buf)
             }
         }
-        #[cfg( $($attr)+ )]
-        pub use $mod::$name;
     }
 }
 
-async_io_impl! {
-    #[cfg(feature = "futures-io")]
-    mod fut {
-        /// Read adapter for `futures::io::AsyncRead`.
-        FuturesReader : futures::io::AsyncRead;
+#[cfg(feature = "futures-io")]
+use fut::FuturesReader;
+
+#[cfg(feature = "tokio-io")]
+mod tok {
+    use std::io;
+    use std::pin::Pin;
+    use std::task::{Context, Poll};
+
+    /// Read adapter for `futures::io::AsyncRead`
+    pub struct TokioReader<T> {
+        inner: T,
     }
-}
 
-async_io_impl! {
-    #[cfg(feature = "tokio-io")]
-    mod tok {
-        /// Read adapter for `tokio::io::AsyncRead`.
-        TokioReader : tokio::io::AsyncRead;
+    impl<T: tokio::io::AsyncRead> TokioReader<T> {
+        pub fn new(inner: T) -> Self {
+            Self { inner }
+        }
+    }
+
+    impl<T: tokio::io::AsyncRead> crate::decoder::SeqRead for TokioReader<T> {
+        fn poll_seq_read(
+            self: Pin<&mut Self>,
+            cx: &mut Context,
+            buf: &mut [u8],
+        ) -> Poll<io::Result<usize>> {
+            let mut read_buf = tokio::io::ReadBuf::new(buf);
+            unsafe {
+                self.map_unchecked_mut(|this| &mut this.inner)
+                    .poll_read(cx, &mut read_buf)
+                    .map_ok(|_| read_buf.filled().len())
+            }
+        }
     }
 }
+
+#[cfg(feature = "tokio-io")]
+use tok::TokioReader;