From 5b946bd06e949e905603811199336a20d1300e46 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sat, 15 Nov 2025 12:19:28 +0100 Subject: [PATCH] Deprecate `From` implementation of `SwapInterval` that could panic, add `TryFrom`-like inherent function. --- changelog.md | 2 ++ src/sdl2/video.rs | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/changelog.md b/changelog.md index 6fd043b790..e18fa94fd2 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another. ### v0.39.0 +[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 #1507](https://github.com/Rust-SDL2/rust-sdl2/pull/1507) **BREAKING CHANGE** Add binding for `SDL_ComposeCustomBlendMode`. This should only be a breaking change for users relying on internal details of `BlendMode` and `SDL_BlendMode`. [PR #1510](https://github.com/Rust-SDL2/rust-sdl2/pull/1510) Fix clippy warnings. diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index abb6321d6f..460354de4c 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 with a [`TryFrom`] implementation later. + 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() } }