From ed95966e497bc3bc2695d673ea36a731793723db Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 7 Jun 2024 11:55:53 -0400 Subject: [PATCH 1/3] Support converting arrays of integers to Rect via TryFrom --- src/sdl2/rect.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/sdl2/rect.rs b/src/sdl2/rect.rs index c0519e6fbc..81fdee985e 100644 --- a/src/sdl2/rect.rs +++ b/src/sdl2/rect.rs @@ -1,13 +1,14 @@ //! Rectangles and points. use crate::sys; -use std::convert::{AsMut, AsRef}; +use std::convert::{AsMut, AsRef, TryFrom, TryInto}; use std::hash::{Hash, Hasher}; use std::mem; use std::ops::{ Add, AddAssign, BitAnd, BitOr, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign, }; +use std::num::TryFromIntError; use std::ptr; /// The maximal integer value that can be used for rectangles. @@ -698,6 +699,29 @@ impl From<(i32, i32, u32, u32)> for Rect { } } +/// Try to convert an array of numbers to a rectangle +/// +/// Example: +/// ```rust +/// use sdl2::rect::Rect; +/// let rect1: Rect = [5_u8; 4].into().unwrap(); +/// let rect2: Rect = [5_u64; 4].into().unwrap(); +/// +/// assert_eq!(rect1, rect2); +/// ``` +impl TryFrom<[T; 4]> for Rect where + T: TryInto + TryInto, + // need to do this to support Infallible conversions + TryFromIntError: From<>::Error>, + TryFromIntError: From<>::Error>, +{ + type Error = TryFromIntError; + fn try_from([x, y, width, height]: [T; 4]) -> Result { + Ok(Rect::new(x.try_into()?, y.try_into()?, + width.try_into()?, height.try_into()?)) + } +} + impl AsRef for Rect { fn as_ref(&self) -> &sys::SDL_Rect { &self.raw From 6828b5cfe3b7549bd83c9dcfb5c80a8ca979e14d Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 7 Jun 2024 11:58:09 -0400 Subject: [PATCH 2/3] Add PR #1403 to changelog Done as a seperate commit since it needs to reference the PR number. --- changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/changelog.md b/changelog.md index 6235875112..c36fa9582b 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another. ### Unreleased +[PR #1403](https://github.com/Rust-SDL2/rust-sdl2/pull/1403) Support converting arrays of integers to Rect via TryFrom. + [PR #1368](https://github.com/Rust-SDL2/rust-sdl2/pull/1368) Remove unnecessary unsafe in Window interface. Make Window `Clone`. [PR #1366](https://github.com/Rust-SDL2/rust-sdl2/pull/1366) Add Primary Selection bindings. From bd1b825f3040b89f7d91ffde047955c7493100b5 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 11 Jun 2024 14:56:37 -0400 Subject: [PATCH 3/3] fix formatting --- src/sdl2/rect.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/sdl2/rect.rs b/src/sdl2/rect.rs index 81fdee985e..3f3264ddfd 100644 --- a/src/sdl2/rect.rs +++ b/src/sdl2/rect.rs @@ -4,11 +4,11 @@ use crate::sys; use std::convert::{AsMut, AsRef, TryFrom, TryInto}; use std::hash::{Hash, Hasher}; use std::mem; +use std::num::TryFromIntError; use std::ops::{ Add, AddAssign, BitAnd, BitOr, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign, }; -use std::num::TryFromIntError; use std::ptr; /// The maximal integer value that can be used for rectangles. @@ -709,17 +709,22 @@ impl From<(i32, i32, u32, u32)> for Rect { /// /// assert_eq!(rect1, rect2); /// ``` -impl TryFrom<[T; 4]> for Rect where - T: TryInto + TryInto, - // need to do this to support Infallible conversions - TryFromIntError: From<>::Error>, - TryFromIntError: From<>::Error>, +impl TryFrom<[T; 4]> for Rect +where + T: TryInto + TryInto, + // need to do this to support Infallible conversions + TryFromIntError: From<>::Error>, + TryFromIntError: From<>::Error>, { - type Error = TryFromIntError; - fn try_from([x, y, width, height]: [T; 4]) -> Result { - Ok(Rect::new(x.try_into()?, y.try_into()?, - width.try_into()?, height.try_into()?)) - } + type Error = TryFromIntError; + fn try_from([x, y, width, height]: [T; 4]) -> Result { + Ok(Rect::new( + x.try_into()?, + y.try_into()?, + width.try_into()?, + height.try_into()?, + )) + } } impl AsRef for Rect {