1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Library that helps you to simulate exception without `panic` in async Rust.
//!
//! There is an unsync version [unsync::ExceptionContext] and a sync version [sync::ExceptionContext].
//!
//! You can also define the context as a static variable so that you don't
//! have to pass them through function argument, using [global::ExceptionContext].
//!
//! Check [this blog](https://jason5lee.me/2022/03/11/rust-exception-async/) for the main idea.
//!
//! Example:
//!
//! ```
//! type ExcptCtx = asynx::unsync::ExceptionContext<String>;
//!
//! async fn perform(ctx: &ExcptCtx, success: bool) -> String {
//! if success {
//! "success".to_string()
//! } else {
//! ctx.throw("failed".to_string()).await
//! }
//! }
//!
//! tokio_test::block_on(async {
//! let r = ExcptCtx::new()
//! .catch(|ctx| async move {
//! assert_eq!("success".to_string(), perform(ctx, true).await);
//! perform(ctx, false).await;
//! unreachable!() // The previous statement throws an exception.
//! })
//! .await;
//! assert_eq!(Err("failed".to_string()), r)
//! });
//! ```