fluke_maybe_uring/
lib.rs

1#![allow(incomplete_features)]
2#![feature(async_fn_in_trait)]
3
4use std::future::Future;
5
6pub mod buf;
7
8#[cfg(all(target_os = "linux", feature = "tokio-uring"))]
9pub use tokio_uring;
10
11#[cfg(all(target_os = "linux", feature = "tokio-uring"))]
12mod compat;
13
14#[cfg(feature = "net")]
15pub mod net;
16
17pub mod io;
18
19pub type BufResult<T, B> = (std::io::Result<T>, B);
20
21/// Spawns a new asynchronous task, returning a [`JoinHandle`] for it.
22///
23/// Spawning a task enables the task to execute concurrently to other tasks.
24/// There is no guarantee that a spawned task will execute to completion. When a
25/// runtime is shutdown, all outstanding tasks are dropped, regardless of the
26/// lifecycle of that task.
27///
28/// This function must be called from the context of a `tokio-uring` runtime,
29/// or a tokio local set (at the time of this writing, they're the same thing).
30pub fn spawn<T: Future + 'static>(task: T) -> tokio::task::JoinHandle<T::Output> {
31    tokio::task::spawn_local(task)
32}
33
34/// Equivalent to `tokio_uring::start`
35#[cfg(all(target_os = "linux", feature = "tokio-uring"))]
36pub fn start<F: Future>(task: F) -> F::Output {
37    tokio_uring::start(task)
38}
39
40/// Equivalent to `tokio_uring::start`
41#[cfg(not(all(target_os = "linux", feature = "tokio-uring")))]
42pub fn start<F: Future>(task: F) -> F::Output {
43    use tokio::task::LocalSet;
44
45    tokio::runtime::Builder::new_current_thread()
46        .enable_all()
47        .build()
48        .unwrap()
49        .block_on(async move {
50            let local = LocalSet::new();
51            local.run_until(task).await
52        })
53}