diff --git a/changelog.md b/changelog.md index 76f74c4cfe9..12808ac830d 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another. ### Next +[PR #1416](https://github.com/Rust-SDL2/rust-sdl2/pull/1416) Apply clippy fixes, fix deprecations and other code quality improvements. + [PR #1408](https://github.com/Rust-SDL2/rust-sdl2/pull/1408) Allow comparing `Version`s, add constant with the version the bindings were compiled with. [PR #1407](https://github.com/Rust-SDL2/rust-sdl2/pull/1407) Add new use_ios_framework for linking to SDL2.framework on iOS diff --git a/examples/animation.rs b/examples/animation.rs index 44024156acf..311212b6bfe 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -74,13 +74,13 @@ fn main() -> Result<(), String> { // set the current frame for time source_rect_0.set_x(32 * ((ticks / 100) % frames_per_anim)); - dest_rect_0.set_x(1 * ((ticks / 14) % 768) - 128); + dest_rect_0.set_x(((ticks / 14) % 768) - 128); source_rect_1.set_x(32 * ((ticks / 100) % frames_per_anim)); - dest_rect_1.set_x((1 * ((ticks / 12) % 768) - 672) * -1); + dest_rect_1.set_x(-(((ticks / 12) % 768) - 672)); source_rect_2.set_x(32 * ((ticks / 100) % frames_per_anim)); - dest_rect_2.set_x(1 * ((ticks / 10) % 768) - 128); + dest_rect_2.set_x(((ticks / 10) % 768) - 128); canvas.clear(); // copy the frame to the canvas diff --git a/examples/audio-capture-and-replay.rs b/examples/audio-capture-and-replay.rs index d4cb2db1cda..9a899e95be4 100644 --- a/examples/audio-capture-and-replay.rs +++ b/examples/audio-capture-and-replay.rs @@ -2,7 +2,6 @@ extern crate sdl2; use sdl2::audio::{AudioCallback, AudioSpecDesired}; use sdl2::AudioSubsystem; -use std::i16; use std::sync::mpsc; use std::time::Duration; diff --git a/examples/audio-wav.rs b/examples/audio-wav.rs index 04186cf3920..aa711f8c9ec 100644 --- a/examples/audio-wav.rs +++ b/examples/audio-wav.rs @@ -69,7 +69,7 @@ fn main() -> Result<(), String> { // initialize the audio callback Sound { - data: data, + data, volume: 0.25, pos: 0, } diff --git a/examples/audio-whitenoise.rs b/examples/audio-whitenoise.rs index d1c3902057b..96d3005dfcb 100644 --- a/examples/audio-whitenoise.rs +++ b/examples/audio-whitenoise.rs @@ -48,7 +48,7 @@ fn main() -> Result<(), String> { { // Acquire a lock. This lets us read and modify callback data. let mut lock = device.lock(); - (*lock).volume = 0.25; + lock.volume = 0.25; // Lock guard is dropped here } diff --git a/examples/game-of-life.rs b/examples/game-of-life.rs index 22cc873eaab..ed28cc35d15 100644 --- a/examples/game-of-life.rs +++ b/examples/game-of-life.rs @@ -40,7 +40,7 @@ mod game_of_life { } GameOfLife { - playground: playground, + playground, state: State::Paused, } } @@ -90,13 +90,18 @@ mod game_of_life { } } } - if count > 3 || count < 2 { - *square = false; - } else if count == 3 { - *square = true; - } else if count == 2 { - *square = *square; - } + match count { + ..=1 => { + *square = false; + } + 2 => {} + 3 => { + *square = true; + } + 4.. => { + *square = false; + } + }; } self.playground = new_playground; } @@ -127,7 +132,7 @@ fn dummy_texture<'a>( .map_err(|e| e.to_string())?; // let's change the textures we just created { - let textures = vec![ + let textures = [ (&mut square_texture1, TextureColor::Yellow), (&mut square_texture2, TextureColor::White), ]; diff --git a/examples/haptic.rs b/examples/haptic.rs index 86f1eb370ae..6434064d6fa 100644 --- a/examples/haptic.rs +++ b/examples/haptic.rs @@ -13,14 +13,14 @@ fn main() -> Result<(), String> { // Iterate over all available joysticks and stop once we manage to open one. let joystick_index = (0..available) - .find_map(|id| match joystick_subsystem.open(id) { + .find(|&id| match joystick_subsystem.open(id) { Ok(c) => { println!("Success: opened \"{}\"", c.name()); - Some(id) + true } Err(e) => { println!("failed: {:?}", e); - None + false } }) .expect("Couldn't open any joystick"); diff --git a/examples/mixer-demo.rs b/examples/mixer-demo.rs index 404b0a250ce..52c3fc63e95 100644 --- a/examples/mixer-demo.rs +++ b/examples/mixer-demo.rs @@ -11,7 +11,7 @@ fn main() -> Result<(), String> { if args.len() < 2 { println!("Usage: ./demo music.[mp3|wav|ogg] [sound-effect.[mp3|wav|ogg]]") } else { - let sound_file = args.get(2).map(|sound_file| Path::new(sound_file)); + let sound_file = args.get(2).map(Path::new); demo(Path::new(&args[1]), sound_file)?; } @@ -23,7 +23,7 @@ fn demo(music_file: &Path, sound_file: Option<&Path>) -> Result<(), String> { let sdl = sdl2::init()?; let _audio = sdl.audio()?; - let mut timer = sdl.timer()?; + let timer = sdl.timer()?; let frequency = 44_100; let format = AUDIO_S16LSB; // signed 16 bit samples, in little-endian byte order @@ -77,9 +77,9 @@ fn demo(music_file: &Path, sound_file: Option<&Path>) -> Result<(), String> { // (played at half the volume to save people's ears). let buffer = (0..frequency) .map(|i| { - (0.1 * i16::max_value() as f32 - * (2.0 * 3.14 * 500.0 * (i as f32 / frequency as f32)).sin()) - as i16 + (0.1 * i16::MAX as f32 + * (2.0 * std::f32::consts::PI * 500.0 * (i as f32 / frequency as f32)) + .sin()) as i16 }) .collect(); sdl2::mixer::Chunk::from_raw_buffer(buffer) diff --git a/examples/resource-manager.rs b/examples/resource-manager.rs index b703bec0853..73b0aca6f86 100644 --- a/examples/resource-manager.rs +++ b/examples/resource-manager.rs @@ -101,7 +101,7 @@ where pub fn new(loader: &'l L) -> Self { ResourceManager { cache: HashMap::new(), - loader: loader, + loader, } } diff --git a/src/sdl2/event.rs b/src/sdl2/event.rs index 6849d98883e..820a086b35e 100644 --- a/src/sdl2/event.rs +++ b/src/sdl2/event.rs @@ -1,6 +1,4 @@ -/*! -Event Handling - */ +//! Event Handling use std::borrow::ToOwned; use std::collections::HashMap; @@ -2359,25 +2357,22 @@ impl Event { // FIXME: Use a constant from sdl2-sys when bindgen will be fixed (see https://github.com/Rust-SDL2/rust-sdl2/issues/1265) const SDL_TOUCH_MOUSEID: u32 = 0xFFFFFFFF; - match self { + matches!( + self, Self::MouseMotion { which: SDL_TOUCH_MOUSEID, .. - } - | Self::MouseButtonDown { + } | Self::MouseButtonDown { which: SDL_TOUCH_MOUSEID, .. - } - | Self::MouseButtonUp { + } | Self::MouseButtonUp { which: SDL_TOUCH_MOUSEID, .. - } - | Self::MouseWheel { + } | Self::MouseWheel { which: SDL_TOUCH_MOUSEID, .. - } => true, - _ => false, - } + } + ) } /// Returns `true` if this is a controller event. diff --git a/src/sdl2/image/mod.rs b/src/sdl2/image/mod.rs index b722dddcaa4..07cdb1cb8cb 100644 --- a/src/sdl2/image/mod.rs +++ b/src/sdl2/image/mod.rs @@ -35,10 +35,10 @@ bitflags! { /// InitFlags are passed to init() to control which subsystem /// functionality to load. pub struct InitFlag : u32 { - const JPG = image::IMG_InitFlags_IMG_INIT_JPG as u32; - const PNG = image::IMG_InitFlags_IMG_INIT_PNG as u32; - const TIF = image::IMG_InitFlags_IMG_INIT_TIF as u32; - const WEBP = image::IMG_InitFlags_IMG_INIT_WEBP as u32; + const JPG = image::IMG_InitFlags_IMG_INIT_JPG; + const PNG = image::IMG_InitFlags_IMG_INIT_PNG; + const TIF = image::IMG_InitFlags_IMG_INIT_TIF; + const WEBP = image::IMG_InitFlags_IMG_INIT_WEBP; } } diff --git a/src/sdl2/keyboard/keycode.rs b/src/sdl2/keyboard/keycode.rs index 324fda01394..1d85a2262a9 100644 --- a/src/sdl2/keyboard/keycode.rs +++ b/src/sdl2/keyboard/keycode.rs @@ -439,14 +439,15 @@ impl Keycode { impl Keycode { pub fn into_i32(&self) -> i32 { - return self.0; + self.0 } pub fn from_i32(n: i32) -> Option { if n != 0 { - return Some(Keycode(n)); + Some(Keycode(n)) + } else { + None } - return None; } } @@ -468,9 +469,9 @@ impl Deref for Keycode { } } -impl Into for Keycode { - fn into(self) -> i32 { - self.into_i32() +impl From for i32 { + fn from(val: Keycode) -> Self { + val.into_i32() } } @@ -485,7 +486,7 @@ impl Keycode { match sys::SDL_GetKeyFromScancode(transmute::(scancode as u32)) { UNKNOWN => None, - keycode_id => Keycode::from_i32(keycode_id as i32), + keycode_id => Keycode::from_i32(keycode_id), } } } @@ -497,7 +498,7 @@ impl Keycode { match CString::new(name) { Ok(name) => match sys::SDL_GetKeyFromName(name.as_ptr() as *const c_char) { UNKNOWN => None, - keycode_id => Some(Keycode::from_i32(keycode_id as i32).unwrap()), + keycode_id => Some(Keycode::from_i32(keycode_id).unwrap()), }, // string contains a nul byte - it won't match anything. Err(_) => None, diff --git a/src/sdl2/mixer/mod.rs b/src/sdl2/mixer/mod.rs index 7a74f76dad7..8f87ad0426c 100644 --- a/src/sdl2/mixer/mod.rs +++ b/src/sdl2/mixer/mod.rs @@ -97,37 +97,18 @@ pub fn get_linked_version() -> Version { bitflags!( pub struct InitFlag : u32 { - const FLAC = mixer::MIX_InitFlags_MIX_INIT_FLAC as u32; - const MOD = mixer::MIX_InitFlags_MIX_INIT_MOD as u32; - const MP3 = mixer::MIX_InitFlags_MIX_INIT_MP3 as u32; - const OGG = mixer::MIX_InitFlags_MIX_INIT_OGG as u32; - const MID = mixer::MIX_InitFlags_MIX_INIT_MID as u32; - const OPUS = mixer::MIX_InitFlags_MIX_INIT_OPUS as u32; + const FLAC = mixer::MIX_InitFlags_MIX_INIT_FLAC; + const MOD = mixer::MIX_InitFlags_MIX_INIT_MOD; + const MP3 = mixer::MIX_InitFlags_MIX_INIT_MP3; + const OGG = mixer::MIX_InitFlags_MIX_INIT_OGG; + const MID = mixer::MIX_InitFlags_MIX_INIT_MID; + const OPUS = mixer::MIX_InitFlags_MIX_INIT_OPUS; } ); -impl ToString for InitFlag { - fn to_string(&self) -> String { - let mut string = "".to_string(); - if self.contains(InitFlag::FLAC) { - string = string + &"INIT_FLAC ".to_string(); - } - if self.contains(InitFlag::MOD) { - string = string + &"INIT_MOD ".to_string(); - } - if self.contains(InitFlag::MP3) { - string = string + &"INIT_MP3 ".to_string(); - } - if self.contains(InitFlag::OGG) { - string = string + &"INIT_OGG ".to_string(); - } - if self.contains(InitFlag::MID) { - string = string + &"INIT_MID ".to_string(); - } - if self.contains(InitFlag::OPUS) { - string = string + &"INIT_OPUS ".to_string(); - } - string +impl fmt::Display for InitFlag { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + ::fmt(self, f) } } @@ -264,8 +245,7 @@ impl Chunk { /// It's your responsibility to provide the audio data in the right format, as no conversion /// will take place when using this method. pub fn from_raw_buffer(buffer: Box<[T]>) -> Result { - use std::mem::size_of; - let len: u32 = (buffer.len() * size_of::()).try_into().unwrap(); + let len: u32 = std::mem::size_of_val(&*buffer).try_into().unwrap(); let raw = unsafe { mixer::Mix_QuickLoad_RAW(Box::into_raw(buffer) as *mut u8, len) }; Self::from_owned_raw(raw) } diff --git a/src/sdl2/rect.rs b/src/sdl2/rect.rs index a938e2955e4..6f1c0817aa7 100644 --- a/src/sdl2/rect.rs +++ b/src/sdl2/rect.rs @@ -991,11 +991,12 @@ pub struct FRect { impl ::std::fmt::Debug for FRect { fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { - return write!( - fmt, - "FRect {{ x: {}, y: {}, w: {}, h: {} }}", - self.raw.x, self.raw.y, self.raw.w, self.raw.h - ); + fmt.debug_struct("FRect") + .field("x", &self.raw.x) + .field("y", &self.raw.y) + .field("w", &self.raw.w) + .field("h", &self.raw.h) + .finish() } } @@ -1056,12 +1057,12 @@ impl FRect { /// The width of this rectangle. pub fn width(&self) -> f32 { - self.raw.w as f32 + self.raw.w } /// The height of this rectangle. pub fn height(&self) -> f32 { - self.raw.h as f32 + self.raw.h } /// Returns the width and height of this rectangle. @@ -1372,10 +1373,7 @@ impl FRect { /// If a clipping rectangle is given, only points that are within it will be /// considered. #[doc(alias = "SDL_EncloseFPoints")] - pub fn from_enclose_points>>( - points: &[FPoint], - clipping_rect: R, - ) -> Option + pub fn from_enclose_points(points: &[FPoint], clipping_rect: R) -> Option where R: Into>, { @@ -1548,15 +1546,15 @@ impl DerefMut for FRect { } } -impl Into for FRect { - fn into(self) -> sys::SDL_FRect { - self.raw +impl From for sys::SDL_FRect { + fn from(val: FRect) -> Self { + val.raw } } -impl Into<(f32, f32, f32, f32)> for FRect { - fn into(self) -> (f32, f32, f32, f32) { - (self.raw.x, self.raw.y, self.raw.w, self.raw.h) +impl From for (f32, f32, f32, f32) { + fn from(val: FRect) -> Self { + (val.raw.x, val.raw.y, val.raw.w, val.raw.h) } } @@ -1609,7 +1607,10 @@ pub struct FPoint { impl ::std::fmt::Debug for FPoint { fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { - return write!(fmt, "FPoint {{ x: {}, y: {} }}", self.raw.x, self.raw.y); + fmt.debug_struct("FPoint") + .field("x", &self.raw.x) + .field("y", &self.raw.y) + .finish() } } @@ -1672,15 +1673,15 @@ impl From<(f32, f32)> for FPoint { } } -impl Into for FPoint { - fn into(self) -> sys::SDL_FPoint { - self.raw +impl From for sys::SDL_FPoint { + fn from(val: FPoint) -> Self { + val.raw } } -impl Into<(f32, f32)> for FPoint { - fn into(self) -> (f32, f32) { - (self.x(), self.y()) +impl From for (f32, f32) { + fn from(val: FPoint) -> Self { + (val.x(), val.y()) } } diff --git a/src/sdl2/rwops.rs b/src/sdl2/rwops.rs index 717df5cf4ef..3d6d9143564 100644 --- a/src/sdl2/rwops.rs +++ b/src/sdl2/rwops.rs @@ -147,7 +147,7 @@ impl<'a> io::Read for RWops<'a> { out_len as libc::size_t, ) }; - Ok(ret as usize) + Ok(ret) } } @@ -162,7 +162,7 @@ impl<'a> io::Write for RWops<'a> { in_len as libc::size_t, ) }; - Ok(ret as usize) + Ok(ret) } fn flush(&mut self) -> io::Result<()> { diff --git a/src/sdl2/sdl.rs b/src/sdl2/sdl.rs index 663c2a7c4ab..fdef86c5e78 100644 --- a/src/sdl2/sdl.rs +++ b/src/sdl2/sdl.rs @@ -378,7 +378,7 @@ pub fn init() -> Result { pub fn get_error() -> String { unsafe { let err = sys::SDL_GetError(); - CStr::from_ptr(err as *const _).to_str().unwrap().to_owned() + CStr::from_ptr(err).to_str().unwrap().to_owned() } } @@ -386,10 +386,7 @@ pub fn get_error() -> String { pub fn set_error(err: &str) -> Result<(), NulError> { let c_string = CString::new(err)?; unsafe { - sys::SDL_SetError( - b"%s\0".as_ptr() as *const c_char, - c_string.as_ptr() as *const c_char, - ); + sys::SDL_SetError(b"%s\0".as_ptr() as *const c_char, c_string.as_ptr()); } Ok(()) } @@ -397,7 +394,7 @@ pub fn set_error(err: &str) -> Result<(), NulError> { #[doc(alias = "SDL_Error")] pub fn set_error_from_code(err: Error) { unsafe { - sys::SDL_Error(transmute(err)); + sys::SDL_Error(transmute::(err)); } } diff --git a/src/sdl2/sensor.rs b/src/sdl2/sensor.rs index 4201592476b..8b83b1f67f6 100644 --- a/src/sdl2/sensor.rs +++ b/src/sdl2/sensor.rs @@ -82,9 +82,9 @@ impl SensorType { } } -impl Into for SensorType { - fn into(self) -> SDL_SensorType { - match self { +impl From for SDL_SensorType { + fn from(val: SensorType) -> Self { + match val { SensorType::Unknown => SDL_SensorType::SDL_SENSOR_UNKNOWN, SensorType::Gyroscope => SDL_SensorType::SDL_SENSOR_GYRO, SensorType::Accelerometer => SDL_SensorType::SDL_SENSOR_ACCEL, @@ -119,7 +119,7 @@ impl Sensor { if result < 0 { // Should only fail if the joystick is NULL. - panic!("{}", get_error()) + panic!("{}", get_error()); } else { result as u32 } @@ -180,11 +180,6 @@ fn c_str_to_string(c_str: *const c_char) -> String { if c_str.is_null() { String::new() } else { - unsafe { - CStr::from_ptr(c_str as *const _) - .to_str() - .unwrap() - .to_owned() - } + unsafe { CStr::from_ptr(c_str).to_str().unwrap().to_owned() } } } diff --git a/src/sdl2/surface.rs b/src/sdl2/surface.rs index efe167ee12e..1785a4f731d 100644 --- a/src/sdl2/surface.rs +++ b/src/sdl2/surface.rs @@ -196,7 +196,7 @@ impl<'a> Surface<'a> { Err("Pitch is too large.".to_owned()) } else { let raw = sys::SDL_CreateRGBSurfaceFrom( - data.as_mut_ptr() as *mut _, + data.as_mut_ptr() as *mut libc::c_void, width as c_int, height as c_int, masks.bpp as c_int, @@ -378,7 +378,7 @@ impl SurfaceRef { panic!("could not lock surface"); } - let raw_pixels = self.raw_ref().pixels as *const _; + let raw_pixels = self.raw_ref().pixels as *const u8; let len = self.raw_ref().pitch as usize * (self.raw_ref().h as usize); let pixels = ::std::slice::from_raw_parts(raw_pixels, len); let rv = f(pixels); @@ -395,7 +395,7 @@ impl SurfaceRef { panic!("could not lock surface"); } - let raw_pixels = self.raw_ref().pixels as *mut _; + let raw_pixels = self.raw_ref().pixels as *mut u8; let len = self.raw_ref().pitch as usize * (self.raw_ref().h as usize); let pixels = ::std::slice::from_raw_parts_mut(raw_pixels, len); let rv = f(pixels); @@ -411,7 +411,7 @@ impl SurfaceRef { None } else { unsafe { - let raw_pixels = self.raw_ref().pixels as *const _; + let raw_pixels = self.raw_ref().pixels as *const u8; let len = self.raw_ref().pitch as usize * (self.raw_ref().h as usize); Some(::std::slice::from_raw_parts(raw_pixels, len)) @@ -426,7 +426,7 @@ impl SurfaceRef { None } else { unsafe { - let raw_pixels = self.raw_ref().pixels as *mut _; + let raw_pixels = self.raw_ref().pixels as *mut u8; let len = self.raw_ref().pitch as usize * (self.raw_ref().h as usize); Some(::std::slice::from_raw_parts_mut(raw_pixels, len))