From 9adfddb053669e9324165db1376d9ca6bfb08764 Mon Sep 17 00:00:00 2001 From: ezioleq Date: Thu, 19 Oct 2023 20:43:33 +0200 Subject: [PATCH 1/6] Closes #242 Throw PlatformNotSupportedException instead of the InvalidOperationException in Window's constructor --- NoiseEngine/Window.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/NoiseEngine/Window.cs b/NoiseEngine/Window.cs index e3694a31..fd1cb0b0 100644 --- a/NoiseEngine/Window.cs +++ b/NoiseEngine/Window.cs @@ -72,8 +72,11 @@ public class Window : IDisposable, ICameraRenderTarget, IReferenceCoutable { if (!WindowInterop.Create(Id, title, width, height, new WindowSettingsRaw(settings)).TryGetValue( out InteropHandle handle, out ResultError error - )) { - error.ThrowAndDispose(); + )) + { + Exception exception = error.ToException(); + error.Dispose(); + throw new PlatformNotSupportedException(exception.Message); } Handle = handle; From b235e17eceb1e200a17408bd05f7ca4ecaa2cf28 Mon Sep 17 00:00:00 2001 From: ezioleq Date: Thu, 19 Oct 2023 21:02:12 +0200 Subject: [PATCH 2/6] Revert commit after CR This reverts commit 9adfddb053669e9324165db1376d9ca6bfb08764 as a result of the Code Review. --- NoiseEngine/Window.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/NoiseEngine/Window.cs b/NoiseEngine/Window.cs index fd1cb0b0..e3694a31 100644 --- a/NoiseEngine/Window.cs +++ b/NoiseEngine/Window.cs @@ -72,11 +72,8 @@ public class Window : IDisposable, ICameraRenderTarget, IReferenceCoutable { if (!WindowInterop.Create(Id, title, width, height, new WindowSettingsRaw(settings)).TryGetValue( out InteropHandle handle, out ResultError error - )) - { - Exception exception = error.ToException(); - error.Dispose(); - throw new PlatformNotSupportedException(exception.Message); + )) { + error.ThrowAndDispose(); } Handle = handle; From 44b6cbce314328ab52d0e1264c63f517d284598f Mon Sep 17 00:00:00 2001 From: ezioleq Date: Thu, 19 Oct 2023 21:20:29 +0200 Subject: [PATCH 3/6] Add PlatformNotSupported error in Rust codebase --- NoiseEngine.Native/src/errors/mod.rs | 1 + .../src/errors/platform_not_supported.rs | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 NoiseEngine.Native/src/errors/platform_not_supported.rs diff --git a/NoiseEngine.Native/src/errors/mod.rs b/NoiseEngine.Native/src/errors/mod.rs index a1f61ce8..d2fe1528 100644 --- a/NoiseEngine.Native/src/errors/mod.rs +++ b/NoiseEngine.Native/src/errors/mod.rs @@ -4,3 +4,4 @@ pub mod conversions; pub mod invalid_operation; pub mod null_reference; pub mod overflow; +pub mod platform_not_supported; diff --git a/NoiseEngine.Native/src/errors/platform_not_supported.rs b/NoiseEngine.Native/src/errors/platform_not_supported.rs new file mode 100644 index 00000000..ea899092 --- /dev/null +++ b/NoiseEngine.Native/src/errors/platform_not_supported.rs @@ -0,0 +1,44 @@ +use std::{error::Error, fmt::Display}; + +use crate::interop::prelude::{ResultError, ResultErrorKind}; + +#[derive(Debug)] +pub struct PlatformNotSupportedError { + message: String, +} + +impl PlatformNotSupportedError { + pub fn new(message: String) -> Self { + Self { message } + } + + pub fn with_str(message: &str) -> Self { + Self::new(message.to_owned()) + } +} + +impl Default for PlatformNotSupportedError { + fn default() -> Self { + Self { + message: "Platform not supported.".to_string(), + } + } +} + +impl Error for PlatformNotSupportedError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + None + } +} + +impl Display for PlatformNotSupportedError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.message) + } +} + +impl From for ResultError { + fn from(err: PlatformNotSupportedError) -> Self { + ResultError::with_kind(&err, ResultErrorKind::PlatformNotSupported) + } +} From 19c0867b48c95926677e7ef6b703982924b04787 Mon Sep 17 00:00:00 2001 From: ezioleq Date: Thu, 19 Oct 2023 21:21:31 +0200 Subject: [PATCH 4/6] Add new error kind to enums --- NoiseEngine.Native/src/interop/result_error_kind.rs | 1 + NoiseEngine/Interop/ResultErrorKind.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/NoiseEngine.Native/src/interop/result_error_kind.rs b/NoiseEngine.Native/src/interop/result_error_kind.rs index 91001389..a2a91a73 100644 --- a/NoiseEngine.Native/src/interop/result_error_kind.rs +++ b/NoiseEngine.Native/src/interop/result_error_kind.rs @@ -8,6 +8,7 @@ pub enum ResultErrorKind { InvalidOperation = 3, Overflow = 4, Argument = 5, + PlatformNotSupported = 6, GraphicsUniversal = 1000, GraphicsInstanceCreate = 1001, diff --git a/NoiseEngine/Interop/ResultErrorKind.cs b/NoiseEngine/Interop/ResultErrorKind.cs index 0838bd95..b17a602b 100644 --- a/NoiseEngine/Interop/ResultErrorKind.cs +++ b/NoiseEngine/Interop/ResultErrorKind.cs @@ -7,6 +7,7 @@ internal enum ResultErrorKind : uint { InvalidOperation = 3, Overflow = 4, Argument = 5, + PlatformNotSupported = 6, GraphicsUniversal = 1000, GraphicsInstanceCreate = 1001, From e93704907a4a3f2b9868d04f0b7878ea7b3df560 Mon Sep 17 00:00:00 2001 From: ezioleq Date: Thu, 19 Oct 2023 21:22:04 +0200 Subject: [PATCH 5/6] Handle new exception type in ResultError --- NoiseEngine/Interop/ResultError.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/NoiseEngine/Interop/ResultError.cs b/NoiseEngine/Interop/ResultError.cs index 20d985c1..640ddf39 100644 --- a/NoiseEngine/Interop/ResultError.cs +++ b/NoiseEngine/Interop/ResultError.cs @@ -49,6 +49,7 @@ internal struct ResultError : IDisposable { ResultErrorKind.InvalidOperation => new InvalidOperationException(Message, innerException), ResultErrorKind.Overflow => new OverflowException(Message, innerException), ResultErrorKind.Argument => new ArgumentException(Message, innerException), + ResultErrorKind.PlatformNotSupported => new PlatformNotSupportedException(Message, innerException), ResultErrorKind.GraphicsUniversal => new GraphicsException(Message, innerException), ResultErrorKind.GraphicsInstanceCreate => new GraphicsInstanceCreateException(Message, innerException), From 17a316580954e05dee3911fca571db08e2e1572e Mon Sep 17 00:00:00 2001 From: ezioleq Date: Thu, 19 Oct 2023 21:23:26 +0200 Subject: [PATCH 6/6] Replace result error type when device where window is created isn't supported Closes #242 --- .../src/interop/rendering/presentation/window_interop.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NoiseEngine.Native/src/interop/rendering/presentation/window_interop.rs b/NoiseEngine.Native/src/interop/rendering/presentation/window_interop.rs index 0371d511..03c800f4 100644 --- a/NoiseEngine.Native/src/interop/rendering/presentation/window_interop.rs +++ b/NoiseEngine.Native/src/interop/rendering/presentation/window_interop.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use cgmath::Vector2; use crate::{ - errors::invalid_operation::InvalidOperationError, + errors::platform_not_supported::PlatformNotSupportedError, interop::prelude::{InteropOption, InteropResult, InteropString}, rendering::presentation::{input::InputData, window::Window, window_settings::WindowSettings}, }; @@ -31,7 +31,7 @@ extern "C" fn rendering_presentation_window_interop_create( #[allow(unreachable_code)] InteropResult::with_err( - InvalidOperationError::with_str("Window is not supported on this device.").into(), + PlatformNotSupportedError::with_str("Window is not supported on this device.").into(), ) }