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

Clean up how we use opIds #4118

Merged
merged 19 commits into from
Feb 25, 2020
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
ry committed Feb 25, 2020
commit 2735e6bf512478b38c0ea31de32517b66b31548d
73 changes: 23 additions & 50 deletions cli/ops/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,6 @@ enum IoState {
Done,
}

/// Tries to read some bytes directly into the given `buf` in asynchronous
/// manner, returning a future type.
///
/// The returned future will resolve to both the I/O stream and the buffer
/// as well as the number of bytes read once the read operation is completed.
pub fn read<T>(state: &State, rid: ResourceId, buf: T) -> Read<T>
where
T: AsMut<[u8]>,
{
Read {
rid,
buf,
io_state: IoState::Pending,
state: state.clone(),
}
}

/// A future which can be used to easily read available number of bytes to fill
/// a buffer.
///
Expand Down Expand Up @@ -185,13 +168,17 @@ pub fn op_read(
zero_copy: Option<ZeroCopyBuf>,
) -> Pin<Box<MinimalOp>> {
debug!("read rid={}", rid);
let zero_copy = match zero_copy {
None => return futures::future::err(no_buffer_specified()).boxed_local(),
Some(buf) => buf,
};

let fut = read(state, rid as u32, zero_copy);
fut.boxed_local()
if zero_copy.is_none() {
return futures::future::err(no_buffer_specified()).boxed_local();
}
// TODO(ry) Probably poll_fn can be used here and the Read struct eliminated.
Read {
rid: rid as u32,
buf: zero_copy.unwrap(),
io_state: IoState::Pending,
state: state.clone(),
}
.boxed_local()
}

/// `DenoAsyncWrite` is the same as the `tokio_io::AsyncWrite` trait
Expand Down Expand Up @@ -261,24 +248,6 @@ pub struct Write<T> {
nwritten: i32,
}

/// Creates a future that will write some of the buffer `buf` to
/// the stream resource with `rid`.
///
/// Any error which happens during writing will cause both the stream and the
/// buffer to get destroyed.
pub fn write<T>(state: &State, rid: ResourceId, buf: T) -> Write<T>
where
T: AsRef<[u8]>,
{
Write {
rid,
buf,
io_state: IoState::Pending,
state: state.clone(),
nwritten: 0,
}
}

/// This is almost the same implementation as in tokio, difference is
/// that error type is `OpError` instead of `std::io::Error`.
impl<T> Future for Write<T>
Expand Down Expand Up @@ -329,12 +298,16 @@ pub fn op_write(
zero_copy: Option<ZeroCopyBuf>,
) -> Pin<Box<MinimalOp>> {
debug!("write rid={}", rid);
let zero_copy = match zero_copy {
None => return futures::future::err(no_buffer_specified()).boxed_local(),
Some(buf) => buf,
};

let fut = write(state, rid as u32, zero_copy);

fut.boxed_local()
if zero_copy.is_none() {
return futures::future::err(no_buffer_specified()).boxed_local();
}
// TODO(ry) Probably poll_fn can be used here and the Write struct eliminated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed 👍

Write {
rid: rid as u32,
buf: zero_copy.unwrap(),
io_state: IoState::Pending,
state: state.clone(),
nwritten: 0,
}
.boxed_local()
}