Skip to content

Commit bbb1a1b

Browse files
committed
Deprecate From implementation of SwapInterval that could panic, add TryFrom-like inherent function.
1 parent 5dbf6e5 commit bbb1a1b

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another.
33

44
### v0.39.0
55

6+
[PR #1413](https://github.com/Rust-SDL2/rust-sdl2/pull/1413) Deprecate `From` implementation of `SwapInterval` that could panic, add `TryFrom`-like inherent function.
7+
68
[PR #1510](https://github.com/Rust-SDL2/rust-sdl2/pull/1510) Fix clippy warnings.
79

810
[PR #1509](https://github.com/Rust-SDL2/rust-sdl2/pull/1509) **BREAKING CHANGE** Add Send + 'static bounds for EventWatchCallback and 'static bound to AudioCallback to avoid soundness hole.

src/sdl2/video.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -592,17 +592,37 @@ pub enum SwapInterval {
592592
LateSwapTearing = -1,
593593
}
594594

595-
impl From<i32> for SwapInterval {
596-
fn from(i: i32) -> Self {
597-
match i {
595+
impl SwapInterval {
596+
/// This function will be replaced with a [`TryFrom`] implementation later.
597+
pub fn try_from(value: i32) -> Result<Self, SwapIntervalConversionError> {
598+
Ok(match value {
598599
-1 => SwapInterval::LateSwapTearing,
599600
0 => SwapInterval::Immediate,
600601
1 => SwapInterval::VSync,
601-
other => panic!(
602-
"Invalid value for SwapInterval: {}; valid values are -1, 0, 1",
603-
other
604-
),
605-
}
602+
_ => return Err(SwapIntervalConversionError(value)),
603+
})
604+
}
605+
}
606+
607+
#[derive(Debug, Clone)]
608+
pub struct SwapIntervalConversionError(pub i32);
609+
610+
impl fmt::Display for SwapIntervalConversionError {
611+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
612+
write!(
613+
f,
614+
"Invalid value for SwapInterval: {}; valid values are -1, 0, 1",
615+
self.0
616+
)
617+
}
618+
}
619+
620+
impl Error for SwapIntervalConversionError {}
621+
622+
impl From<i32> for SwapInterval {
623+
/// This function is deprecated, use [`SwapInterval::try_from`] instead and handle the error.
624+
fn from(i: i32) -> Self {
625+
Self::try_from(i).unwrap()
606626
}
607627
}
608628

0 commit comments

Comments
 (0)