monoio/lib.rs
1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![warn(missing_docs, unreachable_pub)]
4#![allow(stable_features)]
5#![allow(clippy::macro_metavars_in_unsafe)]
6#![cfg_attr(feature = "unstable", feature(io_error_more))]
7#![cfg_attr(feature = "unstable", feature(lazy_cell))]
8#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]
9#![cfg_attr(feature = "unstable", feature(thread_local))]
10
11#[macro_use]
12pub mod macros;
13#[cfg(feature = "macros")]
14#[doc(hidden)]
15pub use monoio_macros::select_priv_declare_output_enum;
16#[macro_use]
17mod driver;
18pub(crate) mod builder;
19#[allow(dead_code)]
20pub(crate) mod runtime;
21mod scheduler;
22pub mod time;
23
24extern crate alloc;
25
26#[cfg(feature = "sync")]
27pub mod blocking;
28
29pub mod buf;
30pub mod fs;
31pub mod io;
32pub mod net;
33pub mod task;
34pub mod utils;
35
36use std::future::Future;
37
38#[cfg(feature = "sync")]
39pub use blocking::spawn_blocking;
40pub use builder::{Buildable, RuntimeBuilder};
41pub use driver::Driver;
42#[cfg(all(target_os = "linux", feature = "iouring"))]
43pub use driver::IoUringDriver;
44#[cfg(feature = "legacy")]
45pub use driver::LegacyDriver;
46#[cfg(feature = "macros")]
47pub use monoio_macros::{main, test, test_all};
48pub use runtime::{spawn, Runtime};
49#[cfg(any(all(target_os = "linux", feature = "iouring"), feature = "legacy"))]
50pub use {builder::FusionDriver, runtime::FusionRuntime};
51
52/// Start a monoio runtime.
53///
54/// # Examples
55///
56/// Basic usage
57///
58/// ```no_run
59/// fn main() -> Result<(), Box<dyn std::error::Error>> {
60/// #[cfg(not(all(target_os = "linux", feature = "iouring")))]
61/// let r = monoio::start::<monoio::LegacyDriver, _>(async {
62/// // Open a file
63/// let file = monoio::fs::File::open("hello.txt").await?;
64///
65/// let buf = vec![0; 4096];
66/// // Read some data, the buffer is passed by ownership and
67/// // submitted to the kernel. When the operation completes,
68/// // we get the buffer back.
69/// let (res, buf) = file.read_at(buf, 0).await;
70/// let n = res?;
71///
72/// // Display the contents
73/// println!("{:?}", &buf[..n]);
74///
75/// Ok(())
76/// });
77/// #[cfg(all(target_os = "linux", feature = "iouring"))]
78/// let r = Ok(());
79/// r
80/// }
81/// ```
82pub fn start<D, F>(future: F) -> F::Output
83where
84 F: Future,
85 F::Output: 'static,
86 D: Buildable + Driver,
87{
88 let mut rt = builder::Buildable::build(builder::RuntimeBuilder::<D>::new())
89 .expect("Unable to build runtime.");
90 rt.block_on(future)
91}
92
93/// A specialized `Result` type for `io-uring` operations with buffers.
94///
95/// This type is used as a return value for asynchronous `io-uring` methods that
96/// require passing ownership of a buffer to the runtime. When the operation
97/// completes, the buffer is returned whether or not the operation completed
98/// successfully.
99///
100/// # Examples
101///
102/// ```no_run
103/// fn main() -> Result<(), Box<dyn std::error::Error>> {
104/// #[cfg(not(all(target_os = "linux", feature = "iouring")))]
105/// let r = monoio::start::<monoio::LegacyDriver, _>(async {
106/// // Open a file
107/// let file = monoio::fs::File::open("hello.txt").await?;
108///
109/// let buf = vec![0; 4096];
110/// // Read some data, the buffer is passed by ownership and
111/// // submitted to the kernel. When the operation completes,
112/// // we get the buffer back.
113/// let (res, buf) = file.read_at(buf, 0).await;
114/// let n = res?;
115///
116/// // Display the contents
117/// println!("{:?}", &buf[..n]);
118///
119/// Ok(())
120/// });
121/// #[cfg(all(target_os = "linux", feature = "iouring"))]
122/// let r = Ok(());
123/// r
124/// }
125/// ```
126pub type BufResult<T, B> = (std::io::Result<T>, B);