Add error trait for InstanceError

This commit is contained in:
Maik Klein 2017-06-05 10:53:54 +02:00
parent 9939be179c
commit 39140519da

View file

@ -5,7 +5,9 @@ use vk;
use instance::Instance; use instance::Instance;
use shared_library::dynamic_library::DynamicLibrary; use shared_library::dynamic_library::DynamicLibrary;
use std::path::Path; use std::path::Path;
use ::RawPtr; use std::error::Error;
use std::fmt;
use RawPtr;
use version::{FunctionPointers, V1_0, InstanceLoader, EntryLoader}; use version::{FunctionPointers, V1_0, InstanceLoader, EntryLoader};
#[cfg(windows)] #[cfg(windows)]
@ -46,6 +48,25 @@ pub enum InstanceError {
VkError(vk::Result), VkError(vk::Result),
} }
impl fmt::Display for InstanceError{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
write!(f, "InstanceError::{:?}", self)
}
}
impl Error for InstanceError {
fn description(&self) -> &str {
"InstanceError"
}
fn cause(&self) -> Option<&Error>{
if let &InstanceError::VkError(ref err) = self{
return err.cause();
}
None
}
}
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub trait EntryV1_0 { pub trait EntryV1_0 {
type Fp: FunctionPointers; type Fp: FunctionPointers;
@ -58,13 +79,17 @@ pub trait EntryV1_0 {
-> Result<Instance<Self::Fp>, InstanceError> { -> Result<Instance<Self::Fp>, InstanceError> {
unsafe { unsafe {
let mut instance: vk::Instance = mem::uninitialized(); let mut instance: vk::Instance = mem::uninitialized();
let err_code = self.fp_v1_0().create_instance(create_info, let err_code = self.fp_v1_0()
allocation_callbacks.as_raw_ptr(), .create_instance(create_info,
&mut instance); allocation_callbacks.as_raw_ptr(),
&mut instance);
if err_code != vk::Result::Success { if err_code != vk::Result::Success {
return Err(InstanceError::VkError(err_code)); return Err(InstanceError::VkError(err_code));
} }
let instance_fp = <<Self as EntryV1_0>::Fp as FunctionPointers>::InstanceFp::load(&self.static_fn(), instance).map_err(|err| InstanceError::LoadError(err))?; let instance_fp =
<<Self as EntryV1_0>::Fp as FunctionPointers>::InstanceFp::load(&self.static_fn(),
instance)
.map_err(|err| InstanceError::LoadError(err))?;
Ok(Instance::from_raw(instance, instance_fp)) Ok(Instance::from_raw(instance, instance_fp))
} }
} }
@ -72,7 +97,8 @@ pub trait EntryV1_0 {
fn enumerate_instance_layer_properties(&self) -> VkResult<Vec<vk::LayerProperties>> { fn enumerate_instance_layer_properties(&self) -> VkResult<Vec<vk::LayerProperties>> {
unsafe { unsafe {
let mut num = 0; let mut num = 0;
self.fp_v1_0().enumerate_instance_layer_properties(&mut num, ptr::null_mut()); self.fp_v1_0()
.enumerate_instance_layer_properties(&mut num, ptr::null_mut());
let mut v = Vec::with_capacity(num as usize); let mut v = Vec::with_capacity(num as usize);
let err_code = self.fp_v1_0() let err_code = self.fp_v1_0()
@ -91,8 +117,11 @@ pub trait EntryV1_0 {
self.fp_v1_0() self.fp_v1_0()
.enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut()); .enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut());
let mut data = Vec::with_capacity(num as usize); let mut data = Vec::with_capacity(num as usize);
let err_code = self.fp_v1_0() let err_code =
.enumerate_instance_extension_properties(ptr::null(), &mut num, data.as_mut_ptr()); self.fp_v1_0()
.enumerate_instance_extension_properties(ptr::null(),
&mut num,
data.as_mut_ptr());
data.set_len(num as usize); data.set_len(num as usize);
match err_code { match err_code {
vk::Result::Success => Ok(data), vk::Result::Success => Ok(data),
@ -124,23 +153,25 @@ impl<V: FunctionPointers> Entry<V> {
let static_fn = match *VK_LIB { let static_fn = match *VK_LIB {
Ok(ref lib) => { Ok(ref lib) => {
let static_fn = vk::StaticFn::load(|name| unsafe { let static_fn = vk::StaticFn::load(|name| unsafe {
let name = name.to_str().unwrap(); let name = name.to_str().unwrap();
let f = match lib.symbol(name) { let f = match lib.symbol(name) {
Ok(s) => s, Ok(s) => s,
Err(_) => ptr::null(), Err(_) => ptr::null(),
}; };
f f
}).map_err(|err| LoadingError::StaticLoadError(err))?; })
.map_err(|err| LoadingError::StaticLoadError(err))?;
Ok(static_fn) Ok(static_fn)
} }
Err(ref err) => Err(LoadingError::LibraryLoadError(err.clone())), Err(ref err) => Err(LoadingError::LibraryLoadError(err.clone())),
}?; }?;
let entry_fn = unsafe { let entry_fn = unsafe {
V::EntryFp::load(&static_fn).map_err(|err| LoadingError::EntryLoadError(err))? V::EntryFp::load(&static_fn)
.map_err(|err| LoadingError::EntryLoadError(err))?
}; };
Ok(Entry { Ok(Entry {
static_fn: static_fn, static_fn: static_fn,
entry_fn: entry_fn, entry_fn: entry_fn,
}) })
} }
} }