use flowync::{error::Cause, Flower};
use std::{io::Error, time::Instant};
type TestTokioFlower = Flower<String, u32>;
#[tokio::main]
async fn main() {
let instant: Instant = Instant::now();
let flower: TestTokioFlower = Flower::new(1);
tokio::spawn({
let this = flower.handle();
this.activate();
async move {
let id = this.id();
let result =
Ok::<String, Error>(format!("the flower with id: {} is flowing", id).into());
match result {
Ok(value) => {
this.send_async(value).await;
}
Err(e) => {
return this.error_verbose(e.into());
}
}
if this.should_cancel() {
let value = format!("canceling the flower with id: {}", id);
this.send_async(value).await;
return this.error(format!("the flower with id: {} canceled", id));
}
this.success(instant.elapsed().subsec_micros());
}
});
let mut done = false;
loop {
if flower.is_active() {
flower
.extract(|value| println!("{}", value))
.finalize(|result| {
match result {
Ok(elapsed) => println!(
"the flower with id: {} finished in: {:?} microseconds \n",
flower.id(),
elapsed
),
Err(Cause::Suppose(msg)) => {
println!("{}", msg)
}
Err(Cause::Panicked(msg)) => {
println!("{}", msg)
}
}
done = true;
});
}
if done {
break;
}
}
}