Skip to content

Commit

Permalink
Replace tokio traits with futures traits
Browse files Browse the repository at this point in the history
Necessary changes to substitutethe use of tokio traits with futures
traits such as AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt,
AsyncWrite and AsyncWriteExt

Signed-off-by: Rafael Garcia Ruiz <rafael.garcia@collabora.com>
  • Loading branch information
Razaloc committed Oct 28, 2022
1 parent 6352135 commit 05585e2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
6 changes: 3 additions & 3 deletions bmap-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ anyhow = "1.0.66"
nix = "0.25.0"
flate2 = "1.0.24"
clap = { version = "4.0.18", features = ["derive"] }
async-compression = { version = "0.3.15", features = ["gzip", "futures-io", "tokio"] }
async-compression = { version = "0.3.15", features = ["gzip", "futures-io"] }
tokio = { version = "1.21.2", features = ["full"] }
reqwest = { version = "0.11.12", features = ["stream"]}
tokio-util= { version = "0.7.4" }
reqwest = { version = "0.11.12", features = ["stream"] }
tokio-util= { version = "0.7.4", features = ["compat"] }
bytes = "1.2.1"
futures = "0.3.25"
9 changes: 4 additions & 5 deletions bmap-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::fs::File;
use std::io::Read;
use std::os::unix::io::AsRawFd;
use std::path::{Path, PathBuf};
use tokio_util::io::StreamReader;
use tokio_util::compat::TokioAsyncReadCompatExt;

#[derive(Parser, Debug)]
struct Copy {
Expand Down Expand Up @@ -175,11 +175,10 @@ async fn copy(c: Copy) -> Result<()> {
let stream = res
.bytes_stream()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
.into_stream();
let read = StreamReader::new(stream);
let reader = async_compression::tokio::bufread::GzipDecoder::new(read);
.into_async_read();
let reader = async_compression::futures::bufread::GzipDecoder::new(stream);
let mut input = AsyncDiscarder::new(reader);
bmap::copy_async(&mut input, output, &bmap).await?;
bmap::copy_async(&mut input, &mut output.compat(), &bmap).await?;
}
};

Expand Down
1 change: 0 additions & 1 deletion bmap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ sha2 = { version = "0.10.6", features = [ "asm" ] }
strum = { version = "0.24.1", features = [ "derive"] }
digest = "0.10.5"
flate2 = "1.0.20"
tokio = { version = "1.21.2" }
async-trait = "0.1.58"
futures = "0.3.25"
20 changes: 17 additions & 3 deletions bmap/src/discarder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{AsyncSeekForward, SeekForward};
use async_trait::async_trait;
use futures::executor::block_on;
use futures::io::{AsyncRead, AsyncReadExt};
use std::io::Read;
use std::io::Result as IOResult;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use tokio::io::{AsyncRead, AsyncReadExt};

/// Adaptor that implements SeekForward on types only implementing Read by discarding data
pub struct Discarder<R: Read> {
Expand Down Expand Up @@ -62,10 +62,24 @@ impl<R: AsyncRead + Unpin> AsyncRead for AsyncDiscarder<R> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<Result<(), std::io::Error>> {
buf: &mut [u8],
) -> Poll<Result<usize, std::io::Error>> {
Pin::new(&mut self.reader).poll_read(cx, buf)
}

fn poll_read_vectored(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [std::io::IoSliceMut<'_>],
) -> Poll<IOResult<usize>> {
for b in bufs {
if !b.is_empty() {
return self.poll_read(cx, b);
}
}

Pin::new(&mut self.reader).poll_read(cx, &mut [])
}
}
#[async_trait(?Send)]
impl<R: AsyncRead + Unpin> AsyncSeekForward for AsyncDiscarder<R> {
Expand Down
2 changes: 1 addition & 1 deletion bmap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ pub use crate::bmap::*;
mod discarder;
pub use crate::discarder::*;
use async_trait::async_trait;
use futures::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt, AsyncWrite, AsyncWriteExt};
use futures::TryFutureExt;
use sha2::{Digest, Sha256};
use std::io::Result as IOResult;
use std::io::{Read, Seek, SeekFrom, Write};
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt, AsyncWrite, AsyncWriteExt};

/// Trait that can only seek further forwards
pub trait SeekForward {
Expand Down

0 comments on commit 05585e2

Please sign in to comment.