diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-04-16 16:29:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-16 16:29:28 +0200 |
commit | 0c48470b353a721eac762003e26e5741f3bc1b5d (patch) | |
tree | 5fa4fbe6764437de93e2051b886908c4b4f78bd5 /cli/ops/tty.rs | |
parent | 5f250bb14835d0f1251b8e875a5fa431f12ef406 (diff) |
remove more calls to futures::executor::block_on (#4775)
Diffstat (limited to 'cli/ops/tty.rs')
-rw-r--r-- | cli/ops/tty.rs | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/cli/ops/tty.rs b/cli/ops/tty.rs index 461c1ca30..90fa2bde3 100644 --- a/cli/ops/tty.rs +++ b/cli/ops/tty.rs @@ -65,22 +65,41 @@ pub fn op_set_raw( use winapi::shared::minwindef::FALSE; use winapi::um::{consoleapi, handleapi}; - let state = state_.borrow_mut(); - let resource_holder = state.resource_table.get::<StreamResourceHolder>(rid); + let mut state = state_.borrow_mut(); + let resource_holder = + state.resource_table.get_mut::<StreamResourceHolder>(rid); if resource_holder.is_none() { return Err(OpError::bad_resource_id()); } + let resource_holder = resource_holder.unwrap(); // For now, only stdin. - let handle = match &resource_holder.unwrap().resource { + let handle = match &mut resource_holder.resource { StreamResource::Stdin(_, _) => std::io::stdin().as_raw_handle(), - StreamResource::FsFile(None) => { - return Err(OpError::resource_unavailable()) - } - StreamResource::FsFile(Some((f, _))) => { - let tokio_file = futures::executor::block_on(f.try_clone())?; - let std_file = futures::executor::block_on(tokio_file.into_std()); - std_file.as_raw_handle() + StreamResource::FsFile(ref mut option_file_metadata) => { + if let Some((tokio_file, metadata)) = option_file_metadata.take() { + match tokio_file.try_into_std() { + Ok(std_file) => { + let raw_handle = std_file.as_raw_handle(); + // Turn the std_file handle back into a tokio file, put it back + // in the resource table. + let tokio_file = tokio::fs::File::from_std(std_file); + resource_holder.resource = + StreamResource::FsFile(Some((tokio_file, metadata))); + // return the result. + raw_handle + } + Err(tokio_file) => { + // This function will return an error containing the file if + // some operation is in-flight. + resource_holder.resource = + StreamResource::FsFile(Some((tokio_file, metadata))); + return Err(OpError::resource_unavailable()); + } + } + } else { + return Err(OpError::resource_unavailable()); + } } _ => { return Err(OpError::bad_resource_id()); @@ -127,9 +146,7 @@ pub fn op_set_raw( (std::io::stdin().as_raw_fd(), &mut metadata.mode) } StreamResource::FsFile(Some((f, ref mut metadata))) => { - let tokio_file = futures::executor::block_on(f.try_clone())?; - let std_file = futures::executor::block_on(tokio_file.into_std()); - (std_file.as_raw_fd(), &mut metadata.tty.mode) + (f.as_raw_fd(), &mut metadata.tty.mode) } StreamResource::FsFile(None) => { return Err(OpError::resource_unavailable()) @@ -173,9 +190,7 @@ pub fn op_set_raw( (std::io::stdin().as_raw_fd(), &mut metadata.mode) } StreamResource::FsFile(Some((f, ref mut metadata))) => { - let tokio_file = futures::executor::block_on(f.try_clone())?; - let std_file = futures::executor::block_on(tokio_file.into_std()); - (std_file.as_raw_fd(), &mut metadata.tty.mode) + (f.as_raw_fd(), &mut metadata.tty.mode) } StreamResource::FsFile(None) => { return Err(OpError::resource_unavailable()); |