Skip to content

Commit

Permalink
more async read traits
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Aug 5, 2020
1 parent 60ed9af commit b20ab59
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
32 changes: 20 additions & 12 deletions htfs/src/async_buf_read.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use async_trait::async_trait;
use futures::AsyncRead;
use futures::{AsyncRead, AsyncReadExt};
use std::io;

/// Read bytes from any offset asynchronously
#[async_trait]
#[async_trait(?Send)]
pub trait AsyncReadAt {
async fn read_at(&self, offset: u64, buf: &mut [u8]) -> io::Result<usize>;
}
Expand All @@ -14,26 +14,34 @@ pub trait GetReaderAt {
async fn get_reader_at(&self, offset: u64) -> io::Result<Self::Reader>;
}

pub trait IntoAsyncReadAt {
pub trait IntoAsyncReadAt: Send {
type Out: AsyncReadAt;
fn into_async_read_at() -> Self::Out;

fn into_async_read_at(self) -> Self::Out;
}

impl<T> IntoAsyncReadAt for T
where
T: GetReaderAt,
T: GetReaderAt + Send,
<T as GetReaderAt>::Reader: std::marker::Unpin,
{
type Out = Wrapper;
fn into_async_read_at() -> Self::Out {
Wrapper {}
type Out = Wrapper<T>;
fn into_async_read_at(self) -> Self::Out {
Wrapper { inner: self }
}
}

pub struct Wrapper {}
pub struct Wrapper<R> {
inner: R,
}

#[async_trait]
impl AsyncReadAt for Wrapper {
#[async_trait(?Send)]
impl<R> AsyncReadAt for Wrapper<R>
where
R: GetReaderAt,
<R as GetReaderAt>::Reader: std::marker::Unpin,
{
async fn read_at(&self, offset: u64, buf: &mut [u8]) -> io::Result<usize> {
todo!()
self.inner.get_reader_at(offset).await?.read(buf).await
}
}
2 changes: 1 addition & 1 deletion htfs/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl ReaderInner {
async fn read_internal(&mut self, n: usize) -> io::Result<usize> {
self.buf.clear();
self.buf.reserve(n);
for i in 0..n {
for _ in 0..n {
self.buf.push(0);
}

Expand Down
2 changes: 1 addition & 1 deletion htfs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn some_test_inner() -> Result<(), Report> {
let size = 4 * 1024;
let mut data = Vec::with_capacity(size);

for i in 0..size {
for _ in 0..size {
data.push(rand.rand_range(0..256) as u8);
}
let data = Arc::new(data);
Expand Down

0 comments on commit b20ab59

Please sign in to comment.