some_executor 0.7.2

A trait for libraries that abstract over any executor
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0
//! `#[some_executor::main]` on a real entry point.
//!
//! `harness = false`, because the macro *generates* `fn main` — which is the
//! only way to check the thing that actually matters, that the expansion links
//! and runs as a program's entry point rather than merely compiling.
//!
//! # Why this is native-only
//!
//! On wasm32 the test binary's entry point belongs to the `wasm_lite` runner
//! (`wasm_lite::test_main!()`), and a second generated `fn main` in the same
//! binary is a conflict rather than a test. The issue that asked for this macro
//! flagged that as the most likely source of surprise, and it is: the two
//! entry-point mechanisms cannot share a binary.
//!
//! What is checked on wasm32 instead is that the expansion *compiles* there —
//! `examples/main_macro_default.rs` is built by `scripts/wasm32/check`, which
//! covers `--all-targets`. Running it needs a backend that hands its future to
//! the event loop, which is a property of the backend rather than of this
//! macro.
//!
//! The gate is per-item rather than a file-level `#![cfg]`, because
//! `harness = false` means this file owns `fn main` -- and a file-level gate
//! would delete it along with everything else, so the target would not link.
//! The stub at the bottom is the other half.

#[cfg(not(target_arch = "wasm32"))]
use std::sync::atomic::{AtomicUsize, Ordering};

#[cfg(not(target_arch = "wasm32"))]
static RAN: AtomicUsize = AtomicUsize::new(0);

/// No backend argument, so this is `LastResort` — the case the review settled
/// on for quick demos.
#[cfg(not(target_arch = "wasm32"))]
#[some_executor::main]
async fn main() {
    RAN.fetch_add(1, Ordering::Relaxed);

    // The `ExecutorMain` contract says the implementation installs itself, so
    // that anything anywhere in the program can spawn. That is the part a
    // caller depends on and the part a wrong expansion would silently skip.
    let installed = some_executor::global_executor::global_executor(|e| e.is_some());
    assert!(
        installed,
        "the backend must install itself globally before running the main future"
    );

    // And it must actually be *driving*: a detached task only completes if
    // something is polling it.
    let (sender, receiver) = r#continue::continuation();
    some_executor::task::Task::without_notifications(
        "main_macro spawned".to_string(),
        some_executor::task::Configuration::default(),
        async move {
            RAN.fetch_add(1, Ordering::Relaxed);
            sender.send(());
        },
    )
    .spawn_static_current();
    receiver.await;

    assert_eq!(
        RAN.load(Ordering::Relaxed),
        2,
        "the main future and the task it spawned both ran"
    );

    // The generated `fn main` is an ordinary `fn main`, so this is how the
    // process reports success.
    println!("some_executor::main expansion: OK");
}

/// Nothing to run in a browser, but `harness = false` still requires the target
/// to link.
#[cfg(target_arch = "wasm32")]
fn main() {}