Skip to content
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

simplify driver operation futures #139

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions src/driver/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::BufResult;

use crate::driver::op::Completable;
use std::io;
use std::task::{Context, Poll};

pub(crate) struct Read<T> {
/// Holds a strong ref to the FD, preventing the file from being closed
Expand Down Expand Up @@ -35,19 +34,6 @@ impl<T: IoBufMut> Op<Read<T>> {
},
)
}

pub(crate) async fn read(mut self) -> BufResult<usize, T> {
crate::future::poll_fn(move |cx| self.poll_read(cx)).await
}

pub(crate) fn poll_read(&mut self, cx: &mut Context<'_>) -> Poll<BufResult<usize, T>> {
use std::future::Future;
use std::pin::Pin;

let complete = ready!(Pin::new(self).poll(cx));

Poll::Ready(complete)
}
}

impl<T> Completable for Read<T>
Expand Down
14 changes: 0 additions & 14 deletions src/driver/readv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::BufResult;
use crate::driver::op::Completable;
use libc::iovec;
use std::io;
use std::task::{Context, Poll};

pub(crate) struct Readv<T> {
/// Holds a strong ref to the FD, preventing the file from being closed
Expand Down Expand Up @@ -54,19 +53,6 @@ impl<T: IoBufMut> Op<Readv<T>> {
},
)
}

pub(crate) async fn readv(mut self) -> BufResult<usize, Vec<T>> {
crate::future::poll_fn(move |cx| self.poll_readv(cx)).await
}

pub(crate) fn poll_readv(&mut self, cx: &mut Context<'_>) -> Poll<BufResult<usize, Vec<T>>> {
use std::future::Future;
use std::pin::Pin;

let complete = ready!(Pin::new(self).poll(cx));

Poll::Ready(complete)
}
}

impl<T> Completable for Readv<T>
Expand Down
19 changes: 0 additions & 19 deletions src/driver/recv_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
use socket2::SockAddr;
use std::{
io::IoSliceMut,
task::{Context, Poll},
{boxed::Box, io, net::SocketAddr},
};

Expand Down Expand Up @@ -53,24 +52,6 @@ impl<T: IoBufMut> Op<RecvFrom<T>> {
},
)
}

pub(crate) async fn recv(mut self) -> BufResult<(usize, SocketAddr), T> {
use crate::future::poll_fn;

poll_fn(move |cx| self.poll_recv_from(cx)).await
}

pub(crate) fn poll_recv_from(
&mut self,
cx: &mut Context<'_>,
) -> Poll<BufResult<(usize, SocketAddr), T>> {
use std::future::Future;
use std::pin::Pin;

let complete = ready!(Pin::new(self).poll(cx));

Poll::Ready(complete)
}
}

impl<T> Completable for RecvFrom<T>
Expand Down
15 changes: 0 additions & 15 deletions src/driver/send_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::driver::{Op, SharedFd};
use crate::BufResult;
use socket2::SockAddr;
use std::io::IoSlice;
use std::task::{Context, Poll};
use std::{boxed::Box, io, net::SocketAddr};

pub(crate) struct SendTo<T> {
Expand Down Expand Up @@ -55,20 +54,6 @@ impl<T: IoBuf> Op<SendTo<T>> {
},
)
}

pub(crate) async fn send(mut self) -> BufResult<usize, T> {
use crate::future::poll_fn;

poll_fn(move |cx| self.poll_send(cx)).await
}

pub(crate) fn poll_send(&mut self, cx: &mut Context<'_>) -> Poll<BufResult<usize, T>> {
use std::future::Future;
use std::pin::Pin;

let complete = ready!(Pin::new(self).poll(cx));
Poll::Ready(complete)
}
}

impl<T> Completable for SendTo<T>
Expand Down
8 changes: 4 additions & 4 deletions src/driver/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Socket {

pub(crate) async fn write<T: IoBuf>(&self, buf: T) -> crate::BufResult<usize, T> {
let op = Op::write_at(&self.fd, buf, 0).unwrap();
op.write().await
op.await
}

pub(crate) async fn send_to<T: IoBuf>(
Expand All @@ -50,20 +50,20 @@ impl Socket {
socket_addr: SocketAddr,
) -> crate::BufResult<usize, T> {
let op = Op::send_to(&self.fd, buf, socket_addr).unwrap();
op.send().await
op.await
}

pub(crate) async fn read<T: IoBufMut>(&self, buf: T) -> crate::BufResult<usize, T> {
let op = Op::read_at(&self.fd, buf, 0).unwrap();
op.read().await
op.await
}

pub(crate) async fn recv_from<T: IoBufMut>(
&self,
buf: T,
) -> crate::BufResult<(usize, SocketAddr), T> {
let op = Op::recv_from(&self.fd, buf).unwrap();
op.recv().await
op.await
}

pub(crate) async fn accept(&self) -> io::Result<(Socket, Option<SocketAddr>)> {
Expand Down
19 changes: 1 addition & 18 deletions src/driver/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use crate::{
driver::{Op, SharedFd},
BufResult,
};
use std::{
io,
task::{Context, Poll},
};
use std::io;

pub(crate) struct Write<T> {
/// Holds a strong ref to the FD, preventing the file from being closed
Expand Down Expand Up @@ -38,20 +35,6 @@ impl<T: IoBuf> Op<Write<T>> {
},
)
}

pub(crate) async fn write(mut self) -> BufResult<usize, T> {
use crate::future::poll_fn;

poll_fn(move |cx| self.poll_write(cx)).await
}

pub(crate) fn poll_write(&mut self, cx: &mut Context<'_>) -> Poll<BufResult<usize, T>> {
use std::future::Future;
use std::pin::Pin;

let complete = ready!(Pin::new(self).poll(cx));
Poll::Ready(complete)
}
}

impl<T> Completable for Write<T>
Expand Down
19 changes: 1 addition & 18 deletions src/driver/writev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use crate::{
BufResult,
};
use libc::iovec;
use std::{
io,
task::{Context, Poll},
};
use std::io;

pub(crate) struct Writev<T> {
/// Holds a strong ref to the FD, preventing the file from being closed
Expand Down Expand Up @@ -56,20 +53,6 @@ impl<T: IoBuf> Op<Writev<T>> {
},
)
}

pub(crate) async fn writev(mut self) -> BufResult<usize, Vec<T>> {
use crate::future::poll_fn;

poll_fn(move |cx| self.poll_writev(cx)).await
}

pub(crate) fn poll_writev(&mut self, cx: &mut Context<'_>) -> Poll<BufResult<usize, Vec<T>>> {
use std::future::Future;
use std::pin::Pin;

let complete = ready!(Pin::new(self).poll(cx));
Poll::Ready(complete)
}
}

impl<T> Completable for Writev<T>
Expand Down
8 changes: 4 additions & 4 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl File {
pub async fn read_at<T: IoBufMut>(&self, buf: T, pos: u64) -> crate::BufResult<usize, T> {
// Submit the read operation
let op = Op::read_at(&self.fd, buf, pos).unwrap();
op.read().await
op.await
}

/// Read some bytes at the specified offset from the file into the specified
Expand Down Expand Up @@ -231,7 +231,7 @@ impl File {
) -> crate::BufResult<usize, Vec<T>> {
// Submit the read operation
let op = Op::readv_at(&self.fd, bufs, pos).unwrap();
op.readv().await
op.await
}

/// Write data from buffers into this file at the specified offset,
Expand Down Expand Up @@ -287,7 +287,7 @@ impl File {
pos: u64,
) -> crate::BufResult<usize, Vec<T>> {
let op = Op::writev_at(&self.fd, buf, pos).unwrap();
op.writev().await
op.await
}

/// Write a buffer into this file at the specified offset, returning how
Expand Down Expand Up @@ -338,7 +338,7 @@ impl File {
/// [`Ok(n)`]: Ok
pub async fn write_at<T: IoBuf>(&self, buf: T, pos: u64) -> crate::BufResult<usize, T> {
let op = Op::write_at(&self.fd, buf, pos).unwrap();
op.write().await
op.await
}

/// Attempts to sync all OS-internal metadata to disk.
Expand Down