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 Jul 11, 2024
1 parent 51451e4 commit dba99f0
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 @@ -591,10 +591,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 @@ -619,6 +618,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 dba99f0

Please sign in to comment.