From 561f20d8504774f07a00315477de2ef57ad3585a Mon Sep 17 00:00:00 2001 From: Frank Plowman Date: Tue, 18 Oct 2022 15:12:46 +0100 Subject: [PATCH 1/2] implement Error for DrawError using thiserror --- nannou/Cargo.toml | 1 + nannou/src/draw/renderer/mod.rs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/nannou/Cargo.toml b/nannou/Cargo.toml index 3d50152bf..1ca394a7a 100644 --- a/nannou/Cargo.toml +++ b/nannou/Cargo.toml @@ -28,6 +28,7 @@ rusttype = { version = "0.8", features = ["gpu_cache"] } serde = "1" serde_derive = "1" serde_json = "1" +thiserror = "1" toml = "0.5" walkdir = "2" web-sys = { version = "0.3.55", optional = true } diff --git a/nannou/src/draw/renderer/mod.rs b/nannou/src/draw/renderer/mod.rs index 7c978435e..bcc62241d 100644 --- a/nannou/src/draw/renderer/mod.rs +++ b/nannou/src/draw/renderer/mod.rs @@ -12,6 +12,7 @@ use std::collections::HashMap; use std::fmt; use std::hash::{Hash, Hasher}; use std::ops::{Deref, DerefMut}; +use thiserror::Error; use wgpu::util::{BufferInitDescriptor, DeviceExt}; /// Draw API primitives that may be rendered via the **Renderer** type. @@ -134,7 +135,8 @@ pub struct Scissor { height: u32, } -#[derive(Debug)] +#[derive(Debug, Error)] +#[error("error rendering drawing")] pub struct DrawError; #[repr(C)] From 742b86b2d510c6c11ec84f89f67f53154fc3ca99 Mon Sep 17 00:00:00 2001 From: Frank Plowman Date: Mon, 24 Oct 2022 12:21:05 +0100 Subject: [PATCH 2/2] implement all public Error types using thiserror --- nannou/src/io.rs | 40 ++++++---------------------------------- nannou/src/text/font.rs | 33 ++++++--------------------------- nannou/src/window.rs | 24 +++++------------------- 3 files changed, 17 insertions(+), 80 deletions(-) diff --git a/nannou/src/io.rs b/nannou/src/io.rs index 2a3df26d9..764f7d9fa 100644 --- a/nannou/src/io.rs +++ b/nannou/src/io.rs @@ -3,16 +3,18 @@ use serde; use serde_json; -use std::error::Error; use std::io::{Read, Write}; use std::path::Path; -use std::{fmt, fs, io}; +use std::{fs, io}; +use thiserror::Error; use toml; /// Errors that might occur when saving a file. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum FileError { - Io(io::Error), + #[error(transparent)] + Io(#[from] io::Error), + #[error(transparent)] Format(E), } @@ -20,12 +22,6 @@ pub type JsonFileError = FileError; pub type TomlFileSaveError = FileError; pub type TomlFileLoadError = FileError; -impl From for FileError { - fn from(err: io::Error) -> Self { - FileError::Io(err) - } -} - impl From for JsonFileError { fn from(err: serde_json::Error) -> Self { FileError::Format(err) @@ -44,30 +40,6 @@ impl From for TomlFileLoadError { } } -impl Error for FileError -where - E: Error, -{ - fn cause(&self) -> Option<&dyn Error> { - match *self { - FileError::Io(ref err) => Some(err), - FileError::Format(ref err) => Some(err), - } - } -} - -impl fmt::Display for FileError -where - E: Error, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - FileError::Io(ref err) => fmt::Display::fmt(err, f), - FileError::Format(ref err) => fmt::Display::fmt(err, f), - } - } -} - /// Safely save a file with the given contents, replacing the original if necessary. /// /// Works as follows: diff --git a/nannou/src/text/font.rs b/nannou/src/text/font.rs index 6cae49631..2f7257352 100644 --- a/nannou/src/text/font.rs +++ b/nannou/src/text/font.rs @@ -4,6 +4,7 @@ use crate::text::{Font, FontCollection}; use std::collections::HashMap; use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; +use thiserror::Error; /// A type-safe wrapper around the `FontId`. /// @@ -34,11 +35,13 @@ pub struct Ids<'a> { } /// Returned when loading new fonts from file or bytes. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// Some error occurred while loading a `FontCollection` from a file. - Io(std::io::Error), + #[error(transparent)] + Io(#[from] std::io::Error), /// No `Font`s could be yielded from the `FontCollection`. + #[error("No `Font` found in the loaded `FontCollection`.")] NoFont, } @@ -201,28 +204,4 @@ impl<'a> Iterator for Ids<'a> { fn next(&mut self) -> Option { self.keys.next().map(|&id| id) } -} - -impl From for Error { - fn from(e: std::io::Error) -> Self { - Error::Io(e) - } -} - -impl std::error::Error for Error { - fn cause(&self) -> Option<&dyn std::error::Error> { - match *self { - Error::Io(ref e) => Some(e), - Error::NoFont => None, - } - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - match *self { - Error::Io(ref e) => std::fmt::Display::fmt(e, f), - Error::NoFont => write!(f, "No `Font` found in the loaded `FontCollection`."), - } - } -} +} \ No newline at end of file diff --git a/nannou/src/window.rs b/nannou/src/window.rs index 81bab37d5..577135679 100644 --- a/nannou/src/window.rs +++ b/nannou/src/window.rs @@ -18,6 +18,7 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use std::time::Duration; use std::{env, fmt}; +use thiserror::Error; use winit::dpi::{LogicalSize, PhysicalSize}; pub use winit::window::Fullscreen; @@ -164,10 +165,12 @@ pub type UnfocusedFn = fn(&App, &mut Model); pub type ClosedFn = fn(&App, &mut Model); /// Errors that might occur while building the window. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum BuildError { + #[error("no wgpu adapter available to build window")] NoAvailableAdapter, - WinitOsError(winit::error::OsError), + #[error(transparent)] + WinitOsError(#[from] winit::error::OsError), } // A macro for generating a handle to a function that can be stored within the Window without @@ -1571,23 +1574,6 @@ impl fmt::Debug for View { } } -// Error implementations. - -impl fmt::Display for BuildError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - BuildError::NoAvailableAdapter => write!(f, "no available wgpu adapter detected"), - BuildError::WinitOsError(ref e) => e.fmt(f), - } - } -} - -impl From for BuildError { - fn from(e: winit::error::OsError) -> Self { - BuildError::WinitOsError(e) - } -} - // Some WGPU helper implementations. impl<'a> wgpu::WithDeviceQueuePair for &'a crate::window::Window {