From 82ce7f3131ea65057bed8739adbb2b486ef0a685 Mon Sep 17 00:00:00 2001 From: Frederick Mayle Date: Mon, 8 May 2023 15:22:00 -0700 Subject: [PATCH] cros_async: delete run_one It isn't widely used, probably because it isn't very useful: you need a reference to the Executor to do IO. The same argument could be made for `block_on`, but it is more widely used in tests. Change-Id: I3e7b5783866d42a18103025f85912a9abeab5327 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4513228 Commit-Queue: Frederick Mayle Reviewed-by: Daniel Verkamp --- cros_async/src/lib.rs | 65 ++++++++++++-------------- cros_async/src/sys.rs | 1 - cros_async/src/sys/unix.rs | 57 ---------------------- cros_async/src/sys/unix/fd_executor.rs | 5 +- cros_async/src/sys/windows.rs | 24 ---------- 5 files changed, 34 insertions(+), 118 deletions(-) diff --git a/cros_async/src/lib.rs b/cros_async/src/lib.rs index 94707f4d4b..e4979228af 100644 --- a/cros_async/src/lib.rs +++ b/cros_async/src/lib.rs @@ -13,10 +13,6 @@ //! //! Use helper functions based the desired behavior of your application. //! -//! ## Running one future. -//! -//! If there is only one top-level future to run, use the [`run_one`](fn.run_one.html) function. -//! //! ## Completing one of several futures. //! //! If there are several top level tasks that should run until any one completes, use the "select" @@ -104,7 +100,6 @@ pub use mem::MemRegion; pub use mem::VecIoWrapper; use remain::sorted; pub use select::SelectResult; -pub use sys::run_one; use thiserror::Error as ThisError; pub use timer::TimerAsync; @@ -168,7 +163,7 @@ impl DetachedTasks { /// # Example /// /// ``` -/// use cros_async::{SelectResult, select2, run_one}; +/// use cros_async::{SelectResult, select2, block_on}; /// use futures::future::pending; /// use futures::pin_mut; /// @@ -176,8 +171,8 @@ impl DetachedTasks { /// let second = async {let () = pending().await;}; /// pin_mut!(first); /// pin_mut!(second); -/// match run_one(select2(first, second)) { -/// Ok((SelectResult::Finished(5), SelectResult::Pending(_second))) => (), +/// match block_on(select2(first, second)) { +/// (SelectResult::Finished(5), SelectResult::Pending(_second)) => (), /// _ => panic!("Select didn't return the first future"), /// }; /// ``` @@ -194,7 +189,7 @@ pub async fn select2( /// # Example /// /// ``` -/// use cros_async::{SelectResult, select3, run_one}; +/// use cros_async::{SelectResult, select3, block_on}; /// use futures::future::pending; /// use futures::pin_mut; /// @@ -204,10 +199,10 @@ pub async fn select2( /// pin_mut!(first); /// pin_mut!(second); /// pin_mut!(third); -/// match run_one(select3(first, second, third)) { -/// Ok((SelectResult::Finished(4), -/// SelectResult::Pending(_second), -/// SelectResult::Finished(5))) => (), +/// match block_on(select3(first, second, third)) { +/// (SelectResult::Finished(4), +/// SelectResult::Pending(_second), +/// SelectResult::Finished(5)) => (), /// _ => panic!("Select didn't return the futures"), /// }; /// ``` @@ -225,7 +220,7 @@ pub async fn select3 /// # Example /// /// ``` -/// use cros_async::{SelectResult, select4, run_one}; +/// use cros_async::{SelectResult, select4, block_on}; /// use futures::future::pending; /// use futures::pin_mut; /// @@ -237,9 +232,9 @@ pub async fn select3 /// pin_mut!(second); /// pin_mut!(third); /// pin_mut!(fourth); -/// match run_one(select4(first, second, third, fourth)) { -/// Ok((SelectResult::Finished(4), SelectResult::Pending(_second), -/// SelectResult::Finished(5), SelectResult::Pending(_fourth))) => (), +/// match block_on(select4(first, second, third, fourth)) { +/// (SelectResult::Finished(4), SelectResult::Pending(_second), +/// SelectResult::Finished(5), SelectResult::Pending(_fourth)) => (), /// _ => panic!("Select didn't return the futures"), /// }; /// ``` @@ -268,7 +263,7 @@ pub async fn select4< /// # Example /// /// ``` -/// use cros_async::{SelectResult, select5, run_one}; +/// use cros_async::{SelectResult, select5, block_on}; /// use futures::future::pending; /// use futures::pin_mut; /// @@ -282,10 +277,10 @@ pub async fn select4< /// pin_mut!(third); /// pin_mut!(fourth); /// pin_mut!(fifth); -/// match run_one(select5(first, second, third, fourth, fifth)) { -/// Ok((SelectResult::Finished(4), SelectResult::Pending(_second), -/// SelectResult::Finished(5), SelectResult::Pending(_fourth), -/// SelectResult::Finished(6))) => (), +/// match block_on(select5(first, second, third, fourth, fifth)) { +/// (SelectResult::Finished(4), SelectResult::Pending(_second), +/// SelectResult::Finished(5), SelectResult::Pending(_fourth), +/// SelectResult::Finished(6)) => (), /// _ => panic!("Select didn't return the futures"), /// }; /// ``` @@ -317,7 +312,7 @@ pub async fn select5< /// # Example /// /// ``` -/// use cros_async::{SelectResult, select6, run_one}; +/// use cros_async::{SelectResult, select6, block_on}; /// use futures::future::pending; /// use futures::pin_mut; /// @@ -333,10 +328,10 @@ pub async fn select5< /// pin_mut!(fourth); /// pin_mut!(fifth); /// pin_mut!(sixth); -/// match run_one(select6(first, second, third, fourth, fifth, sixth)) { -/// Ok((SelectResult::Finished(1), SelectResult::Pending(_second), -/// SelectResult::Finished(3), SelectResult::Pending(_fourth), -/// SelectResult::Finished(5), SelectResult::Finished(6))) => (), +/// match block_on(select6(first, second, third, fourth, fifth, sixth)) { +/// (SelectResult::Finished(1), SelectResult::Pending(_second), +/// SelectResult::Finished(3), SelectResult::Pending(_fourth), +/// SelectResult::Finished(5), SelectResult::Finished(6)) => (), /// _ => panic!("Select didn't return the futures"), /// }; /// ``` @@ -586,11 +581,11 @@ pub async fn select12< /// # Example /// /// ``` -/// use cros_async::{complete2, run_one}; +/// use cros_async::{complete2, block_on}; /// /// let first = async {5}; /// let second = async {6}; -/// assert_eq!(run_one(complete2(first, second)).unwrap_or((0,0)), (5,6)); +/// assert_eq!(block_on(complete2(first, second)), (5,6)); /// ``` pub async fn complete2(f1: F1, f2: F2) -> (F1::Output, F2::Output) where @@ -606,12 +601,12 @@ where /// # Example /// /// ``` -/// use cros_async::{complete3, run_one}; +/// use cros_async::{complete3, block_on}; /// /// let first = async {5}; /// let second = async {6}; /// let third = async {7}; -/// assert_eq!(run_one(complete3(first, second, third)).unwrap_or((0,0,0)), (5,6,7)); +/// assert_eq!(block_on(complete3(first, second, third)), (5,6,7)); /// ``` pub async fn complete3(f1: F1, f2: F2, f3: F3) -> (F1::Output, F2::Output, F3::Output) where @@ -628,13 +623,13 @@ where /// # Example /// /// ``` -/// use cros_async::{complete4, run_one}; +/// use cros_async::{complete4, block_on}; /// /// let first = async {5}; /// let second = async {6}; /// let third = async {7}; /// let fourth = async {8}; -/// assert_eq!(run_one(complete4(first, second, third, fourth)).unwrap_or((0,0,0,0)), (5,6,7,8)); +/// assert_eq!(block_on(complete4(first, second, third, fourth)), (5,6,7,8)); /// ``` pub async fn complete4( f1: F1, @@ -657,14 +652,14 @@ where /// # Example /// /// ``` -/// use cros_async::{complete5, run_one}; +/// use cros_async::{complete5, block_on}; /// /// let first = async {5}; /// let second = async {6}; /// let third = async {7}; /// let fourth = async {8}; /// let fifth = async {9}; -/// assert_eq!(run_one(complete5(first, second, third, fourth, fifth)).unwrap_or((0,0,0,0,0)), +/// assert_eq!(block_on(complete5(first, second, third, fourth, fifth)), /// (5,6,7,8,9)); /// ``` pub async fn complete5( diff --git a/cros_async/src/sys.rs b/cros_async/src/sys.rs index 8ee6654f3b..7e613f529e 100644 --- a/cros_async/src/sys.rs +++ b/cros_async/src/sys.rs @@ -18,4 +18,3 @@ pub use platform::executor::Executor; pub use platform::executor::ExecutorKind; pub use platform::executor::SetDefaultExecutorKindError; pub use platform::executor::TaskHandle; -pub use platform::run_one; diff --git a/cros_async/src/sys/unix.rs b/cros_async/src/sys/unix.rs index e2c7cbc3dd..0de6e33264 100644 --- a/cros_async/src/sys/unix.rs +++ b/cros_async/src/sys/unix.rs @@ -18,64 +18,7 @@ pub use uring_executor::UringExecutorTaskHandle; pub use uring_source::UringSource; mod timer; -use std::future::Future; - -use crate::AsyncError; use crate::Error; -use crate::Executor; -use crate::Result; - -/// Creates a URingExecutor that runs one future to completion. -/// -/// # Example -/// -/// ```no_run -/// use cros_async::sys::unix::run_one_uring; -/// -/// let fut = async { 55 }; -/// assert_eq!(55, run_one_uring(fut).unwrap()); -/// ``` -pub fn run_one_uring(fut: F) -> Result { - URingExecutor::new() - .and_then(|ex| ex.run_until(fut)) - .map_err(Error::URingExecutor) -} - -/// Creates a FdExecutor that runs one future to completion. -/// -/// # Example -/// -/// ``` -/// use cros_async::sys::unix::run_one_poll; -/// -/// let fut = async { 55 }; -/// assert_eq!(55, run_one_poll(fut).unwrap()); -/// ``` -pub fn run_one_poll(fut: F) -> Result { - FdExecutor::new() - .and_then(|ex| ex.run_until(fut)) - .map_err(|e| Error::PollSource(PollSourceError::Executor(e))) -} - -/// Creates an Executor that runs one future to completion. -/// -/// # Example -/// -/// ``` -/// use cros_async::sys::unix::run_one; -/// -/// let fut = async { 55 }; -/// assert_eq!(55, run_one(fut).unwrap()); -/// ``` -pub fn run_one(fut: F) -> Result { - Executor::new() - .and_then(|ex| ex.run_until(fut)) - .map_err(|e| match e { - AsyncError::EventAsync(e) => Error::EventAsync(e), - AsyncError::Uring(e) => Error::URingExecutor(e), - AsyncError::Poll(e) => Error::PollSource(e), - }) -} impl From for std::io::Error { fn from(e: Error) -> Self { diff --git a/cros_async/src/sys/unix/fd_executor.rs b/cros_async/src/sys/unix/fd_executor.rs index 50a2cb4dea..5b5d72a796 100644 --- a/cros_async/src/sys/unix/fd_executor.rs +++ b/cros_async/src/sys/unix/fd_executor.rs @@ -617,7 +617,10 @@ mod test { } let x = Rc::new(RefCell::new(0)); - super::super::run_one_poll(my_async(x.clone())).unwrap(); + { + let ex = FdExecutor::new().unwrap(); + ex.run_until(my_async(x.clone())).unwrap(); + } assert_eq!(*x.borrow(), 4); } diff --git a/cros_async/src/sys/windows.rs b/cros_async/src/sys/windows.rs index 55e4726818..8732724748 100644 --- a/cros_async/src/sys/windows.rs +++ b/cros_async/src/sys/windows.rs @@ -11,7 +11,6 @@ mod io_completion_port; pub mod overlapped_source; mod timer; pub mod wait_for_handle; -use std::future::Future; pub use handle_executor::HandleExecutor; pub use handle_executor::HandleExecutorTaskHandle; @@ -21,29 +20,6 @@ pub use overlapped_source::OverlappedSource; pub(crate) use wait_for_handle::WaitForHandle; use crate::Error; -use crate::Result; - -pub fn run_one_handle(fut: F) -> Result { - let ex = HandleExecutor::new().map_err(Error::HandleExecutor)?; - ex.run_until(fut).map_err(Error::HandleExecutor) -} - -/// Creates an Executor that runs one future to completion. -/// -/// # Example -/// -/// ``` -/// #[cfg(unix)] -/// { -/// use cros_async::run_one; -/// -/// let fut = async { 55 }; -/// assert_eq!(55, run_one(fut).unwrap()); -/// } -/// ``` -pub fn run_one(fut: F) -> Result { - run_one_handle(fut) -} impl From for std::io::Error { fn from(e: Error) -> Self {