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