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 <fmayle@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Frederick Mayle 2023-05-08 15:22:00 -07:00 committed by crosvm LUCI
parent 60392a2483
commit 82ce7f3131
5 changed files with 34 additions and 118 deletions

View file

@ -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<F1: Future + Unpin, F2: Future + Unpin>(
/// # 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<F1: Future + Unpin, F2: Future + Unpin>(
/// 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<F1: Future + Unpin, F2: Future + Unpin, F3: Future + Unpin>
/// # 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<F1: Future + Unpin, F2: Future + Unpin, F3: Future + Unpin>
/// 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, F2>(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, F2, F3>(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, F2, F3, F4>(
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<F1, F2, F3, F4, F5>(

View file

@ -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;

View file

@ -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<F: Future>(fut: F) -> Result<F::Output> {
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<F: Future>(fut: F) -> Result<F::Output> {
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<F: Future>(fut: F) -> Result<F::Output> {
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<Error> for std::io::Error {
fn from(e: Error) -> Self {

View file

@ -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);
}

View file

@ -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<F: Future>(fut: F) -> Result<F::Output> {
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<F: Future>(fut: F) -> Result<F::Output> {
run_one_handle(fut)
}
impl From<Error> for std::io::Error {
fn from(e: Error) -> Self {