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