use crate::RequiresOneOf;
use bytemuck::cast_slice;
use std::{
error::Error,
fmt::{Display, Error as FmtError, Formatter},
};
#[derive(Clone, Debug)]
pub struct ExtensionProperties {
pub extension_name: String,
pub spec_version: u32,
}
impl From<ash::vk::ExtensionProperties> for ExtensionProperties {
#[inline]
fn from(val: ash::vk::ExtensionProperties) -> Self {
Self {
extension_name: {
let bytes = cast_slice(val.extension_name.as_slice());
let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
String::from_utf8_lossy(&bytes[0..end]).into()
},
spec_version: val.spec_version,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct ExtensionRestrictionError {
pub extension: &'static str,
pub restriction: ExtensionRestriction,
}
impl Error for ExtensionRestrictionError {}
impl Display for ExtensionRestrictionError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
write!(
f,
"a restriction for the extension {} was not met: {}",
self.extension, self.restriction,
)
}
}
#[derive(Clone, Copy, Debug)]
pub enum ExtensionRestriction {
NotSupported,
RequiredIfSupported,
Requires(RequiresOneOf),
ConflictsDeviceExtension(&'static str),
}
impl Display for ExtensionRestriction {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match *self {
ExtensionRestriction::NotSupported => {
write!(f, "not supported by the loader or physical device")
}
ExtensionRestriction::RequiredIfSupported => {
write!(f, "required to be enabled by the physical device")
}
ExtensionRestriction::Requires(requires) => {
if requires.len() > 1 {
write!(f, "requires one of: {}", requires)
} else {
write!(f, "requires: {}", requires)
}
}
ExtensionRestriction::ConflictsDeviceExtension(ext) => {
write!(f, "requires device extension {} to be disabled", ext)
}
}
}
}