alef 0.83.0

Opinionated polyglot binding generator for Rust libraries
Documentation
/// Wrapper for {{ service_name }} service instance.
/// Holds the inner service in a blocking mutex to allow mutable access
/// across FFI boundaries.
pub struct {{ service_name }} {
    pub inner: tokio::sync::Mutex<Option<{{ service_path }}>>,
}

impl {{ service_name }} {
    /// Create a new service instance.
    pub fn new() -> Self {
        Self {
            inner: tokio::sync::Mutex::new(Some({{ service_path }}::{{ constructor }}())),
        }
    }

    /// Configure the service.
    pub fn config(&mut self) {
        // Placeholder for future configuration.
    }

    /// Run the service (blocking, drives the Tokio runtime).
    ///
    /// Returns an empty string on success or the error message.
    pub fn run(&mut self) -> String {
        // ~keep 16 MiB: tokio's ~2 MB default worker stack can overflow on a deep extraction
        // future (a nested archive member, a multi-stage OCR pipeline), and a stack overflow
        // aborts the process with SIGBUS instead of raising a catchable panic.
        const SERVICE_RUNTIME_STACK_SIZE_BYTES: usize = 16 * 1024 * 1024;
        let rt = match tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .thread_stack_size(SERVICE_RUNTIME_STACK_SIZE_BYTES)
            .build()
        {
            Ok(rt) => rt,
            Err(e) => return format!("runtime error: {:?}", e),
        };
        rt.block_on(async {
            let mut guard = self.inner.lock().await;
            if let Some(app) = guard.take() {
                match app.run().await {
                    Ok(()) => String::new(),
                    Err(e) => format!("{:?}", e),
                }
            } else {
                "service already consumed".to_string()
            }
        })
    }
}