From 0ff36e6a080a51f2f6acf84d207210657bf1c0d9 Mon Sep 17 00:00:00 2001 From: nanoqsh Date: Thu, 15 Feb 2024 13:33:13 +0600 Subject: [PATCH] Refactor --- README.md | 3 +-- dunge/src/context.rs | 13 ++++++++++--- dunge/src/init.rs | 8 ++++---- dunge/src/lib.rs | 2 +- dunge/src/state.rs | 8 ++++---- dunge/src/window.rs | 8 ++++---- xtask/src/main.rs | 2 +- 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a684d20..8cd2416 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ -# Dunge -
+

Dunge

Simple and portable 3d render based on WGPU.

diff --git a/dunge/src/context.rs b/dunge/src/context.rs index ba75d5f..5ccd175 100644 --- a/dunge/src/context.rs +++ b/dunge/src/context.rs @@ -121,12 +121,12 @@ impl Context { /// An error returned from the [context](Context) constructor. #[derive(Debug)] -pub enum Error { +pub enum MakeContextError { BackendSelection, RequestDevice(wgpu::RequestDeviceError), } -impl fmt::Display for Error { +impl fmt::Display for MakeContextError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::BackendSelection => write!(f, "failed to select backend"), @@ -135,4 +135,11 @@ impl fmt::Display for Error { } } -impl error::Error for Error {} +impl error::Error for MakeContextError { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + match self { + Self::BackendSelection => None, + Self::RequestDevice(err) => Some(err), + } + } +} diff --git a/dunge/src/init.rs b/dunge/src/init.rs index 6b53196..bc125e8 100644 --- a/dunge/src/init.rs +++ b/dunge/src/init.rs @@ -1,6 +1,6 @@ use { crate::{ - context::{self, Context}, + context::{Context, MakeContextError}, state::State, }, wgpu::Instance, @@ -9,7 +9,7 @@ use { #[cfg(feature = "winit")] use crate::{element::Element, window::WindowBuilder}; -pub(crate) async fn make() -> Result<(Context, Instance), context::Error> { +pub(crate) async fn make() -> Result<(Context, Instance), MakeContextError> { use wgpu::{Backends, InstanceDescriptor, InstanceFlags}; let backends; @@ -44,8 +44,8 @@ pub(crate) async fn make() -> Result<(Context, Instance), context::Error> { /// /// # Errors /// Returns an error when the context could not be created. -/// See [`Error`](context::Error) for details. -pub async fn context() -> Result { +/// See [`MakeContextError`] for details. +pub async fn context() -> Result { make().await.map(|(cx, _)| cx) } diff --git a/dunge/src/lib.rs b/dunge/src/lib.rs index 1269006..acb1149 100644 --- a/dunge/src/lib.rs +++ b/dunge/src/lib.rs @@ -38,7 +38,7 @@ pub mod prelude { pub use { crate::{ - context::{Context, Error}, + context::{Context, MakeContextError}, draw::{draw, Draw}, format::Format, init::context, diff --git a/dunge/src/state.rs b/dunge/src/state.rs index 1a7281d..ad92300 100644 --- a/dunge/src/state.rs +++ b/dunge/src/state.rs @@ -1,7 +1,7 @@ use { crate::{ color::Rgba, - context::Error, + context::MakeContextError, draw::Draw, format::Format, layer::{Layer, SetLayer}, @@ -23,7 +23,7 @@ pub(crate) struct State { } impl State { - pub async fn new(instance: &Instance) -> Result { + pub async fn new(instance: &Instance) -> Result { let adapter = { use wgpu::{PowerPreference, RequestAdapterOptions}; @@ -35,7 +35,7 @@ impl State { instance .request_adapter(&options) .await - .ok_or(Error::BackendSelection)? + .ok_or(MakeContextError::BackendSelection)? }; let backend = adapter.get_info().backend; @@ -58,7 +58,7 @@ impl State { adapter .request_device(&desc, None) .await - .map_err(Error::RequestDevice)? + .map_err(MakeContextError::RequestDevice)? }; Ok(Self { diff --git a/dunge/src/window.rs b/dunge/src/window.rs index f29c0c8..021ab78 100644 --- a/dunge/src/window.rs +++ b/dunge/src/window.rs @@ -2,7 +2,7 @@ use { crate::{ - context::{self, Context}, + context::{Context, MakeContextError}, el::Loop, element::Element, format::Format, @@ -320,8 +320,8 @@ impl From for Error { } } -impl From for Error { - fn from(v: context::Error) -> Self { +impl From for Error { + fn from(v: MakeContextError) -> Self { Self(ErrorKind::Context(v)) } } @@ -356,5 +356,5 @@ enum ErrorKind { EventLoop(EventLoopError), Os(OsError), Surface(CreateSurfaceError), - Context(context::Error), + Context(MakeContextError), } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 93243be..0c1147c 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -12,7 +12,7 @@ fn run() -> Result<(), Error> { let module = env::args().nth(1).ok_or("no module specified")?; let root = Path::new(&env!("CARGO_MANIFEST_DIR")) .parent() - .expect("root dir"); + .ok_or("root dir not found")?; let status = Command::new("wasm-pack") .current_dir(root)