Skip to main content

ringkernel_core/
backend_stub.rs

1//! Macro for generating unavailable backend stubs.
2//!
3//! When a backend feature (e.g., `cuda`, `wgpu`, `metal`) is disabled,
4//! the corresponding crate still needs to expose a stub `Runtime` type
5//! that implements `RingKernelRuntime` but returns errors for all operations.
6//!
7//! This macro eliminates the ~50 lines of identical boilerplate per backend.
8
9/// Generate a stub runtime for an unavailable backend.
10///
11/// Creates a struct that implements `RingKernelRuntime` with all methods
12/// returning `BackendUnavailable` errors.
13///
14/// # Example
15///
16/// ```ignore
17/// ringkernel_core::unavailable_backend!(CudaRuntime, Backend::Cuda, "CUDA");
18/// ```
19#[macro_export]
20macro_rules! unavailable_backend {
21    ($runtime:ident, $backend:expr, $name:expr) => {
22        /// Stub runtime when the backend feature is disabled.
23        pub struct $runtime;
24
25        impl $runtime {
26            /// Create fails when backend is not available.
27            pub async fn new() -> $crate::error::Result<Self> {
28                Err($crate::error::RingKernelError::BackendUnavailable(
29                    concat!($name, " feature not enabled").to_string(),
30                ))
31            }
32        }
33
34        #[async_trait::async_trait]
35        impl $crate::runtime::RingKernelRuntime for $runtime {
36            fn backend(&self) -> $crate::runtime::Backend {
37                $backend
38            }
39
40            fn is_backend_available(&self, _backend: $crate::runtime::Backend) -> bool {
41                false
42            }
43
44            async fn launch(
45                &self,
46                _kernel_id: &str,
47                _options: $crate::runtime::LaunchOptions,
48            ) -> $crate::error::Result<$crate::runtime::KernelHandle> {
49                Err($crate::error::RingKernelError::BackendUnavailable(
50                    $name.to_string(),
51                ))
52            }
53
54            fn get_kernel(
55                &self,
56                _kernel_id: &$crate::runtime::KernelId,
57            ) -> Option<$crate::runtime::KernelHandle> {
58                None
59            }
60
61            fn list_kernels(&self) -> Vec<$crate::runtime::KernelId> {
62                vec![]
63            }
64
65            fn metrics(&self) -> $crate::runtime::RuntimeMetrics {
66                $crate::runtime::RuntimeMetrics::default()
67            }
68
69            async fn shutdown(&self) -> $crate::error::Result<()> {
70                Ok(())
71            }
72        }
73    };
74}