Skip to content

Commit

Permalink
fix clippy warnings and other improvements to code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed Jun 29, 2024
1 parent a57f9ad commit d9ab3e0
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 123 deletions.
6 changes: 3 additions & 3 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion examples/audio-capture-and-replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion examples/audio-wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn main() -> Result<(), String> {

// initialize the audio callback
Sound {
data: data,
data,
volume: 0.25,
pos: 0,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/audio-whitenoise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
23 changes: 14 additions & 9 deletions examples/game-of-life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod game_of_life {
}

GameOfLife {
playground: playground,
playground,
state: State::Paused,
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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),
];
Expand Down
6 changes: 3 additions & 3 deletions examples/haptic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 5 additions & 5 deletions examples/mixer-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/resource-manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
pub fn new(loader: &'l L) -> Self {
ResourceManager {
cache: HashMap::new(),
loader: loader,
loader,
}
}

Expand Down
21 changes: 8 additions & 13 deletions src/sdl2/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*!
Event Handling
*/
//! Event Handling

use std::borrow::ToOwned;
use std::collections::HashMap;
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/sdl2/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/sdl2/keyboard/keycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Keycode> {
if n != 0 {
return Some(Keycode(n));
Some(Keycode(n))
} else {
None
}
return None;
}
}

Expand All @@ -468,9 +469,9 @@ impl Deref for Keycode {
}
}

impl Into<i32> for Keycode {
fn into(self) -> i32 {
self.into_i32()
impl From<Keycode> for i32 {
fn from(val: Keycode) -> Self {
val.into_i32()
}
}

Expand All @@ -485,7 +486,7 @@ impl Keycode {
match sys::SDL_GetKeyFromScancode(transmute::<u32, sys::SDL_Scancode>(scancode as u32))
{
UNKNOWN => None,
keycode_id => Keycode::from_i32(keycode_id as i32),
keycode_id => Keycode::from_i32(keycode_id),
}
}
}
Expand All @@ -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,
Expand Down
40 changes: 10 additions & 30 deletions src/sdl2/mixer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
<Self as fmt::Debug>::fmt(self, f)
}
}

Expand Down Expand Up @@ -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<T: AudioFormatNum>(buffer: Box<[T]>) -> Result<Chunk, String> {
use std::mem::size_of;
let len: u32 = (buffer.len() * size_of::<T>()).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)
}
Expand Down
Loading

0 comments on commit d9ab3e0

Please sign in to comment.