Added better error handling

This commit is contained in:
David Johnson 2022-01-16 08:59:29 -06:00
parent e4b1917333
commit dc6fc474be
8 changed files with 136 additions and 105 deletions

View file

@ -1,9 +1,12 @@
#![doc = include_str!("../README.md")]
#[cfg(target_os = "linux")]
mod x11;
#[cfg(target_os = "windows")]
mod win32;
#[cfg(target_os = "linux")]
mod x11;
mod error;
pub use error::SoftBufferError;
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
@ -11,43 +14,46 @@ use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
/// write to a window on that platform. This struct owns the window that this data corresponds to
/// to ensure safety, as that data must be destroyed before the window itself is destroyed. You may
/// access the underlying window via [`window`](Self::window) and [`window_mut`](Self::window_mut).
pub struct GraphicsContext<W: HasRawWindowHandle>{
pub struct GraphicsContext<W: HasRawWindowHandle> {
window: W,
graphics_context_impl: Box<dyn GraphicsContextImpl>
graphics_context_impl: Box<dyn GraphicsContextImpl>,
}
impl<W: HasRawWindowHandle> GraphicsContext<W> {
/// Creates a new instance of this struct, consuming the given window.
///
/// # Safety
///
/// - Ensure that the passed object is valid to draw a 2D buffer to
pub unsafe fn new(window: W) -> Self{
pub unsafe fn new(window: W) -> Result<Self, SoftBufferError<W>> {
let raw_handle = window.raw_window_handle();
let imple = match raw_handle{
let imple = match raw_handle {
#[cfg(target_os = "linux")]
RawWindowHandle::Xlib(xlib_handle) => Box::new(x11::X11Impl::new(xlib_handle)),
RawWindowHandle::Xlib(xlib_handle) => Box::new(x11::X11Impl::new(xlib_handle)?),
#[cfg(target_os = "windows")]
RawWindowHandle::Win32(win32_handle) => Box::new(win32::Win32Impl::new(&win32_handle)),
unimplemented_handle_type => unimplemented!("Unsupported window handle type: {}.", window_handle_type_name(&unimplemented_handle_type))
RawWindowHandle::Win32(win32_handle) => Box::new(win32::Win32Impl::new(&win32_handle)?),
unimplemented_handle_type => return Err(SoftBufferError::UnsupportedPlatform {
window,
human_readable_platform_name: window_handle_type_name(&unimplemented_handle_type),
handle: unimplemented_handle_type,
}),
};
Self{
Ok(Self {
window,
graphics_context_impl: imple
}
graphics_context_impl: imple,
})
}
/// Gets shared access to the underlying window
#[inline]
pub fn window(&self) -> &W{
pub fn window(&self) -> &W {
&self.window
}
/// Gets mut/exclusive access to the underlying window
#[inline]
pub fn window_mut(&mut self) -> &mut W{
pub fn window_mut(&mut self) -> &mut W {
&mut self.window
}
@ -75,8 +81,8 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
/// G: Green channel
/// B: Blue channel
#[inline]
pub fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16){
if (width as usize)*(height as usize) != buffer.len(){
pub fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
if (width as usize) * (height as usize) != buffer.len() {
panic!("The size of the passed buffer is not the correct size. Its length must be exactly width*height.");
}
@ -84,15 +90,14 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
self.graphics_context_impl.set_buffer(buffer, width, height);
}
}
}
trait GraphicsContextImpl{
trait GraphicsContextImpl {
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16);
}
fn window_handle_type_name(handle: &RawWindowHandle) -> &'static str{
match handle{
fn window_handle_type_name(handle: &RawWindowHandle) -> &'static str {
match handle {
RawWindowHandle::Xlib(_) => "Xlib",
RawWindowHandle::Win32(_) => "Win32",
RawWindowHandle::WinRt(_) => "WinRt",
@ -102,6 +107,6 @@ fn window_handle_type_name(handle: &RawWindowHandle) -> &'static str{
RawWindowHandle::AppKit(_) => "AppKit",
RawWindowHandle::Orbital(_) => "Orbital",
RawWindowHandle::UiKit(_) => "UiKit",
_ => "Unknown Name" //don't completely fail to compile if there is a new raw window handle type that's added at some point
_ => "Unknown Name", //don't completely fail to compile if there is a new raw window handle type that's added at some point
}
}
}