diff --git a/changelog.md b/changelog.md index 12808ac830..3be2fce4d7 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another. ### Next +[PR #1413](https://github.com/Rust-SDL2/rust-sdl2/pull/1413) Deprecate `From` implementation of `SwapInterval` that could panic, add `TryFrom`-like inherent function. + [PR #1416](https://github.com/Rust-SDL2/rust-sdl2/pull/1416) Apply clippy fixes, fix deprecations and other code quality improvements. [PR #1408](https://github.com/Rust-SDL2/rust-sdl2/pull/1408) Allow comparing `Version`s, add constant with the version the bindings were compiled with. diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index c758a61425..f90659b170 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -592,17 +592,37 @@ pub enum SwapInterval { LateSwapTearing = -1, } -impl From for SwapInterval { - fn from(i: i32) -> Self { - match i { +impl SwapInterval { + /// This function will be replaced later with a [`TryFrom`] implementation + pub fn try_from(value: i32) -> Result { + Ok(match value { -1 => SwapInterval::LateSwapTearing, 0 => SwapInterval::Immediate, 1 => SwapInterval::VSync, - other => panic!( - "Invalid value for SwapInterval: {}; valid values are -1, 0, 1", - other - ), - } + _ => return Err(SwapIntervalConversionError(value)), + }) + } +} + +#[derive(Debug, Clone)] +pub struct SwapIntervalConversionError(pub i32); + +impl fmt::Display for SwapIntervalConversionError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Invalid value for SwapInterval: {}; valid values are -1, 0, 1", + self.0 + ) + } +} + +impl Error for SwapIntervalConversionError {} + +impl From for SwapInterval { + /// This function is deprecated, use [`SwapInterval::try_from`] instead and handle the error. + fn from(i: i32) -> Self { + Self::try_from(i).unwrap() } }