cel_cxx/async/
mod.rs

1//! Asynchronous runtime support for CEL.
2//!
3//! This module provides support for asynchronous evaluation of CEL expressions
4//! using the [async-std](https://github.com/async-rs/async-std) or
5//! [tokio](https://github.com/tokio-rs/tokio) runtimes.
6//!
7//! # Features
8//! - `async-std`: Enables asynchronous evaluation of CEL expressions using
9//!   [async-std](https://github.com/async-rs/async-std).
10//! - `tokio`: Enables asynchronous evaluation of CEL expressions using
11//!   [tokio](https://github.com/tokio-rs/tokio).
12//!
13
14use async_scoped::spawner::{Blocker, FuncSpawner, Spawner};
15use futures::Future;
16
17pub(crate) mod abort;
18
19/// Runtime trait for CEL asynchronous runtime.
20pub trait Runtime: 'static {
21    /// Scoped spawner for CEL asynchronous runtime.
22    type ScopedSpawner: Spawner<()>
23        + FuncSpawner<(), SpawnHandle = <Self::ScopedSpawner as Spawner<()>>::SpawnHandle>
24        + Blocker
25        + Default
26        + Send
27        + Sync
28        + 'static;
29
30    /// Blocking runner for CEL asynchronous runtime.
31    type BlockingRunner: BlockingRunner;
32}
33
34/// Blocking runner trait for CEL asynchronous runtime.
35pub trait BlockingRunner: 'static {
36    /// Block on a future.
37    fn block_on<F: Future>(fut: F) -> F::Output;
38}
39
40/// Tokio runtime for CEL asynchronous runtime.
41#[cfg(feature = "tokio")]
42#[allow(missing_debug_implementations)]
43pub enum Tokio {}
44
45/// Tokio runtime implementation.
46#[cfg(feature = "tokio")]
47pub mod tokio {
48    #![allow(missing_debug_implementations)]
49    use super::*;
50
51    impl Runtime for Tokio {
52        type ScopedSpawner = async_scoped::spawner::use_tokio::Tokio;
53        type BlockingRunner = TokioBlockingRunner;
54    }
55
56    /// Tokio blocking runner for CEL asynchronous runtime.
57    pub struct TokioBlockingRunner;
58    impl BlockingRunner for TokioBlockingRunner {
59        fn block_on<F: Future>(fut: F) -> F::Output {
60            ::tokio::runtime::Handle::current().block_on(fut)
61        }
62    }
63}
64
65/// Async-std runtime for CEL asynchronous runtime.
66#[cfg(feature = "async-std")]
67#[allow(missing_debug_implementations)]
68pub enum AsyncStd {}
69
70/// Async-std runtime implementation.
71#[cfg(feature = "async-std")]
72pub mod async_std {
73    #![allow(missing_debug_implementations)]
74    use super::*;
75
76    impl Runtime for AsyncStd {
77        type ScopedSpawner = async_scoped::spawner::use_async_std::AsyncStd;
78        type BlockingRunner = AsyncStdBlockingRunner;
79    }
80
81    /// Async-std blocking runner for CEL asynchronous runtime.
82    pub struct AsyncStdBlockingRunner;
83    impl BlockingRunner for AsyncStdBlockingRunner {
84        fn block_on<F: Future>(fut: F) -> F::Output {
85            ::async_std::task::block_on(fut)
86        }
87    }
88}