Browse Source

change naming for HAL errors

master
Alison Watson 2 years ago
parent
commit
5051934b43
  1. 12
      source/framework/hal.rs
  2. 6
      source/framework/hal/context.rs
  3. 16
      source/framework/hal/window.rs
  4. 7
      source/framework/render/instance.rs
  5. 6
      source/framework/render/surface.rs

12
source/framework/hal.rs

@ -11,20 +11,20 @@ pub use self::{
use sdl2_sys::SDL_GetError;
#[derive(Error, Debug)]
pub enum ErrSdl {
pub enum Err {
#[error("SDL Error: {0}")]
Cow(std::borrow::Cow<'static, str>),
Sdl(std::borrow::Cow<'static, str>),
}
impl ErrSdl {
/// Creates an `ErrSdl` from the current SDL error.
impl Err {
/// Creates an `Err` from the current SDL error.
///
/// # Safety
///
/// This function will cause undefined behaviour if used from
/// multiple threads.
pub unsafe fn new() -> Self {
Self::Cow(std::ffi::CStr::from_ptr(SDL_GetError()).to_string_lossy())
pub(self) unsafe fn new_sdl() -> Self {
Self::Sdl(std::ffi::CStr::from_ptr(SDL_GetError()).to_string_lossy())
}
}

6
source/framework/hal/context.rs

@ -1,10 +1,10 @@
use crate::hal::ErrSdl;
use super::Err;
use sdl2_sys::{SDL_Init, SDL_Quit, SDL_SetMainReady, SDL_INIT_VIDEO};
pub struct Context;
impl Context {
pub fn new() -> Result<Self, ErrSdl> {
pub fn new() -> Result<Self, Err> {
let init_flags = SDL_INIT_VIDEO;
unsafe {
SDL_SetMainReady();
@ -12,7 +12,7 @@ impl Context {
if unsafe { SDL_Init(init_flags) } == 0 {
Ok(Self)
} else {
Err(unsafe { ErrSdl::new() })
Err(unsafe { Err::new_sdl() })
}
}
}

16
source/framework/hal/window.rs

@ -1,4 +1,4 @@
use crate::hal::{Context, ErrSdl};
use super::{Context, Err};
use ash::{
version::InstanceV1_0,
vk::{self, Handle},
@ -21,7 +21,7 @@ impl<'a> Window<'a> {
title: crate::ffi::Nts,
w: i16,
h: i16,
) -> Result<Self, ErrSdl> {
) -> Result<Self, Err> {
let handle = unsafe {
SDL_CreateWindow(
title,
@ -37,13 +37,13 @@ impl<'a> Window<'a> {
if !handle.is_null() {
Ok(Self { hal: PhantomData, handle })
} else {
Err(unsafe { ErrSdl::new() })
Err(unsafe { Err::new_sdl() })
}
}
pub fn vulkan_instance_extensions(
&self,
) -> Result<Vec<crate::ffi::Nts>, ErrSdl> {
) -> Result<Vec<crate::ffi::Nts>, Err> {
let mut count = 0;
let res = unsafe {
SDL_Vulkan_GetInstanceExtensions(
@ -64,17 +64,17 @@ impl<'a> Window<'a> {
if res != SDL_bool::SDL_FALSE {
Ok(names)
} else {
Err(unsafe { ErrSdl::new() })
Err(unsafe { Err::new_sdl() })
}
} else {
Err(unsafe { ErrSdl::new() })
Err(unsafe { Err::new_sdl() })
}
}
pub fn vulkan_create_surface(
&self,
instance: &crate::render::Instance,
) -> Result<vk::SurfaceKHR, ErrSdl> {
) -> Result<vk::SurfaceKHR, Err> {
let mut surface = 0;
let res = unsafe {
SDL_Vulkan_CreateSurface(
@ -86,7 +86,7 @@ impl<'a> Window<'a> {
if res != SDL_bool::SDL_FALSE {
Ok(vk::SurfaceKHR::from_raw(surface))
} else {
Err(unsafe { ErrSdl::new() })
Err(unsafe { Err::new_sdl() })
}
}
}

7
source/framework/render/instance.rs

@ -1,6 +1,5 @@
use crate::{
hal::ErrSdl,
meta,
hal, meta,
render::{ensure_properties, Conf, ErrProperty},
};
use ash::{
@ -27,7 +26,7 @@ pub enum ErrInstanceCreate {
#[error("Missing extension: {0}")]
NoExtension(#[from] ErrProperty),
#[error(transparent)]
Sdl(#[from] ErrSdl),
Hal(#[from] hal::Err),
}
fn enable_instance_layers(
@ -50,7 +49,7 @@ impl Instance {
pub fn create(
conf: &Conf,
entry: ash::Entry,
window: &crate::hal::Window,
window: &hal::Window,
) -> Result<Rc<Self>, ErrInstanceCreate> {
let exts = window.vulkan_instance_extensions()?;

6
source/framework/render/surface.rs

@ -1,4 +1,4 @@
use crate::render::Instance;
use crate::{hal, render::Instance};
use ash::vk;
use std::rc::Rc;
@ -11,8 +11,8 @@ pub struct Surface {
impl Surface {
pub fn create(
instance: Rc<Instance>,
window: &crate::hal::Window,
) -> Result<Rc<Self>, crate::hal::ErrSdl> {
window: &hal::Window,
) -> Result<Rc<Self>, hal::Err> {
let handle = window.vulkan_create_surface(&instance)?;
Ok(Rc::new(Self { handle, instance }))
}

Loading…
Cancel
Save