sdl3/
common.rs

1use crate::Error;
2use std::error;
3use std::fmt;
4
5/// A given integer was so big that its representation as a C integer would be
6/// negative.
7#[derive(Debug, Clone, PartialEq)]
8pub enum IntegerOrSdlError {
9    IntegerOverflows(&'static str, u32),
10    SdlError(Error),
11}
12/// Validates and converts the given u32 to a positive C integer.
13pub fn validate_int(value: u32, name: &'static str) -> Result<::libc::c_int, IntegerOrSdlError> {
14    use self::IntegerOrSdlError::*;
15    // Many SDL functions will accept `int` values, even if it doesn't make sense
16    // for the values to be negative.
17    // In the cases that SDL doesn't check negativity, passing negative values
18    // could be unsafe.
19    // For example, `SDL_GetJoystickButton` uses the index argument to access an
20    // array without checking if it's negative, which could potentially lead to
21    // segmentation faults.
22    if value >= 1 << 31 {
23        Err(IntegerOverflows(name, value))
24    } else {
25        Ok(value as ::libc::c_int)
26    }
27}
28
29impl fmt::Display for IntegerOrSdlError {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        use self::IntegerOrSdlError::*;
32
33        match *self {
34            IntegerOverflows(name, value) => write!(f, "Integer '{name}' overflows ({value})"),
35            SdlError(ref e) => write!(f, "SDL error: {e}"),
36        }
37    }
38}
39
40impl error::Error for IntegerOrSdlError {
41    fn description(&self) -> &str {
42        use self::IntegerOrSdlError::*;
43
44        match *self {
45            IntegerOverflows(_, _) => "integer overflow",
46            SdlError(ref e) => &e.0,
47        }
48    }
49}