From afbe3841a45da1934ffa65e55329c7a1f5c68a9a Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:01:51 -0500 Subject: [PATCH 01/12] Fix clippy redunant field names warning See https://rust-lang.github.io/rust-clippy/master/#redundant_field_names --- src/builder.rs | 12 ++++++------ src/meta.rs | 12 ++++++------ src/reader.rs | 14 ++++++++------ src/writer.rs | 4 ++-- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 3a46cd2..2b8eb13 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -15,13 +15,13 @@ pub struct AbsoluteEvent { impl AbsoluteEvent { pub fn new_midi(time: u64, midi: MidiMessage) -> AbsoluteEvent { AbsoluteEvent { - time: time, + time, event: Event::Midi(midi), } } pub fn new_meta(time: u64, meta: MetaEvent) -> AbsoluteEvent { AbsoluteEvent { - time: time, + time, event: Event::Meta(meta), } } @@ -154,7 +154,7 @@ impl TrackBuilder { }; prev_time = ev.time; events.push(TrackEvent { - vtime: vtime, + vtime, event: ev.event, }); } @@ -215,7 +215,7 @@ impl SMFBuilder { let vtime = bev.time - cur_time; cur_time = vtime; TrackEvent { - vtime: vtime, + vtime, event: bev.event.clone(), } }).collect(); @@ -270,7 +270,7 @@ impl SMFBuilder { match self.tracks.index_mut(track).events { EventContainer::Heap(ref mut heap) => { heap.push(AbsoluteEvent { - time: time, + time, event: Event::Midi(msg), }); } @@ -302,7 +302,7 @@ impl SMFBuilder { match self.tracks.index_mut(track).events { EventContainer::Heap(ref mut heap) => { heap.push(AbsoluteEvent { - time: time, + time, event: Event::Meta(event), }); } diff --git a/src/meta.rs b/src/meta.rs index df0422f..e406d47 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -157,16 +157,16 @@ impl MetaEvent { Some(c) => {c}, None => MetaCommand::Unknown, }; - let len = match SMFReader::read_vtime(reader) { + let length = match SMFReader::read_vtime(reader) { Ok(t) => { t } Err(_) => { return Err(MetaError::OtherErr("Couldn't read time for meta command")); } }; let mut data = Vec::new(); - read_amount(reader,&mut data,len as usize)?; + read_amount(reader,&mut data,length as usize)?; Ok(MetaEvent{ - command: command, - length: len, - data: data + command, + length, + data, }) } @@ -350,7 +350,7 @@ impl MetaEvent { MetaEvent { command: MetaCommand::SequencerSpecificEvent, length: data.len() as u64, - data: data, + data, } } diff --git a/src/reader.rs b/src/reader.rs index 61a05b2..6cc3942 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -40,9 +40,11 @@ impl SMFReader { let tracks = (header[10] as u16) << 8 | header[11] as u16; let division = (header[12] as i16) << 8 | header[13] as i16; - Ok(SMF { format: format, - tracks: Vec::with_capacity(tracks as usize), - division: division } ) + Ok(SMF { + format, + tracks: Vec::with_capacity(tracks as usize), + division, + }) } fn next_event(reader: &mut dyn Read, laststat: u8, was_running: &mut bool) -> Result { @@ -156,9 +158,9 @@ impl SMFReader { } } Ok(Track { - copyright: copyright, - name: name, - events: res + copyright, + name, + events: res, }) } diff --git a/src/writer.rs b/src/writer.rs index 312c48e..f217c98 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -35,7 +35,7 @@ impl SMFWriter { pub fn new_with_division(ticks: i16) -> SMFWriter { SMFWriter { format: 1, - ticks: ticks, + ticks, tracks: Vec::new(), } } @@ -45,7 +45,7 @@ impl SMFWriter { pub fn new_with_division_and_format(format: SMFFormat, ticks: i16) -> SMFWriter { SMFWriter { format: format as u16, - ticks: ticks, + ticks, tracks: Vec::new(), } } From 5a0f28254a5017b5e392a53c097bdcadfd68d497 Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:10:20 -0500 Subject: [PATCH 02/12] Fix "length comparison to zero" clippy warnings See https://rust-lang.github.io/rust-clippy/master/index.html#len_zero --- src/lib.rs | 2 +- src/midi.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 03d2993..fae4e53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -302,7 +302,7 @@ impl SMF { division: self.division, }; for events in &mut tracks { - if events.len() > 0 { + if !events.is_empty() { let mut time = 0; for event in events.iter_mut() { let tmp = event.vtime; diff --git a/src/midi.rs b/src/midi.rs index 39d9957..a3d4072 100644 --- a/src/midi.rs +++ b/src/midi.rs @@ -331,7 +331,7 @@ impl fmt::Display for MidiMessage { else if self.data.len() == 3 { write!(f, "{}: [{},{}]\tchannel: {:?}", self.status(), self.data[1], self.data[2], self.channel()) } - else if self.data.len() == 0 { + else if self.data.is_empty() { write!(f, "{}: [no data]\tchannel: {:?}", self.status(), self.channel()) } else { From 2b0095dd4dcc6a84c66f806f754d48b433042392 Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:13:49 -0500 Subject: [PATCH 03/12] Fix clippy warning for collapsible if statement See https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if --- src/builder.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 2b8eb13..b998183 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -101,15 +101,9 @@ impl Ord for AbsoluteEvent { (&Event::Midi(ref me),&Event::Midi(ref you)) => { if me.data(0) < you.data(0) { Ordering::Less } else if me.data(0) > you.data(0) { Ordering::Greater } - else { - if me.data(1) < you.data(1) { - Ordering::Less - } else if me.data(1) > you.data(1) { - Ordering::Greater - } else { - res - } - } + else if me.data(1) < you.data(1) { Ordering::Less } + else if me.data(1) > you.data(1) { Ordering::Greater } + else { res } }, } } From b2e3f11c06d439e02bdf03d9d5fc9d0754d2a6df Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:14:56 -0500 Subject: [PATCH 04/12] Fix clippy warning: statics have by default a `'static` lifetime See https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes --- src/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 31a02a8..83ba892 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,7 +3,7 @@ use std::iter; use std::io::{Read,Error,ErrorKind}; -static NSTRS: &'static str = "C C#D D#E F F#G G#A A#B "; +static NSTRS: &str = "C C#D D#E F F#G G#A A#B "; /// convert a midi note number to a name pub fn note_num_to_name(num: u32) -> String { From 880f1336dd340844e656093edde4dcf61923d9a1 Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:17:06 -0500 Subject: [PATCH 05/12] Fix clippy warning: useless use of `format!` See https://rust-lang.github.io/rust-clippy/master/index.html#useless_format --- src/meta.rs | 6 +++--- src/util.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/meta.rs b/src/meta.rs index e406d47..63f8137 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -117,9 +117,9 @@ impl fmt::Display for MetaEvent { MetaCommand::CuePoint => format!("CuePoint: {}", latin1_decode(&self.data)), MetaCommand::MIDIChannelPrefixAssignment => format!("MIDI Channel Prefix Assignment, channel: {}", self.data[0]+1), MetaCommand::MIDIPortPrefixAssignment => format!("MIDI Port Prefix Assignment, port: {}", self.data[0]), - MetaCommand::EndOfTrack => format!("End Of Track"), + MetaCommand::EndOfTrack => "End Of Track".to_string(), MetaCommand::TempoSetting => format!("Set Tempo, microseconds/quarter note: {}", self.data_as_u64(3)), - MetaCommand::SMPTEOffset => format!("SMPTEOffset"), + MetaCommand::SMPTEOffset => "SMPTEOffset".to_string(), MetaCommand::TimeSignature => format!("Time Signature: {}/{}, {} ticks/metronome click, {} 32nd notes/quarter note", self.data[0], 2usize.pow(self.data[1] as u32), @@ -132,7 +132,7 @@ impl fmt::Display for MetaEvent { 1 => "Minor", _ => "Invalid Signature", }), - MetaCommand::SequencerSpecificEvent => format!("SequencerSpecificEvent"), + MetaCommand::SequencerSpecificEvent => "SequencerSpecificEvent".to_string(), MetaCommand::Unknown => format!("Unknown, length: {}", self.data.len()), }) } diff --git a/src/util.rs b/src/util.rs index 83ba892..bd391e9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -74,7 +74,7 @@ pub fn latin1_decode(s: &[u8]) -> String { Ok(s) => s, Err(_) => match str::from_utf8(s) { Ok(s) => s.to_string(), - Err(_) => format!("[invalid string data]"), + Err(_) => "[invalid string data]".to_string(), } } } From 90aadb04048e86a7a271d375ffb5c8765160cc3d Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:20:41 -0500 Subject: [PATCH 06/12] Fix clippy warnings about unnecessary literal casts See: 1) https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast 2) https://rust-lang.github.io/rust-clippy/master/index.html#char_lit_as_u8 --- src/util.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util.rs b/src/util.rs index bd391e9..13f4798 100644 --- a/src/util.rs +++ b/src/util.rs @@ -7,10 +7,10 @@ static NSTRS: &str = "C C#D D#E F F#G G#A A#B "; /// convert a midi note number to a name pub fn note_num_to_name(num: u32) -> String { - let oct = (num as f32 /12 as f32).floor()-1.0; + let oct = (num as f32 / 12_f32).floor()-1.0; let nmt = ((num%12)*2) as usize; let slice = - if NSTRS.as_bytes()[nmt+1] == ' ' as u8{ + if NSTRS.as_bytes()[nmt+1] == b' ' { &NSTRS[nmt..(nmt+1)] } else { &NSTRS[nmt..(nmt+2)] From 8d695e4cb461f111534e096689c1dab045e8c663 Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:23:47 -0500 Subject: [PATCH 07/12] Fix clippy warning: manual implementation of an assign operation See https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_pattern --- src/reader.rs | 2 +- src/writer.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 6cc3942..65a78b6 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -181,7 +181,7 @@ impl SMFReader { if (next & cont_mask) == 0 { break; } - res = res << 7; + res <<= 7; } Ok(res) } diff --git a/src/writer.rs b/src/writer.rs index f217c98..75f87de 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -81,7 +81,7 @@ impl SMFWriter { let val_mask = 0x7F as u64; loop { let mut to_write = (cur & val_mask) as u8; - cur = cur >> 7; + cur >>= 7; if continuation { // we're writing a continuation byte, so set the bit to_write |= cont_mask; @@ -147,7 +147,7 @@ impl SMFWriter { let lbyte = (*length & 0xFF) as u8; // 7-i because smf is big endian and we want to put this in bytes 4-7 vec[7-i] = lbyte; - *length = (*length)>>8; + *length >>= 8; } } From cd07494b500c27c0b712e7c31d70f19dc3a9f6ac Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:30:35 -0500 Subject: [PATCH 08/12] Fix clippy warning: re-implementing `PartialEq::ne` is unnecessary See https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl --- src/builder.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index b998183..dd80f5b 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -74,10 +74,6 @@ impl PartialEq for AbsoluteEvent { false } } - - fn ne(&self, other: &AbsoluteEvent) -> bool { - !(self.eq(other)) - } } // Implement `Ord` and sort messages by time From 573eacb91373fb5dc9765cadce19c41d889fce9a Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:33:15 -0500 Subject: [PATCH 09/12] Fix clippy warning: you don't need to add `&` to all patterns https://rust-lang.github.io/rust-clippy/master/index.html#match_ref_pats --- src/writer.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/writer.rs b/src/writer.rs index 75f87de..128a5e1 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -114,12 +114,12 @@ impl SMFWriter { } fn write_event(&self, vec: &mut Vec, event: &Event, length: &mut u32, saw_eot: &mut bool) { - match event { - &Event::Midi(ref midi) => { + match *event { + Event::Midi(ref midi) => { vec.extend(midi.data.iter()); *length += midi.data.len() as u32; } - &Event::Meta(ref meta) => { + Event::Meta(ref meta) => { vec.push(0xff); // indicate we're writing a meta event vec.push(meta.command as u8); // +2 on next line for the 0xff and the command byte we just wrote From 01e08e2bd6b53748e6adc995208d3969694c6c4d Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:36:56 -0500 Subject: [PATCH 10/12] Fix clippy warning: warning: you seem to be trying to use match for destructuring a single pattern. Consider using `if let` See https://rust-lang.github.io/rust-clippy/master/index.html#single_match --- src/reader.rs | 30 ++++++++++++------------------ src/writer.rs | 11 ++++------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 65a78b6..02f7712 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -108,9 +108,9 @@ impl SMFReader { let last = { // use status from last midi event, skip meta events let mut last = 0u8; for e in res.iter().rev() { - match e.event { - Event::Midi(ref m) => { last = m.data[0]; break; } - _ => () + if let Event::Midi(ref m) = e.event { + last = m.data[0]; + break; } } last @@ -118,15 +118,12 @@ impl SMFReader { let mut was_running = false; match SMFReader::next_event(reader,last,&mut was_running) { Ok(event) => { - match event.event { - Event::Meta(ref me) => { - match me.command { - MetaCommand::CopyrightNotice => copyright = Some(latin1_decode(&me.data)), - MetaCommand::SequenceOrTrackName => name = Some(latin1_decode(&me.data)), - _ => {} - } - }, - _ => {} + if let Event::Meta(ref me) = event.event { + match me.command { + MetaCommand::CopyrightNotice => copyright = Some(latin1_decode(&me.data)), + MetaCommand::SequenceOrTrackName => name = Some(latin1_decode(&me.data)), + _ => {} + } } read_so_far += event.len(); if was_running { @@ -189,13 +186,10 @@ impl SMFReader { /// Read an entire SMF file pub fn read_smf(reader: &mut dyn Read) -> Result { let mut smf = SMFReader::parse_header(reader); - match smf { - Ok(ref mut s) => { - for _ in 0..s.tracks.capacity() { - s.tracks.push(SMFReader::parse_track(reader)?); - } + if let Ok(ref mut s) = smf { + for _ in 0..s.tracks.capacity() { + s.tracks.push(SMFReader::parse_track(reader)?); } - _ => {} } smf } diff --git a/src/writer.rs b/src/writer.rs index 128a5e1..6e7fac4 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -167,13 +167,10 @@ impl SMFWriter { let mut cur_time: u64 = 0; let mut saw_eot = false; - match name { - Some(n) => { - let namemeta = Event::Meta(MetaEvent::sequence_or_track_name(n)); - length += SMFWriter::write_vtime(0,&mut vec).unwrap(); - self.write_event(&mut vec, &namemeta, &mut length, &mut saw_eot); - } - None => {} + if let Some(n) = name { + let namemeta = Event::Meta(MetaEvent::sequence_or_track_name(n)); + length += SMFWriter::write_vtime(0,&mut vec).unwrap(); + self.write_event(&mut vec, &namemeta, &mut length, &mut saw_eot); } for ev in track { From 0fb7313f6ce028ee17e7ed3abaef94bd48c00a10 Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Fri, 4 Dec 2020 16:47:57 -0500 Subject: [PATCH 11/12] Fix clippy error: read amount is not handled. Use `Read::read_exact` instead See https://rust-lang.github.io/rust-clippy/master/index.html#unused_io_amount --- src/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 13f4798..6e99c17 100644 --- a/src/util.rs +++ b/src/util.rs @@ -21,7 +21,7 @@ pub fn note_num_to_name(num: u32) -> String { /// Read a single byte from a Reader pub fn read_byte(reader: &mut dyn Read) -> Result { let mut b = [0; 1]; - reader.read(&mut b)?; + reader.read_exact(&mut b)?; Ok(b[0]) } From 0ed8d9dfea71f21e229840a7e8ea18e26525fdb7 Mon Sep 17 00:00:00 2001 From: Alex Kesling Date: Mon, 7 Dec 2020 13:09:37 -0500 Subject: [PATCH 12/12] Update license and authors entries to reflect contribution copyright --- Cargo.toml | 2 +- LICENSE | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 716f4dd..5e8160d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "rimd" description = "Library for handling Midi and reading and writing Standard Midi Files in Rust" version = "0.0.3" -authors = ["Nick Lanham "] +authors = ["Nick Lanham ", "Alex Kesling "] homepage = "https://github.com/RustAudio/rimd" repository = "https://github.com/RustAudio/rimd" documentation = "https://nicklan.github.io/rimd/target/doc/rimd/index.html" diff --git a/LICENSE b/LICENSE index d79c3ad..66cee32 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) Copyright (c) 2015 Nick Lanham +Copyright (c) 2020 Alex Kesling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal