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