1 // Copyright 2021 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 pub mod async_types;
6 pub mod event;
7 pub mod executor;
8 pub mod fd_executor;
9 pub mod poll_source;
10 pub mod uring_executor;
11 pub mod uring_source;
12 pub use fd_executor::FdExecutor;
13 pub use fd_executor::FdExecutorTaskHandle;
14 pub use poll_source::Error as PollSourceError;
15 pub use poll_source::PollSource;
16 pub use uring_executor::URingExecutor;
17 pub use uring_executor::UringExecutorTaskHandle;
18 pub use uring_source::UringSource;
19 mod timer;
20
21 use std::future::Future;
22
23 use crate::AsyncError;
24 use crate::Error;
25 use crate::Executor;
26 use crate::Result;
27
28 /// Creates a URingExecutor that runs one future to completion.
29 ///
30 /// # Example
31 ///
32 /// ```
33 /// use cros_async::sys::unix::run_one_uring;
34 ///
35 /// let fut = async { 55 };
36 /// assert_eq!(55, run_one_uring(fut).unwrap());
37 /// ```
run_one_uring<F: Future>(fut: F) -> Result<F::Output>38 pub fn run_one_uring<F: Future>(fut: F) -> Result<F::Output> {
39 URingExecutor::new()
40 .and_then(|ex| ex.run_until(fut))
41 .map_err(Error::URingExecutor)
42 }
43
44 /// Creates a FdExecutor that runs one future to completion.
45 ///
46 /// # Example
47 ///
48 /// ```
49 /// use cros_async::sys::unix::run_one_poll;
50 ///
51 /// let fut = async { 55 };
52 /// assert_eq!(55, run_one_poll(fut).unwrap());
53 /// ```
run_one_poll<F: Future>(fut: F) -> Result<F::Output>54 pub fn run_one_poll<F: Future>(fut: F) -> Result<F::Output> {
55 FdExecutor::new()
56 .and_then(|ex| ex.run_until(fut))
57 .map_err(|e| Error::PollSource(PollSourceError::Executor(e)))
58 }
59
60 /// Creates an Executor that runs one future to completion.
61 ///
62 /// # Example
63 ///
64 /// ```
65 /// use cros_async::sys::unix::run_one;
66 ///
67 /// let fut = async { 55 };
68 /// assert_eq!(55, run_one(fut).unwrap());
69 /// ```
run_one<F: Future>(fut: F) -> Result<F::Output>70 pub fn run_one<F: Future>(fut: F) -> Result<F::Output> {
71 Executor::new()
72 .and_then(|ex| ex.run_until(fut))
73 .map_err(|e| match e {
74 AsyncError::EventAsync(e) => Error::EventAsync(e),
75 AsyncError::Uring(e) => Error::URingExecutor(e),
76 AsyncError::Poll(e) => Error::PollSource(e),
77 })
78 }
79
80 impl From<Error> for std::io::Error {
from(e: Error) -> Self81 fn from(e: Error) -> Self {
82 use Error::*;
83 match e {
84 EventAsync(e) => e.into(),
85 URingExecutor(e) => e.into(),
86 PollSource(e) => e.into(),
87 Timer(e) => e.into(),
88 TimerAsync(e) => e.into(),
89 }
90 }
91 }
92