From 623b3e5070bb0e67bb3869a22d47674979cb65b3 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sun, 4 Feb 2024 10:25:28 -0800 Subject: [PATCH] chore: Move part of error.rs to winit-core Signed-off-by: John Nunley --- Cargo.toml | 3 +++ winit-core/src/error.rs | 54 +++++++++++++++++++++++++++++++++++++++++ winit-core/src/lib.rs | 6 +++-- winit/Cargo.toml | 1 + winit/src/error.rs | 36 +++------------------------ 5 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 winit-core/src/error.rs diff --git a/Cargo.toml b/Cargo.toml index a8a7ad4c3..9463a7173 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,6 @@ members = [ "winit-core" ] resolver = "2" + +[workspace.dependencies] +winit-core = { path = "./winit-core", default-features = false, features = ["std"] } diff --git a/winit-core/src/error.rs b/winit-core/src/error.rs new file mode 100644 index 000000000..e7013de0f --- /dev/null +++ b/winit-core/src/error.rs @@ -0,0 +1,54 @@ +//! Common error types. + +use std::{error, fmt}; + +/// The error type for when the requested operation is not supported by the backend. +#[derive(Clone)] +pub struct NotSupportedError { + _marker: (), +} + +impl Default for NotSupportedError { + fn default() -> Self { + Self::new() + } +} + +impl NotSupportedError { + /// Create a new [`NotSupportedError`]. + #[inline] + pub fn new() -> NotSupportedError { + NotSupportedError { _marker: () } + } +} + +impl fmt::Debug for NotSupportedError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + f.debug_struct("NotSupportedError").finish() + } +} + +impl fmt::Display for NotSupportedError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + f.pad("the requested operation is not supported by Winit") + } +} + +impl error::Error for NotSupportedError {} + +#[cfg(test)] +mod tests { + #![allow(clippy::redundant_clone)] + + use super::*; + + // Eat attributes for testing + #[test] + fn ensure_fmt_does_not_panic() { + let _ = format!( + "{:?}, {}", + NotSupportedError::new(), + NotSupportedError::new().clone() + ); + } +} diff --git a/winit-core/src/lib.rs b/winit-core/src/lib.rs index e08d3ca1d..bd0dc6304 100644 --- a/winit-core/src/lib.rs +++ b/winit-core/src/lib.rs @@ -1,11 +1,13 @@ //! Base types for a windowing library. -//! +//! //! This crate contains types, traits and basic functions from [`winit`] that are platform //! independent. It is intended to allow for other crates to build abstractions around [`winit`] //! without needing to pull in all of [`winit`]'s dependencies, as well as to provide an //! interface for alternative backends for [`winit`] to be constructed. -//! +//! //! [`winit`]: https://docs.rs/winit #[cfg(any(not(feature = "std"), not(feature = "alloc")))] compile_error! { "no-std and no-alloc usage are not yet supported" } + +pub mod error; diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 83eaca444..585f14ce3 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -71,6 +71,7 @@ rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = ["std"], rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"], optional = true } serde = { version = "1", optional = true, features = ["serde_derive"] } smol_str = "0.2.0" +winit-core.workspace = true [dev-dependencies] image = { version = "0.24.0", default-features = false, features = ["png"] } diff --git a/winit/src/error.rs b/winit/src/error.rs index bb8dc42d5..af2b0e616 100644 --- a/winit/src/error.rs +++ b/winit/src/error.rs @@ -1,6 +1,8 @@ +use crate::platform_impl; use std::{error, fmt}; -use crate::platform_impl; +#[doc(inline)] +pub use winit_core::error::NotSupportedError; // TODO: Rename /// An error that may be generated when requesting Winit state @@ -14,12 +16,6 @@ pub enum ExternalError { Os(OsError), } -/// The error type for when the requested operation is not supported by the backend. -#[derive(Clone)] -pub struct NotSupportedError { - _marker: (), -} - /// The error type for when the OS cannot perform the requested operation. #[derive(Debug)] pub struct OsError { @@ -47,14 +43,6 @@ impl From for EventLoopError { } } -impl NotSupportedError { - #[inline] - #[allow(dead_code)] - pub(crate) fn new() -> NotSupportedError { - NotSupportedError { _marker: () } - } -} - impl OsError { #[allow(dead_code)] pub(crate) fn new(line: u32, file: &'static str, error: platform_impl::OsError) -> OsError { @@ -88,18 +76,6 @@ impl fmt::Display for ExternalError { } } -impl fmt::Debug for NotSupportedError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - f.debug_struct("NotSupportedError").finish() - } -} - -impl fmt::Display for NotSupportedError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - f.pad("the requested operation is not supported by Winit") - } -} - impl fmt::Display for EventLoopError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { @@ -113,7 +89,6 @@ impl fmt::Display for EventLoopError { impl error::Error for OsError {} impl error::Error for ExternalError {} -impl error::Error for NotSupportedError {} impl error::Error for EventLoopError {} #[cfg(test)] @@ -125,11 +100,6 @@ mod tests { // Eat attributes for testing #[test] fn ensure_fmt_does_not_panic() { - let _ = format!( - "{:?}, {}", - NotSupportedError::new(), - NotSupportedError::new().clone() - ); let _ = format!( "{:?}, {}", ExternalError::NotSupported(NotSupportedError::new()),