Skip to content

Commit

Permalink
put From impl back, inform users of deprecation with a doc comment an…
Browse files Browse the repository at this point in the history
…d 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.
  • Loading branch information
antonilol committed Aug 20, 2024
1 parent 264936d commit 5a2f144
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
18 changes: 14 additions & 4 deletions src/sdl2/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,9 @@ pub enum SwapInterval {
LateSwapTearing = -1,
}

impl TryFrom<i32> for SwapInterval {
type Error = SwapIntervalConversionError;

fn try_from(value: i32) -> Result<Self, Self::Error> {
impl SwapInterval {
/// This function will be replaced later with a [`TryFrom`] implementation
pub fn try_from(value: i32) -> Result<Self, SwapIntervalConversionError> {
Ok(match value {
-1 => SwapInterval::LateSwapTearing,
0 => SwapInterval::Immediate,
Expand All @@ -620,6 +619,17 @@ impl fmt::Display for SwapIntervalConversionError {

impl Error for SwapIntervalConversionError {}

impl From<i32> 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)]
Expand Down

0 comments on commit 5a2f144

Please sign in to comment.