diff --git a/vulkano/src/instance/instance.rs b/vulkano/src/instance/instance.rs index 2f6781f4a..2549b6a8b 100644 --- a/vulkano/src/instance/instance.rs +++ b/vulkano/src/instance/instance.rs @@ -19,8 +19,8 @@ use std::slice; use std::sync::Arc; use smallvec::SmallVec; -//use alloc::Alloc; use instance::loader; +use instance::loader::LoadingError; use check_errors; use Error; use OomError; @@ -98,7 +98,7 @@ impl Instance { extension.as_ptr() }).collect::>(); - let entry_points = loader::entry_points().unwrap(); // TODO: return proper error + let entry_points = try!(loader::entry_points()); // Creating the Vulkan instance. let instance = unsafe { @@ -299,8 +299,10 @@ impl<'a> Default for ApplicationInfo<'a> { } /// Error that can happen when creating an instance. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug)] pub enum InstanceCreationError { + /// Failed to load the Vulkan shared library. + LoadingError(LoadingError), /// Not enough memory. OomError(OomError), /// Failed to initialize for an implementation-specific reason. @@ -317,6 +319,7 @@ impl error::Error for InstanceCreationError { #[inline] fn description(&self) -> &str { match *self { + InstanceCreationError::LoadingError(_) => "failed to load the Vulkan shared library", InstanceCreationError::OomError(_) => "not enough memory available", InstanceCreationError::InitializationFailed => "initialization failed", InstanceCreationError::LayerNotPresent => "layer not present", @@ -328,6 +331,7 @@ impl error::Error for InstanceCreationError { #[inline] fn cause(&self) -> Option<&error::Error> { match *self { + InstanceCreationError::LoadingError(ref err) => Some(err), InstanceCreationError::OomError(ref err) => Some(err), _ => None } @@ -348,6 +352,13 @@ impl From for InstanceCreationError { } } +impl From for InstanceCreationError { + #[inline] + fn from(err: LoadingError) -> InstanceCreationError { + InstanceCreationError::LoadingError(err) + } +} + impl From for InstanceCreationError { #[inline] fn from(err: Error) -> InstanceCreationError { diff --git a/vulkano/src/instance/layers.rs b/vulkano/src/instance/layers.rs index f4c355425..58e9e0444 100644 --- a/vulkano/src/instance/layers.rs +++ b/vulkano/src/instance/layers.rs @@ -7,21 +7,24 @@ // notice may not be copied, modified, or distributed except // according to those terms. +use std::error; +use std::fmt; use std::ffi::CStr; use std::ptr; use std::vec::IntoIter; -//use alloc::Alloc; use check_errors; use OomError; +use Error; use vk; use instance::loader; +use instance::loader::LoadingError; use version::Version; /// Queries the list of layers that are available when creating an instance. -pub fn layers_list() -> Result { +pub fn layers_list() -> Result { unsafe { - let entry_points = loader::entry_points().unwrap(); // TODO: return proper error + let entry_points = try!(loader::entry_points()); let mut num = 0; try!(check_errors(entry_points.EnumerateInstanceLayerProperties(&mut num, ptr::null_mut()))); @@ -68,6 +71,65 @@ impl LayerProperties { } } +/// Error that can happen when loading the list of layers. +#[derive(Clone, Debug)] +pub enum LayersListError { + /// Failed to load the Vulkan shared library. + LoadingError(LoadingError), + /// Not enough memory. + OomError(OomError), +} + +impl error::Error for LayersListError { + #[inline] + fn description(&self) -> &str { + match *self { + LayersListError::LoadingError(_) => "failed to load the Vulkan shared library", + LayersListError::OomError(_) => "not enough memory available", + } + } + + #[inline] + fn cause(&self) -> Option<&error::Error> { + match *self { + LayersListError::LoadingError(ref err) => Some(err), + LayersListError::OomError(ref err) => Some(err), + } + } +} + +impl fmt::Display for LayersListError { + #[inline] + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(fmt, "{}", error::Error::description(self)) + } +} + +impl From for LayersListError { + #[inline] + fn from(err: OomError) -> LayersListError { + LayersListError::OomError(err) + } +} + +impl From for LayersListError { + #[inline] + fn from(err: LoadingError) -> LayersListError { + LayersListError::LoadingError(err) + } +} + +impl From for LayersListError { + #[inline] + fn from(err: Error) -> LayersListError { + match err { + err @ Error::OutOfHostMemory => LayersListError::OomError(OomError::from(err)), + err @ Error::OutOfDeviceMemory => LayersListError::OomError(OomError::from(err)), + _ => panic!("unexpected error: {:?}", err) + } + } +} + /// Iterator that produces the list of layers that are available. // TODO: #[derive(Debug, Clone)] pub struct LayersIterator {