use crate::{Device, Error, Result};
#[cfg(feature = "metal")]
pub use fuel_metal_backend::*;
#[cfg(feature = "metal")]
impl From<fuel_metal_backend::MetalDevice> for Device {
fn from(dev: fuel_metal_backend::MetalDevice) -> Self {
Device::custom(std::sync::Arc::new(dev))
}
}
#[cfg(feature = "metal")]
pub fn new_device(ordinal: usize) -> Result<Device> {
Ok(fuel_metal_backend::MetalDevice::new(ordinal)?.into())
}
#[cfg(not(feature = "metal"))]
pub fn new_device(_ordinal: usize) -> Result<Device> {
Err(Error::NotCompiledWithMetalSupport.bt())
}
fn metal_is_available() -> bool {
cfg!(feature = "metal")
}
pub fn device_if_available(ordinal: usize) -> Result<Device> {
if metal_is_available() {
new_device(ordinal)
} else {
Ok(Device::cpu())
}
}
#[cfg(feature = "metal")]
pub fn as_device(device: &Device) -> Result<&fuel_metal_backend::MetalDevice> {
device
.inner
.as_any()
.downcast_ref::<fuel_metal_backend::MetalDevice>()
.ok_or_else(|| Error::Msg("expected a metal device".into()).bt())
}