Skip to content

Commit

Permalink
refactor: use Self when able
Browse files Browse the repository at this point in the history
  • Loading branch information
schneiderfelipe authored and noeddl committed May 8, 2024
1 parent 481fabd commit d0f6c2a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/chord_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
6 changes: 3 additions & 3 deletions src/chord_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = ChordType> {
pub fn values() -> impl Iterator<Item = Self> {
use ChordType::*;

[
Expand Down Expand Up @@ -298,7 +298,7 @@ impl FromStr for ChordType {
type Err = NoValidChordTypeError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
ChordType::values()
Self::values()
.find(|ct| ct.symbols().any(|sym| sym == s))
.ok_or(NoValidChordTypeError)
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/fingering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/tuning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
6 changes: 3 additions & 3 deletions src/voicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d0f6c2a

Please sign in to comment.