[][src]Macro vulkayes_core::vk_result_error

macro_rules! vk_result_error {
    (
		$( #[$attribute: meta] )*
		pub enum $name: ident $([ $($generic_params: tt)+ ] where [ $($generic_bounds: tt)+ ])? {
			vk {
				$(
					$( #[$variant_attribute: meta] )*
					$vk_error: ident
				),+ $(,)?
			}
			$( $other: tt )*
		}
	) => { ... };
}

Generates a public enum that derives thiserror::Error with VkResult variants and their From impls.

Usage:

trait Trait: std::error::Error + 'static {}

vk_result_error! {
	#[derive(Debug)]
	pub enum ImageError [A] where [A: Trait] {
		vk {
			ERROR_OUT_OF_HOST_MEMORY,
			ERROR_OUT_OF_DEVICE_MEMORY
		}

		#[error("Description")]
		Other(#[from] A)
	}
}

expands to:

// ...

#[allow(unused_imports)]
use thiserror::*;

#[derive(Debug)]
#[derive(Error)]
pub enum ImageError<A: Trait> {
	#[error("{}", ash::vk::Result::ERROR_OUT_OF_HOST_MEMORY)]
	#[allow(non_camel_case_types)]
	ERROR_OUT_OF_HOST_MEMORY,
	#[error("{}", ash::vk::Result::ERROR_OUT_OF_DEVICE_MEMORY)]
	#[allow(non_camel_case_types)]
	ERROR_OUT_OF_DEVICE_MEMORY,

	#[error("Description")]
	Other(#[from] A)
}
impl<A: Trait> From<ash::vk::Result> for ImageError<A> {
	fn from(err: ash::vk::Result) -> Self {
		match err {
			ash::vk::Result::ERROR_OUT_OF_HOST_MEMORY => ImageError::ERROR_OUT_OF_HOST_MEMORY,
			ash::vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => ImageError::ERROR_OUT_OF_DEVICE_MEMORY,
			_ => unreachable!("Cannot create {} from {}", stringify!(ImageError), err)
		}
	}
}