From d0f6c2a8e44279c456c6bc6dabdffd4d625f722d Mon Sep 17 00:00:00 2001 From: "Felipe S. S. Schneider" Date: Sat, 30 Mar 2024 11:04:13 -0300 Subject: [PATCH] refactor: use Self when able --- src/chord.rs | 2 +- src/chord_sequence.rs | 2 +- src/chord_type.rs | 6 +++--- src/distance.rs | 4 ++-- src/fingering.rs | 2 +- src/tuning.rs | 6 +++--- src/voicing.rs | 6 +++--- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/chord.rs b/src/chord.rs index 44357f7..8036dc6 100644 --- a/src/chord.rs +++ b/src/chord.rs @@ -74,7 +74,7 @@ impl Chord { .sorted() } - pub fn transpose(&self, semitones: i8) -> Chord { + pub fn transpose(&self, semitones: i8) -> Self { match semitones { s if s < 0 => self.clone() - semitones.unsigned_abs() as Semitones, _ => self.clone() + semitones as Semitones, diff --git a/src/chord_sequence.rs b/src/chord_sequence.rs index 5ec436d..f49d254 100644 --- a/src/chord_sequence.rs +++ b/src/chord_sequence.rs @@ -13,7 +13,7 @@ impl ChordSequence { self.chords.iter() } - pub fn transpose(&self, semitones: i8) -> ChordSequence { + pub fn transpose(&self, semitones: i8) -> Self { let chords = self.chords().map(|c| c.transpose(semitones)).collect(); Self { chords } } diff --git a/src/chord_type.rs b/src/chord_type.rs index c799e79..0911848 100644 --- a/src/chord_type.rs +++ b/src/chord_type.rs @@ -65,7 +65,7 @@ impl ChordType { /// /// Unfortunately, we have to list them all and make sure to update /// this list if a value is added or removed. - pub fn values() -> impl Iterator { + pub fn values() -> impl Iterator { use ChordType::*; [ @@ -298,7 +298,7 @@ impl FromStr for ChordType { type Err = NoValidChordTypeError; fn from_str(s: &str) -> Result { - ChordType::values() + Self::values() .find(|ct| ct.symbols().any(|sym| sym == s)) .ok_or(NoValidChordTypeError) } @@ -328,7 +328,7 @@ impl TryFrom<&[PitchClass]> for ChordType { } }; - for chord_type in ChordType::values() { + for chord_type in Self::values() { // If a chord has less required intervals than we have strings, add optional intervals // until all strings are used. let min_len = min(chord_type.intervals().count(), STRING_COUNT); diff --git a/src/distance.rs b/src/distance.rs index 61ef9e3..258906a 100644 --- a/src/distance.rs +++ b/src/distance.rs @@ -23,8 +23,8 @@ impl Distance { impl Add for Distance { type Output = Self; - fn add(self, other: Distance) -> Self { - Distance(self.0 + other.0, self.1 + other.1) + fn add(self, other: Self) -> Self { + Self(self.0 + other.0, self.1 + other.1) } } diff --git a/src/fingering.rs b/src/fingering.rs index 380d405..ae28200 100644 --- a/src/fingering.rs +++ b/src/fingering.rs @@ -10,7 +10,7 @@ pub struct Fingering { impl Fingering { /// Compute the distance between two fingerings, inspired by /// http://www.petecorey.com/blog/2018/08/27/computing-fingering-distance-with-dr-levenshtein/ - pub fn distance(&self, other: Fingering) -> u8 { + pub fn distance(&self, other: Self) -> u8 { let dist = |(&(s1, f1), &(s2, f2))| { let add = s1 == 0 && s2 != 0; let remove = s1 != 0 && s2 == 0; diff --git a/src/tuning.rs b/src/tuning.rs index 56fb238..c2086d6 100644 --- a/src/tuning.rs +++ b/src/tuning.rs @@ -44,9 +44,9 @@ impl Tuning { impl fmt::Display for Tuning { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s = match self { - Tuning::C => "C", - Tuning::D => "D", - Tuning::G => "G", + Self::C => "C", + Self::D => "D", + Self::G => "G", }; write!(f, "{s}") diff --git a/src/voicing.rs b/src/voicing.rs index 5c58938..370b80d 100644 --- a/src/voicing.rs +++ b/src/voicing.rs @@ -226,21 +226,21 @@ impl Voicing { /// It's computed by simply summing up the distances between the frets that /// are pressed down on the same string when moving from one voicing to the other. /// Inspired by http://www.petecorey.com/blog/2018/07/30/voice-leading-with-elixir/ - pub fn semitone_distance(&self, other: Voicing) -> u8 { + pub fn semitone_distance(&self, other: Self) -> u8 { self.frets() .zip(other.frets()) .map(|(f1, f2)| max(f1, f2) - min(f1, f2)) .sum() } - pub fn fingering_distance(&self, other: Voicing) -> u8 { + pub fn fingering_distance(&self, other: Self) -> u8 { let l_fingering = Fingering::from(*self); let r_fingering = Fingering::from(other); l_fingering.distance(r_fingering) } - pub fn distance(&self, other: Voicing) -> Distance { + pub fn distance(&self, other: Self) -> Distance { let semitone_distance = self.semitone_distance(other); let fingering_distance = self.fingering_distance(other);