thag_rs 0.2.1

A versatile cross-platform playground and REPL for Rust snippets, expressions and programs. Accepts a script file or dynamic options.
Documentation
/*[toml]
[features]
# Use a spinlock internally (may be faster on some platforms)
spin = []
select = []
async = ["futures-sink", "futures-core"]
eventual-fairness = ["select", "nanorand"]
default = ["async", "select", "eventual-fairness", "profiling"]
profiling = ["thag_profiler/full_profiling"]

[dependencies]
async-std = { version = "1.13.0", features = ["attributes"] }
spin1 = { package = "spin", version = "0.9.8", features = ["mutex"] }
futures-sink = { version = "0.3", default-features = false, optional = true }
futures-core = { version = "0.3", default-features = false, optional = true }
nanorand = { version = "0.7", features = ["getrandom"], optional = true }
thag_profiler = { version = "0.1, thag-auto", features = ["full_profiling"] }
*/

/// Published example from the `flume` channel crate.
/// Must be run with --multimain (-m) option to allow multiple main methods.
///
/// Refactored and profiled to test and demonstrate profiling of non-tokio
/// async functions with `thag_profiler`.
//# Purpose: demo and test profiling of non-tokio async functions with `thag_profiler`.
//# Categories: async, crates, proc_macros, profiling, technique
use flume;

use thag_profiler::{self, enable_profiling, end, profile, profiled};

#[cfg(feature = "async")]
#[async_std::main]
#[enable_profiling(runtime)]
async fn main() {
    // Check if profiling is enabled
    println!(
        "is_profiling_enabled() = {}",
        thag_profiler::is_profiling_enabled()
    );

    if cfg!(feature = "profiling") {
        println!("Profiling feature is enabled");
    } else {
        println!("Profiling feature is disabled");
    }

    let _ = perform().await;
}

#[profiled]
async fn perform() {
    let (tx, rx) = flume::bounded(1);

    // #[cfg(feature = "profiling")]
    profile!(async_operation, time, mem_summary, async_fn);

    let t = async_std::task::spawn(async move {
        while let Ok(msg) = rx.recv_async().await {
            println!("Received: {}", msg);
        }
    });

    // #[cfg(feature = "profiling")]
    end!(async_operation);

    // #[cfg(feature = "profiling")]
    profile!(send_async, time, mem_detail, async_fn);

    tx.send_async("Hello, world!").await.unwrap();
    tx.send_async("How are you today?").await.unwrap();

    // #[cfg(feature = "profiling")]
    end!(send_async);

    drop(tx);

    t.await;
}

#[cfg(not(feature = "async"))]
#[enable_profiling]
fn main() {
    println!(r#"Run with flume "async" feature activated in toml block to enable this demo"#);
}