diff --git a/src/sdl2/common.rs b/src/sdl2/common.rs index ff2daf8c06..9b4a1e13f4 100644 --- a/src/sdl2/common.rs +++ b/src/sdl2/common.rs @@ -1,16 +1,92 @@ -use std::error::Error; -use std::fmt; +use std::ffi::{CString, NulError}; +use std::{error, fmt, io}; -/// A given integer was so big that its representation as a C integer would be -/// negative. -#[derive(Debug, Clone, PartialEq)] -pub enum IntegerOrSdlError { - IntegerOverflows(&'static str, u32), - SdlError(String), +#[derive(Debug)] +pub struct SdlError { + msg: String, } + +impl SdlError { + pub fn from_last_error() -> Self { + let msg = crate::get_error(); + Self { msg } + } + + pub(crate) fn from_string(msg: String) -> Self { + Self { msg } + } + + pub fn message(&self) -> &str { + &self.msg + } +} + +impl fmt::Display for SdlError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.msg) + } +} + +impl error::Error for SdlError {} + +#[derive(Debug)] +pub enum Error { + Sdl(SdlError), + Io(io::Error), + /// An integer was larger than [`i32::MAX`] in a parameter, and it can't be converted to a C int + IntOverflow(&'static str, u32), + /// A null byte was found within a parameter, and it can't be sent to SDL + InvalidString(NulError, &'static str), +} + +impl Error { + pub fn from_sdl_error() -> Self { + Self::Sdl(SdlError::from_last_error()) + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Sdl(msg) => write!(f, "SDL error: {msg}"), + Self::Io(err) => write!(f, "IO error: {err}"), + Self::IntOverflow(name, value) => write!(f, "Integer '{name}' overflows: {value}"), + Self::InvalidString(name, nul) => write!(f, "Invalid string '{name}': {nul}"), + } + } +} + +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + match self { + Self::Sdl(..) | Self::IntOverflow(..) => None, + Self::Io(err) => Some(err), + Self::InvalidString(nul, _) => Some(nul), + } + } +} + +impl From for Error { + fn from(value: SdlError) -> Self { + Self::Sdl(value) + } +} + +impl From for Error { + fn from(value: io::Error) -> Self { + Self::Io(value) + } +} + +pub fn validate_string(str: impl Into>, name: &'static str) -> Result { + match CString::new(str) { + Ok(c) => Ok(c), + Err(nul) => Err(Error::InvalidString(nul, name)), + } +} + /// Validates and converts the given u32 to a positive C integer. -pub fn validate_int(value: u32, name: &'static str) -> Result<::libc::c_int, IntegerOrSdlError> { - use self::IntegerOrSdlError::*; +pub fn validate_int(value: u32, name: &'static str) -> Result { // Many SDL functions will accept `int` values, even if it doesn't make sense // for the values to be negative. // In the cases that SDL doesn't check negativity, passing negative values @@ -18,22 +94,9 @@ pub fn validate_int(value: u32, name: &'static str) -> Result<::libc::c_int, Int // For example, `SDL_JoystickGetButton` uses the index argument to access an // array without checking if it's negative, which could potentially lead to // segmentation faults. - if value >= 1 << 31 { - Err(IntegerOverflows(name, value)) + if value > libc::c_int::MAX as u32 { + Err(Error::IntOverflow(name, value)) } else { - Ok(value as ::libc::c_int) - } -} - -impl fmt::Display for IntegerOrSdlError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::IntegerOrSdlError::*; - - match *self { - IntegerOverflows(name, value) => write!(f, "Integer '{}' overflows ({})", name, value), - SdlError(ref e) => write!(f, "SDL error: {}", e), - } + Ok(value as libc::c_int) } } - -impl Error for IntegerOrSdlError {} diff --git a/src/sdl2/controller.rs b/src/sdl2/controller.rs index 33c60ca94f..89dcc404a5 100644 --- a/src/sdl2/controller.rs +++ b/src/sdl2/controller.rs @@ -11,10 +11,10 @@ use crate::sensor::SensorType; #[cfg(feature = "hidapi")] use std::convert::TryInto; -use crate::common::{validate_int, IntegerOrSdlError}; -use crate::get_error; +use crate::common::validate_int; use crate::joystick; use crate::GameControllerSubsystem; +use crate::{get_error, Error}; use std::mem::transmute; use crate::sys; @@ -78,13 +78,12 @@ impl GameControllerSubsystem { /// Controller IDs are the same as joystick IDs and the maximum number can /// be retrieved using the `SDL_NumJoysticks` function. #[doc(alias = "SDL_GameControllerOpen")] - pub fn open(&self, joystick_index: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn open(&self, joystick_index: u32) -> Result { let joystick_index = validate_int(joystick_index, "joystick_index")?; let controller = unsafe { sys::SDL_GameControllerOpen(joystick_index) }; if controller.is_null() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(GameController { subsystem: self.clone(), @@ -95,13 +94,12 @@ impl GameControllerSubsystem { /// Return the name of the controller at index `joystick_index`. #[doc(alias = "SDL_GameControllerNameForIndex")] - pub fn name_for_index(&self, joystick_index: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn name_for_index(&self, joystick_index: u32) -> Result { let joystick_index = validate_int(joystick_index, "joystick_index")?; let c_str = unsafe { sys::SDL_GameControllerNameForIndex(joystick_index) }; if c_str.is_null() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(unsafe { CStr::from_ptr(c_str as *const _) @@ -170,9 +168,10 @@ impl GameControllerSubsystem { use self::AddMappingError::*; let result = unsafe { sys::SDL_GameControllerAddMappingsFromRW(rw.raw(), 0) }; - match result { - -1 => Err(SdlError(get_error())), - _ => Ok(result), + if result == -1 { + Err(SdlError(get_error())) + } else { + Ok(result) } } @@ -509,7 +508,7 @@ impl GameController { low_frequency_rumble: u16, high_frequency_rumble: u16, duration_ms: u32, - ) -> Result<(), IntegerOrSdlError> { + ) -> Result<(), Error> { let result = unsafe { sys::SDL_GameControllerRumble( self.raw, @@ -520,7 +519,7 @@ impl GameController { }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } @@ -533,13 +532,13 @@ impl GameController { left_rumble: u16, right_rumble: u16, duration_ms: u32, - ) -> Result<(), IntegerOrSdlError> { + ) -> Result<(), Error> { let result = unsafe { sys::SDL_GameControllerRumbleTriggers(self.raw, left_rumble, right_rumble, duration_ms) }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } @@ -580,11 +579,11 @@ impl GameController { /// Update a game controller's LED color. #[doc(alias = "SDL_GameControllerSetLED")] - pub fn set_led(&mut self, red: u8, green: u8, blue: u8) -> Result<(), IntegerOrSdlError> { + pub fn set_led(&mut self, red: u8, green: u8, blue: u8) -> Result<(), Error> { let result = unsafe { sys::SDL_GameControllerSetLED(self.raw, red, green, blue) }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } @@ -637,7 +636,7 @@ impl GameController { &self, sensor_type: crate::sensor::SensorType, enabled: bool, - ) -> Result<(), IntegerOrSdlError> { + ) -> Result<(), Error> { let result = unsafe { sys::SDL_GameControllerSetSensorEnabled( self.raw, @@ -651,7 +650,7 @@ impl GameController { }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } @@ -668,11 +667,7 @@ impl GameController { /// The number of data points depends on the sensor. Both Gyroscope and /// Accelerometer return 3 values, one for each axis. #[doc(alias = "SDL_GameControllerGetSensorData")] - pub fn sensor_get_data( - &self, - sensor_type: SensorType, - data: &mut [f32], - ) -> Result<(), IntegerOrSdlError> { + pub fn sensor_get_data(&self, sensor_type: SensorType, data: &mut [f32]) -> Result<(), Error> { let result = unsafe { sys::SDL_GameControllerGetSensorData( self.raw, @@ -683,7 +678,7 @@ impl GameController { }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } diff --git a/src/sdl2/filesystem.rs b/src/sdl2/filesystem.rs index 27d46c5898..886d766a8e 100644 --- a/src/sdl2/filesystem.rs +++ b/src/sdl2/filesystem.rs @@ -1,9 +1,6 @@ -use crate::get_error; -use libc::c_char; +use crate::{get_error, Error}; use libc::c_void; -use std::error; -use std::ffi::{CStr, CString, NulError}; -use std::fmt; +use std::ffi::CStr; use crate::sys; @@ -23,58 +20,20 @@ pub fn base_path() -> Result { } } -#[derive(Debug, Clone)] -pub enum PrefPathError { - InvalidOrganizationName(NulError), - InvalidApplicationName(NulError), - SdlError(String), -} - -impl fmt::Display for PrefPathError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::PrefPathError::*; - - match *self { - InvalidOrganizationName(ref e) => write!(f, "Invalid organization name: {}", e), - InvalidApplicationName(ref e) => write!(f, "Invalid application name: {}", e), - SdlError(ref e) => write!(f, "SDL error: {}", e), - } - } -} - -impl error::Error for PrefPathError { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match self { - Self::InvalidOrganizationName(err) => Some(err), - Self::InvalidApplicationName(err) => Some(err), - Self::SdlError(_) => None, - } - } -} - // TODO: Change to OsStr or something? /// Return the preferred directory for the application to write files on this /// system, based on the given organization and application name. #[doc(alias = "SDL_GetPrefPath")] -pub fn pref_path(org_name: &str, app_name: &str) -> Result { - use self::PrefPathError::*; - let result = unsafe { - let org = match CString::new(org_name) { - Ok(s) => s, - Err(err) => return Err(InvalidOrganizationName(err)), - }; - let app = match CString::new(app_name) { - Ok(s) => s, - Err(err) => return Err(InvalidApplicationName(err)), - }; - let buf = - sys::SDL_GetPrefPath(org.as_ptr() as *const c_char, app.as_ptr() as *const c_char); - CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned() - }; - - if result.is_empty() { - Err(SdlError(get_error())) - } else { - Ok(result) +pub fn pref_path(org_name: &str, app_name: &str) -> Result { + unsafe { + let buf = sys::SDL_GetPrefPath( + as_cstring!(org_name)?.as_ptr(), + as_cstring!(app_name)?.as_ptr(), + ); + if buf.is_null() { + Err(Error::from_sdl_error()) + } else { + Ok(CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned()) + } } } diff --git a/src/sdl2/haptic.rs b/src/sdl2/haptic.rs index 6c5f857d25..dbdd44b9e0 100644 --- a/src/sdl2/haptic.rs +++ b/src/sdl2/haptic.rs @@ -1,15 +1,14 @@ //! Haptic Functions use crate::sys; -use crate::common::{validate_int, IntegerOrSdlError}; -use crate::get_error; +use crate::common::validate_int; +use crate::Error; use crate::HapticSubsystem; impl HapticSubsystem { /// Attempt to open the joystick at index `joystick_index` and return its haptic device. #[doc(alias = "SDL_JoystickOpen")] - pub fn open_from_joystick_id(&self, joystick_index: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn open_from_joystick_id(&self, joystick_index: u32) -> Result { let joystick_index = validate_int(joystick_index, "joystick_index")?; let haptic = unsafe { @@ -18,7 +17,7 @@ impl HapticSubsystem { }; if haptic.is_null() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { unsafe { sys::SDL_HapticRumbleInit(haptic) }; Ok(Haptic { diff --git a/src/sdl2/joystick.rs b/src/sdl2/joystick.rs index 82e20b449a..8e843012e5 100644 --- a/src/sdl2/joystick.rs +++ b/src/sdl2/joystick.rs @@ -2,12 +2,12 @@ use crate::sys; use crate::sys::SDL_JoystickPowerLevel; use crate::clear_error; -use crate::common::{validate_int, IntegerOrSdlError}; +use crate::common::{validate_int, Error, SdlError}; use crate::get_error; use crate::JoystickSubsystem; use libc::c_char; -use std::ffi::{CStr, CString, NulError}; -use std::fmt::{Display, Error, Formatter}; +use std::ffi::CStr; +use std::fmt; impl JoystickSubsystem { /// Retrieve the total number of attached joysticks *and* controllers identified by SDL. @@ -24,14 +24,13 @@ impl JoystickSubsystem { /// Attempt to open the joystick at index `joystick_index` and return it. #[doc(alias = "SDL_JoystickOpen")] - pub fn open(&self, joystick_index: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn open(&self, joystick_index: u32) -> Result { let joystick_index = validate_int(joystick_index, "joystick_index")?; let joystick = unsafe { sys::SDL_JoystickOpen(joystick_index) }; if joystick.is_null() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(Joystick { subsystem: self.clone(), @@ -42,14 +41,13 @@ impl JoystickSubsystem { /// Return the name of the joystick at index `joystick_index`. #[doc(alias = "SDL_JoystickNameForIndex")] - pub fn name_for_index(&self, joystick_index: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn name_for_index(&self, joystick_index: u32) -> Result { let joystick_index = validate_int(joystick_index, "joystick_index")?; let c_str = unsafe { sys::SDL_JoystickNameForIndex(joystick_index) }; if c_str.is_null() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(unsafe { CStr::from_ptr(c_str as *const _) @@ -62,8 +60,7 @@ impl JoystickSubsystem { /// Get the GUID for the joystick at index `joystick_index` #[doc(alias = "SDL_JoystickGetDeviceGUID")] - pub fn device_guid(&self, joystick_index: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn device_guid(&self, joystick_index: u32) -> Result { let joystick_index = validate_int(joystick_index, "joystick_index")?; let raw = unsafe { sys::SDL_JoystickGetDeviceGUID(joystick_index) }; @@ -71,7 +68,7 @@ impl JoystickSubsystem { let guid = Guid { raw }; if guid.is_zero() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(guid) } @@ -191,8 +188,7 @@ impl Joystick { /// Retrieve the battery level of this joystick #[doc(alias = "SDL_JoystickCurrentPowerLevel")] - pub fn power_level(&self) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn power_level(&self) -> Result { clear_error(); let result = unsafe { sys::SDL_JoystickCurrentPowerLevel(self.raw) }; @@ -207,7 +203,7 @@ impl Joystick { if err.is_empty() { Ok(state) } else { - Err(SdlError(err)) + Err(Error::Sdl(SdlError::from_string(err))) } } } @@ -229,8 +225,7 @@ impl Joystick { /// /// The function will fail if the joystick doesn't have the provided axis. #[doc(alias = "SDL_JoystickGetAxis")] - pub fn axis(&self, axis: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn axis(&self, axis: u32) -> Result { // This interface is a bit messed up: 0 is a valid position // but can also mean that an error occured. As far as I can // tell the only way to know if an error happened is to see if @@ -248,7 +243,7 @@ impl Joystick { if err.is_empty() { Ok(pos) } else { - Err(SdlError(err)) + Err(Error::Sdl(SdlError::from_string(err))) } } } @@ -270,8 +265,7 @@ impl Joystick { /// /// The function will fail if the joystick doesn't have the provided button. #[doc(alias = "SDL_JoystickGetButton")] - pub fn button(&self, button: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn button(&self, button: u32) -> Result { // Same deal as axis, 0 can mean both unpressed or // error... clear_error(); @@ -288,7 +282,7 @@ impl Joystick { // Button is not pressed Ok(false) } else { - Err(SdlError(err)) + Err(Error::Sdl(SdlError::from_string(err))) } } // Should be unreachable @@ -312,8 +306,7 @@ impl Joystick { /// Return a pair `(dx, dy)` containing the difference in axis /// position since the last poll #[doc(alias = "SDL_JoystickGetBall")] - pub fn ball(&self, ball: u32) -> Result<(i32, i32), IntegerOrSdlError> { - use crate::common::IntegerOrSdlError::*; + pub fn ball(&self, ball: u32) -> Result<(i32, i32), Error> { let mut dx = 0; let mut dy = 0; @@ -323,7 +316,7 @@ impl Joystick { if result == 0 { Ok((dx, dy)) } else { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } } @@ -342,8 +335,7 @@ impl Joystick { /// Return the position of `hat` for this joystick #[doc(alias = "SDL_JoystickGetHat")] - pub fn hat(&self, hat: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn hat(&self, hat: u32) -> Result { // Guess what? This function as well uses 0 to report an error // but 0 is also a valid value (HatState::Centered). So we // have to use the same hack as `axis`... @@ -362,7 +354,7 @@ impl Joystick { if err.is_empty() { Ok(state) } else { - Err(SdlError(err)) + Err(Error::Sdl(SdlError::from_string(err))) } } } @@ -384,7 +376,7 @@ impl Joystick { low_frequency_rumble: u16, high_frequency_rumble: u16, duration_ms: u32, - ) -> Result<(), IntegerOrSdlError> { + ) -> Result<(), Error> { let result = unsafe { sys::SDL_JoystickRumble( self.raw, @@ -395,7 +387,7 @@ impl Joystick { }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } @@ -408,13 +400,13 @@ impl Joystick { left_rumble: u16, right_rumble: u16, duration_ms: u32, - ) -> Result<(), IntegerOrSdlError> { + ) -> Result<(), Error> { let result = unsafe { sys::SDL_JoystickRumbleTriggers(self.raw, left_rumble, right_rumble, duration_ms) }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } @@ -455,11 +447,11 @@ impl Joystick { /// Update a joystick's LED color. #[doc(alias = "SDL_JoystickSetLED")] - pub fn set_led(&mut self, red: u8, green: u8, blue: u8) -> Result<(), IntegerOrSdlError> { + pub fn set_led(&mut self, red: u8, green: u8, blue: u8) -> Result<(), Error> { let result = unsafe { sys::SDL_JoystickSetLED(self.raw, red, green, blue) }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } @@ -467,7 +459,7 @@ impl Joystick { /// Send a joystick specific effect packet. #[doc(alias = "SDL_JoystickSendEffect")] - pub fn send_effect(&mut self, data: &[u8]) -> Result<(), IntegerOrSdlError> { + pub fn send_effect(&mut self, data: &[u8]) -> Result<(), Error> { let result = unsafe { sys::SDL_JoystickSendEffect( self.raw, @@ -477,7 +469,7 @@ impl Joystick { }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(()) } @@ -511,8 +503,8 @@ impl Eq for Guid {} impl Guid { /// Create a GUID from a string representation. #[doc(alias = "SDL_JoystickGetGUIDFromString")] - pub fn from_string(guid: &str) -> Result { - let guid = CString::new(guid)?; + pub fn from_string(guid: &str) -> Result { + let guid = as_cstring!(guid)?; let raw = unsafe { sys::SDL_JoystickGetGUIDFromString(guid.as_ptr() as *const c_char) }; @@ -566,8 +558,8 @@ impl Guid { } } -impl Display for Guid { - fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { +impl fmt::Display for Guid { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{}", self.string()) } } diff --git a/src/sdl2/lib.rs b/src/sdl2/lib.rs index 20ef28b11f..ce2b87de59 100644 --- a/src/sdl2/lib.rs +++ b/src/sdl2/lib.rs @@ -103,7 +103,7 @@ pub mod ttf; mod common; // Export return types and such from the common module. -pub use crate::common::IntegerOrSdlError; +pub use crate::common::Error; #[cfg(feature = "raw-window-handle")] pub mod raw_window_handle; diff --git a/src/sdl2/macros.rs b/src/sdl2/macros.rs index 673ca3c116..444aa62688 100644 --- a/src/sdl2/macros.rs +++ b/src/sdl2/macros.rs @@ -24,3 +24,9 @@ macro_rules! impl_raw_constructor( )+ ) ); + +macro_rules! as_cstring { + ($i:ident) => { + $crate::common::validate_string($i, stringify!($i)) + }; +} diff --git a/src/sdl2/messagebox.rs b/src/sdl2/messagebox.rs index 8eff0e20c7..df0a922f61 100644 --- a/src/sdl2/messagebox.rs +++ b/src/sdl2/messagebox.rs @@ -1,14 +1,12 @@ // 0 should not be used in bitflags, but here it is. Removing it will break existing code. #![allow(clippy::bad_bit_mask)] -use std::error; -use std::ffi::{CString, NulError}; -use std::fmt; use std::os::raw::{c_char, c_int}; use std::ptr; -use crate::get_error; +use crate::common::validate_string; use crate::video::Window; +use crate::Error; use crate::sys; @@ -104,40 +102,6 @@ impl From<[sys::SDL_MessageBoxColor; 5]> for MessageBoxColorScheme { } } -#[derive(Debug, Clone)] -pub enum ShowMessageError { - InvalidTitle(NulError), - InvalidMessage(NulError), - /// Second argument of the tuple (i32) corresponds to the - /// first button_id having an error - InvalidButton(NulError, i32), - SdlError(String), -} - -impl fmt::Display for ShowMessageError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::ShowMessageError::*; - - match *self { - InvalidTitle(ref e) => write!(f, "Invalid title: {}", e), - InvalidMessage(ref e) => write!(f, "Invalid message: {}", e), - InvalidButton(ref e, value) => write!(f, "Invalid button ({}): {}", value, e), - SdlError(ref e) => write!(f, "SDL error: {}", e), - } - } -} - -impl error::Error for ShowMessageError { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match self { - Self::InvalidTitle(err) => Some(err), - Self::InvalidMessage(err) => Some(err), - Self::InvalidButton(err, _) => Some(err), - Self::SdlError(_) => None, - } - } -} - /// Show a simple message box, meant to be informative only. /// /// There is no way to know if the user clicked "Ok" or closed the message box, @@ -149,32 +113,25 @@ pub fn show_simple_message_box<'a, W>( title: &str, message: &str, window: W, -) -> Result<(), ShowMessageError> +) -> Result<(), Error> where W: Into>, { - use self::ShowMessageError::*; + let title = as_cstring!(title)?; + let message = as_cstring!(message)?; let result = unsafe { - let title = match CString::new(title) { - Ok(s) => s, - Err(err) => return Err(InvalidTitle(err)), - }; - let message = match CString::new(message) { - Ok(s) => s, - Err(err) => return Err(InvalidMessage(err)), - }; sys::SDL_ShowSimpleMessageBox( flags.bits(), title.as_ptr() as *const c_char, message.as_ptr() as *const c_char, window.into().map_or(ptr::null_mut(), |win| win.raw()), ) - } == 0; + }; - if result { + if result == 0 { Ok(()) } else { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } } @@ -195,7 +152,7 @@ pub fn show_message_box<'a, 'b, W, M>( message: &str, window: W, scheme: M, -) -> Result, ShowMessageError> +) -> Result, Error> where W: Into>, M: Into>, @@ -203,24 +160,13 @@ where let window = window.into(); let scheme = scheme.into(); - use self::ShowMessageError::*; let mut button_id: c_int = 0; - let title = match CString::new(title) { - Ok(s) => s, - Err(err) => return Err(InvalidTitle(err)), - }; - let message = match CString::new(message) { - Ok(s) => s, - Err(err) => return Err(InvalidMessage(err)), - }; - let button_texts: Result, (_, i32)> = buttons + let title = as_cstring!(title)?; + let message = as_cstring!(message)?; + let button_texts = buttons .iter() - .map(|b| CString::new(b.text).map_err(|e| (e, b.button_id))) - .collect(); // Create CString for every button; and catch any CString Error - let button_texts = match button_texts { - Ok(b) => b, - Err(e) => return Err(InvalidButton(e.0, e.1)), - }; + .map(|b| validate_string(b.text, "buttons")) + .collect::, _>>()?; let raw_buttons: Vec = buttons .iter() .zip(button_texts.iter()) @@ -230,25 +176,24 @@ where text: b_text.as_ptr(), }) .collect(); - let result = unsafe { - let scheme = scheme.map(|scheme| sys::SDL_MessageBoxColorScheme { - colors: scheme.into(), - }); - let msg_box_data = sys::SDL_MessageBoxData { - flags: flags.bits(), - window: window.map_or(ptr::null_mut(), |win| win.raw()), - title: title.as_ptr() as *const c_char, - message: message.as_ptr() as *const c_char, - numbuttons: raw_buttons.len() as c_int, - buttons: raw_buttons.as_ptr(), - colorScheme: scheme - .as_ref() - .map(|p| p as *const _) - .unwrap_or(ptr::null()), - }; - sys::SDL_ShowMessageBox(&msg_box_data as *const _, &mut button_id as &mut _) - } == 0; - if result { + let scheme = scheme.map(|scheme| sys::SDL_MessageBoxColorScheme { + colors: scheme.into(), + }); + let msg_box_data = sys::SDL_MessageBoxData { + flags: flags.bits(), + window: window.map_or(ptr::null_mut(), |win| win.raw()), + title: title.as_ptr() as *const c_char, + message: message.as_ptr() as *const c_char, + numbuttons: raw_buttons.len() as c_int, + buttons: raw_buttons.as_ptr(), + colorScheme: scheme + .as_ref() + .map(|p| p as *const _) + .unwrap_or(ptr::null()), + }; + let result = + unsafe { sys::SDL_ShowMessageBox(&msg_box_data as *const _, &mut button_id as &mut _) }; + if result == 0 { match button_id { -1 => Ok(ClickedButton::CloseButton), id => { @@ -257,6 +202,6 @@ where } } } else { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } } diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index e18e9a06b6..12c442b651 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -28,7 +28,7 @@ //! None of the draw methods in `Canvas` are expected to fail. //! If they do, a panic is raised and the program is aborted. -use crate::common::{validate_int, IntegerOrSdlError}; +use crate::common::{validate_int, Error, SdlError}; use crate::get_error; use crate::pixels; use crate::pixels::PixelFormatEnum; @@ -42,7 +42,7 @@ use crate::video::{Window, WindowContext}; use libc::c_void; use libc::{c_double, c_int}; use std::convert::TryFrom; -use std::error::Error; +use std::error; use std::ffi::CStr; use std::fmt; #[cfg(not(feature = "unsafe_textures"))] @@ -57,25 +57,13 @@ use crate::sys; use crate::sys::SDL_BlendMode; use crate::sys::SDL_TextureAccess; -/// Contains the description of an error returned by SDL -#[derive(Debug, Clone)] -pub struct SdlError(String); - /// Possible errors returned by targeting a `Canvas` to render to a `Texture` -#[derive(Debug, Clone)] +#[derive(Debug)] pub enum TargetRenderError { SdlError(SdlError), NotSupported, } -impl fmt::Display for SdlError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "SDL error: {}", self.0) - } -} - -impl Error for SdlError {} - impl fmt::Display for TargetRenderError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::TargetRenderError::*; @@ -86,8 +74,8 @@ impl fmt::Display for TargetRenderError { } } -impl Error for TargetRenderError { - fn source(&self) -> Option<&(dyn Error + 'static)> { +impl error::Error for TargetRenderError { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { match self { Self::SdlError(err) => Some(err), Self::NotSupported => None, @@ -251,7 +239,7 @@ impl RendererContext { if sys::SDL_SetRenderTarget(self.raw, raw_texture) == 0 { Ok(()) } else { - Err(SdlError(get_error())) + Err(SdlError::from_last_error()) } } @@ -685,8 +673,7 @@ impl CanvasBuilder { /// Builds the renderer. #[doc(alias = "SDL_CreateRenderer")] - pub fn build(self) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn build(self) -> Result { let index = match self.index { None => -1, Some(index) => validate_int(index, "index")?, @@ -694,7 +681,7 @@ impl CanvasBuilder { let raw = unsafe { sys::SDL_CreateRenderer(self.window.raw(), index, self.renderer_flags) }; if raw.is_null() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { let context = Rc::new(unsafe { RendererContext::from_ll(raw, self.window.context()) }); let default_pixel_format = self.window.window_pixel_format(); @@ -771,7 +758,7 @@ impl fmt::Display for TextureValueError { } } -impl Error for TextureValueError {} +impl error::Error for TextureValueError {} #[doc(alias = "SDL_CreateTexture")] fn ll_create_texture( @@ -1055,14 +1042,14 @@ impl Canvas { /// Sets a device independent resolution for rendering. #[doc(alias = "SDL_RenderSetLogicalSize")] - pub fn set_logical_size(&mut self, width: u32, height: u32) -> Result<(), IntegerOrSdlError> { - use crate::common::IntegerOrSdlError::*; + pub fn set_logical_size(&mut self, width: u32, height: u32) -> Result<(), Error> { let width = validate_int(width, "width")?; let height = validate_int(height, "height")?; let result = unsafe { sys::SDL_RenderSetLogicalSize(self.context.raw, width, height) }; - match result { - 0 => Ok(()), - _ => Err(SdlError(get_error())), + if result == 0 { + Ok(()) + } else { + Err(Error::from_sdl_error()) } } @@ -1985,7 +1972,7 @@ impl fmt::Display for UpdateTextureError { } } -impl Error for UpdateTextureError {} +impl error::Error for UpdateTextureError {} #[derive(Debug, Clone)] pub enum UpdateTextureYUVError { @@ -2045,7 +2032,7 @@ impl fmt::Display for UpdateTextureYUVError { } } -impl Error for UpdateTextureYUVError {} +impl error::Error for UpdateTextureYUVError {} struct InternalTexture { raw: *mut sys::SDL_Texture, diff --git a/src/sdl2/sdl.rs b/src/sdl2/sdl.rs index fdef86c5e7..6c28f0fe0a 100644 --- a/src/sdl2/sdl.rs +++ b/src/sdl2/sdl.rs @@ -1,39 +1,10 @@ use libc::c_char; use std::cell::Cell; -use std::error; use std::ffi::{CStr, CString, NulError}; -use std::fmt; use std::marker::PhantomData; -use std::mem::transmute; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; -use crate::sys; - -#[repr(i32)] -#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] -pub enum Error { - NoMemError = sys::SDL_errorcode::SDL_ENOMEM as i32, - ReadError = sys::SDL_errorcode::SDL_EFREAD as i32, - WriteError = sys::SDL_errorcode::SDL_EFWRITE as i32, - SeekError = sys::SDL_errorcode::SDL_EFSEEK as i32, - UnsupportedError = sys::SDL_errorcode::SDL_UNSUPPORTED as i32, -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Error::*; - - match *self { - NoMemError => write!(f, "Out of memory"), - ReadError => write!(f, "Error reading from datastream"), - WriteError => write!(f, "Error writing to datastream"), - SeekError => write!(f, "Error seeking in datastream"), - UnsupportedError => write!(f, "Unknown SDL error"), - } - } -} - -impl error::Error for Error {} +use crate::{sys, Error}; /// True if the main thread has been declared. The main thread is declared when /// SDL is first initialized. @@ -383,21 +354,14 @@ pub fn get_error() -> String { } #[doc(alias = "SDL_SetError")] -pub fn set_error(err: &str) -> Result<(), NulError> { - let c_string = CString::new(err)?; +pub fn set_error(err: &str) -> Result<(), Error> { + let err = as_cstring!(err)?; unsafe { - sys::SDL_SetError(b"%s\0".as_ptr() as *const c_char, c_string.as_ptr()); + sys::SDL_SetError(b"%s\0".as_ptr() as *const c_char, err.as_ptr()); } Ok(()) } -#[doc(alias = "SDL_Error")] -pub fn set_error_from_code(err: Error) { - unsafe { - sys::SDL_Error(transmute::(err)); - } -} - #[doc(alias = "SDL_ClearError")] pub fn clear_error() { unsafe { diff --git a/src/sdl2/sensor.rs b/src/sdl2/sensor.rs index 8b83b1f67f..7333932438 100644 --- a/src/sdl2/sensor.rs +++ b/src/sdl2/sensor.rs @@ -18,7 +18,7 @@ /// - -z ... +z is roll from right to left use crate::sys; -use crate::common::{validate_int, IntegerOrSdlError}; +use crate::common::{validate_int, Error}; use crate::get_error; use crate::SensorSubsystem; use libc::c_char; @@ -41,14 +41,13 @@ impl SensorSubsystem { /// Attempt to open the sensor at index `sensor_index` and return it. #[doc(alias = "SDL_SensorOpen")] - pub fn open(&self, sensor_index: u32) -> Result { - use crate::common::IntegerOrSdlError::*; + pub fn open(&self, sensor_index: u32) -> Result { let sensor_index = validate_int(sensor_index, "sensor_index")?; let sensor = unsafe { sys::SDL_SensorOpen(sensor_index) }; if sensor.is_null() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(Sensor { subsystem: self.clone(), @@ -144,12 +143,12 @@ impl Sensor { /// /// Output depends on the type of the sensor. See module documentation for units and axis. #[doc(alias = "SDL_SensorGetType")] - pub fn get_data(&self) -> Result { + pub fn get_data(&self) -> Result { let mut data = [0f32; 16]; let result = unsafe { SDL_SensorGetData(self.raw, data.as_mut_ptr(), data.len() as i32) }; if result != 0 { - Err(IntegerOrSdlError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(match self.sensor_type() { SensorType::Gyroscope => SensorData::Accel([data[0], data[1], data[2]]), diff --git a/src/sdl2/ttf/font.rs b/src/sdl2/ttf/font.rs index 3a2fa2c34a..a9981e6cfc 100644 --- a/src/sdl2/ttf/font.rs +++ b/src/sdl2/ttf/font.rs @@ -1,11 +1,10 @@ // 0 should not be used in bitflags, but here it is. Removing it will break existing code. #![allow(clippy::bad_bit_mask)] -use get_error; +use crate::Error; use pixels::Color; use rwops::RWops; use std::error; -use std::ffi::NulError; use std::ffi::{CStr, CString}; use std::fmt; use std::marker::PhantomData; @@ -47,40 +46,6 @@ pub struct GlyphMetrics { pub advance: i32, } -/// The result of an `SDL2_TTF` font operation. -pub type FontResult = Result; - -/// A font-related error. -#[derive(Debug, Clone)] -pub enum FontError { - /// A Latin-1 encoded byte string is invalid. - InvalidLatin1Text(NulError), - /// A SDL2-related error occured. - SdlError(String), -} - -impl error::Error for FontError { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match *self { - FontError::InvalidLatin1Text(ref error) => Some(error), - FontError::SdlError(_) => None, - } - } -} - -impl fmt::Display for FontError { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - match *self { - FontError::InvalidLatin1Text(ref err) => { - write!(f, "Invalid Latin-1 bytes: {}", err) - } - FontError::SdlError(ref msg) => { - write!(f, "SDL2 error: {}", msg) - } - } - } -} - /// A renderable piece of text in the UTF8 or Latin-1 format. enum RenderableText<'a> { Utf8(&'a str), @@ -89,12 +54,10 @@ enum RenderableText<'a> { } impl<'a> RenderableText<'a> { /// Converts the given text to a c-style string if possible. - fn convert(&self) -> FontResult { + fn convert(&self) -> Result { match *self { RenderableText::Utf8(text) => Ok(CString::new(text).unwrap()), - RenderableText::Latin1(bytes) => { - CString::new(bytes).map_err(FontError::InvalidLatin1Text) - } + RenderableText::Latin1(text) => as_cstring!(text), RenderableText::Char(ch) => { Ok(CString::new(ch.encode_utf8(&mut [0; 4]).as_bytes()).unwrap()) } @@ -110,9 +73,9 @@ pub struct PartialRendering<'f, 'text> { } /// Converts the given raw pointer to a surface. -fn convert_to_surface<'a>(raw: *mut SDL_Surface) -> FontResult> { +fn convert_to_surface<'a>(raw: *mut SDL_Surface) -> Result, Error> { if (raw as *mut ()).is_null() { - Err(FontError::SdlError(get_error())) + Err(Error::from_sdl_error()) } else { Ok(unsafe { Surface::from_ll(raw) }) } @@ -122,7 +85,7 @@ impl<'f, 'text> PartialRendering<'f, 'text> { /// Renders the text in *solid* mode. /// See [the SDL2_TTF docs](https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC42) /// for an explanation. - pub fn solid<'b, T>(self, color: T) -> FontResult> + pub fn solid<'b, T>(self, color: T) -> Result, Error> where T: Into, { @@ -144,7 +107,7 @@ impl<'f, 'text> PartialRendering<'f, 'text> { /// Renders the text in *shaded* mode. /// See [the SDL2_TTF docs](https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC42) /// for an explanation. - pub fn shaded<'b, T>(self, color: T, background: T) -> FontResult> + pub fn shaded<'b, T>(self, color: T, background: T) -> Result, Error> where T: Into, { @@ -173,7 +136,7 @@ impl<'f, 'text> PartialRendering<'f, 'text> { /// Renders the text in *blended* mode. /// See [the SDL2_TTF docs](https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC42) /// for an explanation. - pub fn blended<'b, T>(self, color: T) -> FontResult> + pub fn blended<'b, T>(self, color: T) -> Result, Error> where T: Into, { @@ -196,7 +159,7 @@ impl<'f, 'text> PartialRendering<'f, 'text> { /// exceeds the given maximum width. /// See [the SDL2_TTF docs](https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC42) /// for an explanation of the mode. - pub fn blended_wrapped<'b, T>(self, color: T, wrap_max_width: u32) -> FontResult> + pub fn blended_wrapped<'b, T>(self, color: T, wrap_max_width: u32) -> Result, Error> where T: Into, { @@ -338,7 +301,7 @@ impl<'ttf, 'r> Font<'ttf, 'r> { /// Returns the width and height of the given text when rendered using this /// font. - pub fn size_of(&self, text: &str) -> FontResult<(u32, u32)> { + pub fn size_of(&self, text: &str) -> Result<(u32, u32), Error> { let c_string = RenderableText::Utf8(text).convert()?; let (res, size) = unsafe { let mut w = 0; // mutated by C code @@ -349,13 +312,13 @@ impl<'ttf, 'r> Font<'ttf, 'r> { if res == 0 { Ok(size) } else { - Err(FontError::SdlError(get_error())) + Err(Error::from_sdl_error()) } } /// Returns the width and height of the given text when rendered using this /// font. - pub fn size_of_latin1(&self, text: &[u8]) -> FontResult<(u32, u32)> { + pub fn size_of_latin1(&self, text: &[u8]) -> Result<(u32, u32), Error> { let c_string = RenderableText::Latin1(text).convert()?; let (res, size) = unsafe { let mut w: i32 = 0; @@ -366,13 +329,13 @@ impl<'ttf, 'r> Font<'ttf, 'r> { if res == 0 { Ok(size) } else { - Err(FontError::SdlError(get_error())) + Err(Error::from_sdl_error()) } } /// Returns the width and height of the given text when rendered using this /// font. - pub fn size_of_char(&self, ch: char) -> FontResult<(u32, u32)> { + pub fn size_of_char(&self, ch: char) -> Result<(u32, u32), Error> { self.size_of(ch.encode_utf8(&mut [0; 4])) } diff --git a/src/sdl2/url.rs b/src/sdl2/url.rs index 8a662937e7..178418a29b 100644 --- a/src/sdl2/url.rs +++ b/src/sdl2/url.rs @@ -1,39 +1,9 @@ //! Opening URLs in default system handlers -use std::error; -use std::ffi::{CString, NulError}; -use std::fmt; - -use crate::get_error; +use crate::Error; use crate::sys; -#[derive(Debug, Clone)] -pub enum OpenUrlError { - InvalidUrl(NulError), - SdlError(String), -} - -impl fmt::Display for OpenUrlError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::OpenUrlError::*; - - match *self { - InvalidUrl(ref e) => write!(f, "Invalid URL: {}", e), - SdlError(ref e) => write!(f, "SDL error: {}", e), - } - } -} - -impl error::Error for OpenUrlError { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match self { - Self::InvalidUrl(err) => Some(err), - Self::SdlError(_) => None, - } - } -} - /// Opens a URL/URI in the default system-provided application. /// /// This will most likely open a web browser for http:// and https:// links, @@ -53,19 +23,12 @@ impl error::Error for OpenUrlError { /// .expect("Opening URLs not supported on this platform"); /// ``` #[doc(alias = "SDL_OpenURL")] -pub fn open_url(url: &str) -> Result<(), OpenUrlError> { - use self::OpenUrlError::*; - let result = unsafe { - let url = match CString::new(url) { - Ok(s) => s, - Err(err) => return Err(InvalidUrl(err)), - }; - sys::SDL_OpenURL(url.as_ptr()) - } == 0; +pub fn open_url(url: &str) -> Result<(), Error> { + let result = unsafe { sys::SDL_OpenURL(as_cstring!(url)?.as_ptr()) }; - if result { + if result == 0 { Ok(()) } else { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } } diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index c758a61425..d40700cc24 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -1,12 +1,11 @@ use libc::{c_char, c_float, c_int, c_uint}; use std::convert::TryFrom; -use std::error::Error; -use std::ffi::{CStr, CString, NulError}; +use std::ffi::{CStr, CString}; use std::ops::{Deref, DerefMut}; use std::rc::Rc; -use std::{fmt, mem, ptr}; +use std::{mem, ptr}; -use crate::common::{validate_int, IntegerOrSdlError}; +use crate::common::{validate_int, validate_string, Error}; use crate::pixels::PixelFormatEnum; use crate::rect::Rect; use crate::render::CanvasBuilder; @@ -1071,36 +1070,6 @@ impl VideoSubsystem { } } -#[derive(Debug, Clone)] -pub enum WindowBuildError { - HeightOverflows(u32), - WidthOverflows(u32), - InvalidTitle(NulError), - SdlError(String), -} - -impl fmt::Display for WindowBuildError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::WindowBuildError::*; - - match *self { - HeightOverflows(h) => write!(f, "Window height ({}) is too high.", h), - WidthOverflows(w) => write!(f, "Window width ({}) is too high.", w), - InvalidTitle(ref e) => write!(f, "Invalid window title: {}", e), - SdlError(ref e) => write!(f, "SDL error: {}", e), - } - } -} - -impl Error for WindowBuildError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::InvalidTitle(err) => Some(err), - Self::HeightOverflows(_) | Self::WidthOverflows(_) | Self::SdlError(_) => None, - } - } -} - /// The type that allows you to build windows. #[derive(Debug)] pub struct WindowBuilder { @@ -1135,21 +1104,11 @@ impl WindowBuilder { /// Builds the window. #[doc(alias = "SDL_CreateWindow")] - pub fn build(&self) -> Result { - use self::WindowBuildError::*; - let title = match CString::new(self.title.as_bytes()) { - Ok(t) => t, - Err(err) => return Err(InvalidTitle(err)), - }; - if self.width >= (1 << 31) { - return Err(WidthOverflows(self.width)); - } - if self.height >= (1 << 31) { - return Err(HeightOverflows(self.width)); - } + pub fn build(&self) -> Result { + let title = validate_string(self.title.as_bytes(), "title")?; + let raw_width = validate_int(self.width, "width")?; + let raw_height = validate_int(self.height, "height")?; - let raw_width = self.width as c_int; - let raw_height = self.height as c_int; unsafe { let raw = if self.shaped { sys::SDL_CreateShapedWindow( @@ -1172,7 +1131,7 @@ impl WindowBuilder { }; if raw.is_null() { - Err(SdlError(get_error())) + Err(Error::from_sdl_error()) } else { let metal_view = match self.create_metal_view { #[cfg(target_os = "macos")] @@ -1545,8 +1504,8 @@ impl Window { } #[doc(alias = "SDL_SetWindowTitle")] - pub fn set_title(&mut self, title: &str) -> Result<(), NulError> { - let title = CString::new(title)?; + pub fn set_title(&mut self, title: &str) -> Result<(), Error> { + let title = as_cstring!(title)?; unsafe { sys::SDL_SetWindowTitle(self.context.raw, title.as_ptr() as *const c_char); } @@ -1661,7 +1620,7 @@ impl Window { } #[doc(alias = "SDL_SetWindowSize")] - pub fn set_size(&mut self, width: u32, height: u32) -> Result<(), IntegerOrSdlError> { + pub fn set_size(&mut self, width: u32, height: u32) -> Result<(), Error> { let w = validate_int(width, "width")?; let h = validate_int(height, "height")?; unsafe { @@ -1695,7 +1654,7 @@ impl Window { } #[doc(alias = "SDL_SetWindowMinimumSize")] - pub fn set_minimum_size(&mut self, width: u32, height: u32) -> Result<(), IntegerOrSdlError> { + pub fn set_minimum_size(&mut self, width: u32, height: u32) -> Result<(), Error> { let w = validate_int(width, "width")?; let h = validate_int(height, "height")?; unsafe { @@ -1713,7 +1672,7 @@ impl Window { } #[doc(alias = "SDL_SetWindowMaximumSize")] - pub fn set_maximum_size(&mut self, width: u32, height: u32) -> Result<(), IntegerOrSdlError> { + pub fn set_maximum_size(&mut self, width: u32, height: u32) -> Result<(), Error> { let w = validate_int(width, "width")?; let h = validate_int(height, "height")?; unsafe {