async_main/
lib.rs

1// Copyright © 2022-2023 The async_main crate contributors.
2//
3// Licensed under any of:
4// - Apache License, Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
5// - Boost Software License, Version 1.0 (https://www.boost.org/LICENSE_1_0.txt)
6// - MIT License (https://mit-license.org/)
7// At your choosing (See accompanying files LICENSE_APACHE_2_0.txt,
8// LICENSE_MIT.txt and LICENSE_BOOST_1_0.txt).
9//
10//! Runtime-agnostic async main proc macro.  Currently, this crate only supports
11//! single-threaded task pools, but in a future version will add a configuration
12//! option to enable multi-threaded task pools.
13//!
14//! # Getting Started
15//! Choose a runtime by enabling one of the following features:
16//!
17//!  - *`async-executor`*
18//!  - *`async-std`*
19//!  - *`futures`*
20//!  - *`pasts`*
21//!  - *`tokio`*
22//!
23//! ```rust
24#![doc = include_str!("../examples/main.rs")]
25//! ```
26
27#![no_std]
28#![forbid(unsafe_code)]
29#![warn(
30    anonymous_parameters,
31    missing_copy_implementations,
32    missing_debug_implementations,
33    missing_docs,
34    nonstandard_style,
35    rust_2018_idioms,
36    single_use_lifetimes,
37    trivial_casts,
38    trivial_numeric_casts,
39    unreachable_pub,
40    unused_extern_crates,
41    unused_qualifications,
42    variant_size_differences
43)]
44
45#[allow(unused_extern_crates)]
46extern crate alloc;
47
48#[cfg_attr(feature = "async-executor", path = "async_executor.rs")]
49#[cfg_attr(feature = "async-std", path = "async_std.rs")]
50#[cfg_attr(feature = "futures", path = "futures.rs")]
51#[cfg_attr(feature = "pasts", path = "pasts.rs")]
52#[cfg_attr(feature = "tokio", path = "tokio.rs")]
53mod spawn;
54
55pub use async_main_macro::async_main;
56
57pub use self::spawn::LocalSpawner;
58
59/// Implementation for spawning tasks on an executor.
60pub trait Spawn: Clone {
61    /// Spawn a [`Future`](core::future::Future) without the [`Send`]
62    /// requirement.
63    ///
64    /// This forces the executor to always run the task on the same thread that
65    /// this method is called on.
66    fn spawn_local(&self, f: impl core::future::Future<Output = ()> + 'static);
67
68    /// Spawn a [`Future`](core::future::Future) that is [`Send`].
69    ///
70    /// This allows the executor to run the task on whatever thread it
71    /// determines is most efficient.
72    #[inline(always)]
73    fn spawn(&self, f: impl core::future::Future<Output = ()> + Send + 'static) {
74        self.spawn_local(f)
75    }
76}