use std::num::ParseIntError;
use std::process::ExitCode;
use go_lib::{chan::chan, go, sync::WaitGroup};
use std::sync::Arc;
#[allow(dead_code)]
fn plain() {
#[go_lib::run]
fn _inner() {
let (tx, rx) = chan::<&str>(0);
go!(move || tx.send("hello from #[go_lib::run]"));
println!("{}", rx.recv().unwrap());
}
_inner();
}
#[allow(dead_code)]
fn with_exitcode() -> ExitCode {
#[go_lib::run]
fn _inner() -> ExitCode {
const N: usize = 4;
let (tx, rx) = chan::<bool>(N);
let wg = Arc::new(WaitGroup::new());
for id in 0..N {
let (tx, wg2) = (tx.clone(), Arc::clone(&wg));
wg.add(1);
go!(move || {
tx.send(id % 2 == 0); wg2.done();
});
}
wg.wait();
let failures = (0..N)
.filter(|_| rx.recv() == Some(false))
.count();
println!("{}/{N} tasks passed", N - failures);
if failures == 0 { ExitCode::SUCCESS } else { ExitCode::FAILURE }
}
_inner()
}
#[allow(dead_code)]
fn with_result() -> Result<(), ParseIntError> {
#[go_lib::run]
fn _inner() -> Result<(), ParseIntError> {
let inputs = ["1", "2", "3", "4"];
let (tx, rx) = chan::<Result<i64, ParseIntError>>(inputs.len());
for s in inputs {
let tx = tx.clone();
go!(move || tx.send(s.parse::<i64>()));
}
let mut sum = 0_i64;
for _ in 0..inputs.len() {
sum += rx.recv().unwrap()?; }
println!("sum = {sum}"); Ok(())
}
_inner()
}
fn main() {
println!("── plain:");
plain();
println!("── with ExitCode:");
let code = with_exitcode();
println!(" ExitCode = {code:?}");
println!("── with Result:");
match with_result() {
Ok(()) => println!(" Ok(())"),
Err(e) => println!(" Err({e})"),
}
}