baobao_codegen/adapters/
async_runtime.rs

1//! Async runtime adapter abstraction.
2//!
3//! This module defines the [`RuntimeAdapter`] trait for abstracting async
4//! runtime setup (tokio, async-std, smol, etc.).
5
6use super::cli::{Dependency, ImportSpec};
7use crate::builder::CodeFragment;
8
9/// Info for generating async runtime setup.
10#[derive(Debug, Clone)]
11pub struct RuntimeInfo {
12    /// Whether the application uses async
13    pub is_async: bool,
14    /// Whether multi-threaded runtime is needed
15    pub multi_threaded: bool,
16}
17
18/// Trait for async runtime adapters.
19///
20/// Implement this trait to support a specific async runtime (tokio, async-std, etc.).
21pub trait RuntimeAdapter {
22    /// Adapter name for identification.
23    fn name(&self) -> &'static str;
24
25    /// Dependencies required by this runtime.
26    fn dependencies(&self) -> Vec<Dependency>;
27
28    /// Attribute to apply to async main function (e.g., `#[tokio::main]`).
29    fn main_attribute(&self) -> Option<String>;
30
31    /// Generate any runtime initialization code.
32    fn generate_init(&self, info: &RuntimeInfo) -> Option<Vec<CodeFragment>>;
33
34    /// Imports needed for runtime code.
35    fn imports(&self) -> Vec<ImportSpec>;
36}