Skip to main content

gpu_handle_types/
external_queue.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3#[cfg(feature = "opencl")]
4use crate::ClQueueRef;
5#[cfg(feature = "cuda")]
6use crate::CudaStreamRef;
7use crate::{BackendKind, MtlCommandBufferRef};
8
9/// Borrowed handle into an external command queue. The `'a` lifetime
10/// ties the queue to the caller's scope so the borrow-checker prevents
11/// reentrant use across the call it is lent to.
12///
13/// The payloads are the typed handle newtypes re-exported at the crate
14/// root — [`MtlCommandBufferRef`] and, under the matching feature,
15/// `ClQueueRef` / `CudaStreamRef` — rather than bare `*mut c_void`s.
16/// Construction of each newtype happens once at the call-site boundary
17/// via its `try_from_raw(unsafe { ... })`; pattern-match arms inside
18/// backends are safe code.
19///
20/// `_Phantom` variant exists so `'a` remains load-bearing under
21/// `--no-default-features` (where every other typed variant is cfg'd out).
22#[non_exhaustive]
23pub enum ExternalQueue<'a> {
24    #[cfg(feature = "wgpu")]
25    WgpuEncoder(&'a mut wgpu::CommandEncoder),
26    #[cfg(feature = "opencl")]
27    OpenClQueue(&'a ClQueueRef),
28    #[cfg(feature = "cuda")]
29    CudaStream(&'a CudaStreamRef),
30    MetalCommandBuffer(&'a MtlCommandBufferRef),
31    Cpu,
32    _Phantom(std::marker::PhantomData<&'a ()>),
33}
34
35impl ExternalQueue<'_> {
36    pub fn backend(&self) -> BackendKind {
37        match self {
38            Self::Cpu => BackendKind::Cpu,
39            #[cfg(feature = "wgpu")]
40            Self::WgpuEncoder(_) => BackendKind::Wgpu,
41            #[cfg(feature = "opencl")]
42            Self::OpenClQueue(_) => BackendKind::OpenCl,
43            #[cfg(feature = "cuda")]
44            Self::CudaStream(_) => BackendKind::Cuda,
45            #[cfg(feature = "wgpu")]
46            Self::MetalCommandBuffer(_) => BackendKind::Wgpu,
47            #[cfg(not(feature = "wgpu"))]
48            Self::MetalCommandBuffer(_) => BackendKind::Cpu,
49            Self::_Phantom(_) => BackendKind::Cpu,
50        }
51    }
52}
53
54impl core::fmt::Debug for ExternalQueue<'_> {
55    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
56        f.write_str("ExternalQueue")
57    }
58}