gpu-handle-types 0.2.0

Typed, owned native GPU resource handles (Vulkan, D3D11/12, Metal, OpenGL, CUDA, OpenCL, DMA-BUF, IOSurface, AHardwareBuffer, WebGPU, ...), cross-API sync points and video pixel formats, for passing GPU resources between libraries.
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0

#[cfg(feature = "opencl")]
use crate::ClQueueRef;
#[cfg(feature = "cuda")]
use crate::CudaStreamRef;
use crate::{BackendKind, MtlCommandBufferRef};

/// Borrowed handle into an external command queue. The `'a` lifetime
/// ties the queue to the caller's scope so the borrow-checker prevents
/// reentrant use across the call it is lent to.
///
/// The payloads are the typed handle newtypes re-exported at the crate
/// root — [`MtlCommandBufferRef`] and, under the matching feature,
/// `ClQueueRef` / `CudaStreamRef` — rather than bare `*mut c_void`s.
/// Construction of each newtype happens once at the call-site boundary
/// via its `try_from_raw(unsafe { ... })`; pattern-match arms inside
/// backends are safe code.
///
/// `_Phantom` variant exists so `'a` remains load-bearing under
/// `--no-default-features` (where every other typed variant is cfg'd out).
#[non_exhaustive]
pub enum ExternalQueue<'a> {
    #[cfg(feature = "wgpu")]
    WgpuEncoder(&'a mut wgpu::CommandEncoder),
    #[cfg(feature = "opencl")]
    OpenClQueue(&'a ClQueueRef),
    #[cfg(feature = "cuda")]
    CudaStream(&'a CudaStreamRef),
    MetalCommandBuffer(&'a MtlCommandBufferRef),
    Cpu,
    _Phantom(std::marker::PhantomData<&'a ()>),
}

impl ExternalQueue<'_> {
    pub fn backend(&self) -> BackendKind {
        match self {
            Self::Cpu => BackendKind::Cpu,
            #[cfg(feature = "wgpu")]
            Self::WgpuEncoder(_) => BackendKind::Wgpu,
            #[cfg(feature = "opencl")]
            Self::OpenClQueue(_) => BackendKind::OpenCl,
            #[cfg(feature = "cuda")]
            Self::CudaStream(_) => BackendKind::Cuda,
            #[cfg(feature = "wgpu")]
            Self::MetalCommandBuffer(_) => BackendKind::Wgpu,
            #[cfg(not(feature = "wgpu"))]
            Self::MetalCommandBuffer(_) => BackendKind::Cpu,
            Self::_Phantom(_) => BackendKind::Cpu,
        }
    }
}

impl core::fmt::Debug for ExternalQueue<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("ExternalQueue")
    }
}