Rename the Error type and add docs for the variants.

This commit is contained in:
Jay Oster 2019-09-25 23:47:04 -07:00
parent f0d7c84a3f
commit c4d762d5f5
2 changed files with 23 additions and 25 deletions

View file

@ -1,8 +1,8 @@
use pixels::{Pixels, PixelsError}; use pixels::{Error, Pixels};
use winit::event; use winit::event;
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
fn main() -> Result<(), PixelsError> { fn main() -> Result<(), Error> {
env_logger::init(); env_logger::init();
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();

View file

@ -14,7 +14,7 @@
// Needed for proc-spirv // Needed for proc-spirv
#![feature(proc_macro_hygiene)] #![feature(proc_macro_hygiene)]
use std::error::Error; use std::error::Error as StdError;
use std::fmt; use std::fmt;
use std::io::Cursor; use std::io::Cursor;
@ -42,9 +42,12 @@ pub struct PixelsOptions {
/// All the ways in which creating a frame buffer can fail. /// All the ways in which creating a frame buffer can fail.
#[derive(Debug)] #[derive(Debug)]
pub enum PixelsError { pub enum Error {
/// No suitable Adapter found
AdapterNotFound, AdapterNotFound,
/// Vertex shader is invalid SPIR-V
VertexShaderInvalid, VertexShaderInvalid,
/// Fragment shader is invalid SPIR-V
FragmentShaderInvalid, FragmentShaderInvalid,
} }
@ -80,15 +83,15 @@ pub enum PixelsError {
/// # } /// # }
/// # let surface = wgpu::Surface::create(&RWH()); /// # let surface = wgpu::Surface::create(&RWH());
/// let fb = Pixels::new(320, 240, &surface)?; /// let fb = Pixels::new(320, 240, &surface)?;
/// # Ok::<(), pixels::PixelsError>(()) /// # Ok::<(), pixels::Error>(())
/// ``` /// ```
impl Pixels { impl Pixels {
/// Create a frame buffer instance with default options. /// Create a frame buffer instance with default options.
/// ///
/// # Errors /// # Errors
/// ///
/// Returns `PixelsError` when a `wgpu::Adapter` cannot be found. /// Returns an error when a `wgpu::Adapter` cannot be found.
pub fn new(width: u32, height: u32, surface: &wgpu::Surface) -> Result<Pixels, PixelsError> { pub fn new(width: u32, height: u32, surface: &wgpu::Surface) -> Result<Pixels, Error> {
Pixels::new_with_options(width, height, surface, PixelsOptions::new()) Pixels::new_with_options(width, height, surface, PixelsOptions::new())
} }
@ -96,24 +99,24 @@ impl Pixels {
/// ///
/// # Errors /// # Errors
/// ///
/// Returns `PixelsError` when a `wgpu::Adapter` cannot be found or shaders /// Returns an error when a `wgpu::Adapter` cannot be found or shaders
/// are invalid SPIR-V. /// are invalid SPIR-V.
pub fn new_with_options( pub fn new_with_options(
width: u32, width: u32,
height: u32, height: u32,
surface: &wgpu::Surface, surface: &wgpu::Surface,
options: PixelsOptions, options: PixelsOptions,
) -> Result<Pixels, PixelsError> { ) -> Result<Pixels, Error> {
let adapter = wgpu::Adapter::request(&options.request_adapter_options) let adapter = wgpu::Adapter::request(&options.request_adapter_options)
.ok_or(PixelsError::AdapterNotFound)?; .ok_or(Error::AdapterNotFound)?;
let (device, queue) = adapter.request_device(&options.device_descriptor); let (device, queue) = adapter.request_device(&options.device_descriptor);
let vs_module = device.create_shader_module( let vs_module = device.create_shader_module(
&wgpu::read_spirv(Cursor::new(&options.vertex_spirv)) &wgpu::read_spirv(Cursor::new(&options.vertex_spirv))
.map_err(|_| PixelsError::VertexShaderInvalid)?, .map_err(|_| Error::VertexShaderInvalid)?,
); );
let fs_module = device.create_shader_module( let fs_module = device.create_shader_module(
&wgpu::read_spirv(Cursor::new(&options.fragment_spirv)) &wgpu::read_spirv(Cursor::new(&options.fragment_spirv))
.map_err(|_| PixelsError::FragmentShaderInvalid)?, .map_err(|_| Error::FragmentShaderInvalid)?,
); );
// The rest of this is technically a fixed-function pipeline... For now! // The rest of this is technically a fixed-function pipeline... For now!
@ -241,7 +244,7 @@ impl Pixels {
/// # ); /// # );
/// # } /// # }
/// # } /// # }
/// # fn main() -> Result<(), pixels::PixelsError> { /// # fn main() -> Result<(), pixels::Error> {
/// # let surface = wgpu::Surface::create(&RWH()); /// # let surface = wgpu::Surface::create(&RWH());
/// let fb = PixelsOptions::new() /// let fb = PixelsOptions::new()
/// .fragment_spirv(spirv_from_file!(Fragment, "shaders/shader.frag", "main").to_vec()) /// .fragment_spirv(spirv_from_file!(Fragment, "shaders/shader.frag", "main").to_vec())
@ -283,14 +286,9 @@ impl PixelsOptions {
/// ///
/// # Errors /// # Errors
/// ///
/// Returns `PixelsError` when a `wgpu::Adapter` cannot be found or shaders /// Returns an error when a `wgpu::Adapter` cannot be found or shaders
/// are invalid SPIR-V. /// are invalid SPIR-V.
pub fn build( pub fn build(self, width: u32, height: u32, surface: &wgpu::Surface) -> Result<Pixels, Error> {
self,
width: u32,
height: u32,
surface: &wgpu::Surface,
) -> Result<Pixels, PixelsError> {
Pixels::new_with_options(width, height, surface, self) Pixels::new_with_options(width, height, surface, self)
} }
} }
@ -309,18 +307,18 @@ impl Default for PixelsOptions {
} }
} }
impl fmt::Display for PixelsError { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description()) write!(f, "{}", self.description())
} }
} }
impl Error for PixelsError { impl StdError for Error {
fn description(&self) -> &str { fn description(&self) -> &str {
match self { match self {
PixelsError::AdapterNotFound => "Adapter not found", Error::AdapterNotFound => "No suitable Adapter found",
PixelsError::FragmentShaderInvalid => "Fragment shader is invalid SPIR-V", Error::VertexShaderInvalid => "Vertex shader is invalid SPIR-V",
PixelsError::VertexShaderInvalid => "Vertex shader is invalid SPIR-V", Error::FragmentShaderInvalid => "Fragment shader is invalid SPIR-V",
} }
} }
} }