From fbbcf09cc7e701d4b146adb2b8f6ac2bf438d40c Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sat, 29 Jun 2024 16:36:30 +0200 Subject: [PATCH 1/4] replace From implementation of SwapInterval that could panic with TryFrom --- src/sdl2/video.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index c758a61425..30326ce8a0 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -592,20 +592,34 @@ pub enum SwapInterval { LateSwapTearing = -1, } -impl From for SwapInterval { - fn from(i: i32) -> Self { - match i { +impl TryFrom for SwapInterval { + type Error = SwapIntervalConversionError; + + 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 {} + /// Represents orientation of a display. #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[repr(i32)] From 264936d96ec8b9a14d3c816d3f2853fe69bcdd45 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sat, 29 Jun 2024 16:41:23 +0200 Subject: [PATCH 2/4] update changelog --- changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/changelog.md b/changelog.md index 12808ac830..da6705e334 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) **BREAKING CHANGE** Replace `From` implementation of `SwapInterval` that could panic with `TryFrom`. + [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. From 5a2f144d60f6b9369a9c755e956e52a6495ffcb6 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Thu, 11 Jul 2024 21:22:59 +0200 Subject: [PATCH 3/4] put From impl back, inform users of deprecation with a doc comment and a println Notes: - Trait implementations can't be deprecated, so had to use some other way. - try_from is now an inherent function because From and TryFrom can't be implemented at the same time. --- changelog.md | 2 +- src/sdl2/video.rs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index da6705e334..3be2fce4d7 100644 --- a/changelog.md +++ b/changelog.md @@ -3,7 +3,7 @@ when upgrading from a version of rust-sdl2 to another. ### Next -[PR #1413](https://github.com/Rust-SDL2/rust-sdl2/pull/1413) **BREAKING CHANGE** Replace `From` implementation of `SwapInterval` that could panic with `TryFrom`. +[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. diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index 30326ce8a0..ca38d4b11e 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -592,10 +592,9 @@ pub enum SwapInterval { LateSwapTearing = -1, } -impl TryFrom for SwapInterval { - type Error = SwapIntervalConversionError; - - fn try_from(value: i32) -> Result { +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, @@ -620,6 +619,17 @@ impl fmt::Display for SwapIntervalConversionError { 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 { + println!( + "SwapInterval::from is deprecated (could be called from .into()), \ + use SwapInterval::try_from instead and handle the error" + ); + Self::try_from(i).unwrap() + } +} + /// Represents orientation of a display. #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[repr(i32)] From 821289352bb669c023eff6f00111ebaca9515ca5 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Fri, 12 Jul 2024 20:42:34 +0200 Subject: [PATCH 4/4] remove println from deprecated SwapInterval::from --- src/sdl2/video.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index ca38d4b11e..f90659b170 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -622,10 +622,6 @@ 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 { - println!( - "SwapInterval::from is deprecated (could be called from .into()), \ - use SwapInterval::try_from instead and handle the error" - ); Self::try_from(i).unwrap() } }