Implement Deref for newtypes

This commit is contained in:
Simon Bruder 2020-07-04 23:17:17 +02:00
parent 0ea4a20326
commit 19eebdc345
No known key found for this signature in database
GPG key ID: 6F03E0000CC5B62F
6 changed files with 41 additions and 34 deletions

12
Cargo.lock generated
View file

@ -51,6 +51,7 @@ dependencies = [
"anyhow", "anyhow",
"byteorder", "byteorder",
"clap", "clap",
"derive_more",
"konami-lz77", "konami-lz77",
"log", "log",
"num-derive", "num-derive",
@ -140,6 +141,17 @@ dependencies = [
"lazy_static", "lazy_static",
] ]
[[package]]
name = "derive_more"
version = "0.99.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "298998b1cf6b5b2c8a7b023dfd45821825ce3ba8a8af55c921a0e734e4653f76"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.7.1" version = "0.7.1"

View file

@ -8,6 +8,7 @@ edition = "2018"
anyhow = "1.0.31" anyhow = "1.0.31"
byteorder = "1.3.4" byteorder = "1.3.4"
clap = "3.0.0-beta.1" clap = "3.0.0-beta.1"
derive_more = "0.99.9"
konami-lz77 = { git = "https://github.com/sbruder/konami-lz77" } konami-lz77 = { git = "https://github.com/sbruder/konami-lz77" }
log = "0.4.8" log = "0.4.8"
num-derive = "0.3" num-derive = "0.3"

View file

@ -144,8 +144,8 @@ impl ShockStepGenerator {
} }
} }
fn get_time_from_beats(beats: f32, tempo_changes: &[ssq::TempoChange]) -> Option<beatmap::Time> { fn get_time_from_beats(beats: f32, tempo_changes: &ssq::TempoChanges) -> Option<beatmap::Time> {
for tempo_change in tempo_changes { for tempo_change in tempo_changes.to_vec() {
// For TempoChanges that are infinitely short but exactly cover that beat, use the start // For TempoChanges that are infinitely short but exactly cover that beat, use the start
// time of that TempoChange // time of that TempoChange
if (beats - tempo_change.start_beats).abs() < 0.001 if (beats - tempo_change.start_beats).abs() < 0.001
@ -198,7 +198,7 @@ impl ssq::Step {
match self { match self {
ssq::Step::Step { beats, row } => { ssq::Step::Step { beats, row } => {
let time = get_time_from_beats(*beats, &tempo_changes.0); let time = get_time_from_beats(*beats, tempo_changes);
match time { match time {
Some(time) => { Some(time) => {
@ -236,8 +236,8 @@ impl ssq::Step {
} }
} }
ssq::Step::Freeze { start, end, row } => { ssq::Step::Freeze { start, end, row } => {
let time = get_time_from_beats(*start, &tempo_changes.0); let time = get_time_from_beats(*start, tempo_changes);
let end_time = get_time_from_beats(*end, &tempo_changes.0); let end_time = get_time_from_beats(*end, tempo_changes);
match (time, end_time) { match (time, end_time) {
(Some(time), Some(end_time)) => { (Some(time), Some(end_time)) => {
@ -290,7 +290,7 @@ impl ssq::Step {
hit_objects.push(beatmap::HitObject::HitCircle { hit_objects.push(beatmap::HitObject::HitCircle {
x: beatmap::column_to_x(column as u8, num_columns), x: beatmap::column_to_x(column as u8, num_columns),
y: 192, y: 192,
time: get_time_from_beats(*beats, &tempo_changes.0)?, time: get_time_from_beats(*beats, tempo_changes)?,
hit_sound: beatmap::HitSound { hit_sound: beatmap::HitSound {
normal: true, normal: true,
whistle: false, whistle: false,
@ -378,18 +378,17 @@ impl ssq::SSQ {
pub fn to_beatmaps(&self, config: &Config) -> Result<Vec<beatmap::Beatmap>> { pub fn to_beatmaps(&self, config: &Config) -> Result<Vec<beatmap::Beatmap>> {
debug!("Configuration: {:?}", config); debug!("Configuration: {:?}", config);
let mut timing_points = Vec::new(); let mut timing_points = beatmap::TimingPoints(Vec::new());
for entry in &self.tempo_changes.0 { for entry in self.tempo_changes.to_vec() {
if config.stops || entry.beat_length != f32::INFINITY { if config.stops || entry.beat_length != f32::INFINITY {
trace!("Converting {:?} to to timing point", entry); trace!("Converting {:?} to to timing point", entry);
let timing_point: beatmap::TimingPoint = entry.clone().into(); timing_points.push(entry.into());
timing_points.push(timing_point);
} }
} }
debug!( debug!(
"Converted {} tempo changes to timing points", "Converted {} tempo changes to timing points",
self.tempo_changes.0.len() self.tempo_changes.len()
); );
let mut converted_charts = Vec::new(); let mut converted_charts = Vec::new();
@ -407,19 +406,19 @@ impl ssq::SSQ {
&self.tempo_changes, &self.tempo_changes,
&mut shock_step_generator, &mut shock_step_generator,
) { ) {
hit_objects.0.append(&mut step_hit_objects); hit_objects.append(&mut step_hit_objects);
} }
} }
let converted_chart = ConvertedChart { let converted_chart = ConvertedChart {
difficulty: chart.difficulty.clone(), difficulty: chart.difficulty.clone(),
hit_objects, hit_objects,
timing_points: beatmap::TimingPoints(timing_points.clone()), timing_points: timing_points.clone(),
}; };
debug!( debug!(
"Converted to beatmap with {} hit objects", "Converted to beatmap with {} hit objects",
converted_chart.hit_objects.0.len(), converted_chart.hit_objects.len(),
); );
converted_charts.push(converted_chart); converted_charts.push(converted_chart);

View file

@ -1,7 +1,7 @@
use std::ops::Deref;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use derive_more::Deref;
use quick_xml::de::{from_str, DeError}; use quick_xml::de::{from_str, DeError};
use serde::de; use serde::de;
use serde::Deserialize; use serde::Deserialize;
@ -24,7 +24,7 @@ pub enum Error {
/// Type that implements [`serde::de::Deserialize`] for space separated lists in xml tag bodies. /// Type that implements [`serde::de::Deserialize`] for space separated lists in xml tag bodies.
/// ///
/// [`serde::de::Deserialize`]: ../../../serde/de/trait.Deserialize.html /// [`serde::de::Deserialize`]: ../../../serde/de/trait.Deserialize.html
#[derive(Debug)] #[derive(Debug, Deref)]
pub struct XMLList<T>(Vec<T>); pub struct XMLList<T>(Vec<T>);
impl<'de, T> serde::de::Deserialize<'de> for XMLList<T> impl<'de, T> serde::de::Deserialize<'de> for XMLList<T>
@ -45,14 +45,6 @@ where
} }
} }
impl<T> Deref for XMLList<T> {
type Target = Vec<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// This currently only includes fields present in every entry. /// This currently only includes fields present in every entry.
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Entry { pub struct Entry {

View file

@ -7,6 +7,7 @@ use std::io::Cursor;
use std::num; use std::num;
use byteorder::{ReadBytesExt, LE}; use byteorder::{ReadBytesExt, LE};
use derive_more::Deref;
use log::{debug, info, trace, warn}; use log::{debug, info, trace, warn};
use thiserror::Error; use thiserror::Error;
@ -159,7 +160,7 @@ pub struct TempoChange {
pub beat_length: f32, pub beat_length: f32,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Deref, PartialEq)]
pub struct TempoChanges(pub Vec<TempoChange>); pub struct TempoChanges(pub Vec<TempoChange>);
impl TempoChanges { impl TempoChanges {

View file

@ -1,6 +1,8 @@
use std::fmt;
use derive_more::{Deref, DerefMut};
use num_derive::ToPrimitive; use num_derive::ToPrimitive;
use num_traits::ToPrimitive; use num_traits::ToPrimitive;
use std::fmt;
use crate::utils; use crate::utils;
@ -165,7 +167,7 @@ impl fmt::Display for Difficulty {
} }
} }
#[derive(Clone)] #[derive(Clone, Deref)]
pub struct Events(pub Vec<Event>); pub struct Events(pub Vec<Event>);
impl fmt::Display for Events { impl fmt::Display for Events {
@ -176,7 +178,7 @@ impl fmt::Display for Events {
[Events]\n\ [Events]\n\
{}\n\ {}\n\
", ",
utils::join_display_values(self.0.clone(), "\n") utils::join_display_values(self.to_vec(), "\n")
) )
} }
} }
@ -226,7 +228,7 @@ impl fmt::Display for Event {
} }
} }
#[derive(Clone)] #[derive(Clone, Deref, DerefMut)]
pub struct TimingPoints(pub Vec<TimingPoint>); pub struct TimingPoints(pub Vec<TimingPoint>);
impl fmt::Display for TimingPoints { impl fmt::Display for TimingPoints {
@ -237,7 +239,7 @@ impl fmt::Display for TimingPoints {
[TimingPoints]\n\ [TimingPoints]\n\
{}\n\ {}\n\
", ",
utils::join_display_values(self.0.clone(), "\n") utils::join_display_values(self.to_vec(), "\n")
) )
} }
} }
@ -296,7 +298,7 @@ impl fmt::Display for TimingPoint {
} }
} }
#[derive(Clone)] #[derive(Clone, Deref)]
pub struct Colours(pub Vec<Colour>); pub struct Colours(pub Vec<Colour>);
impl fmt::Display for Colours { impl fmt::Display for Colours {
@ -307,7 +309,7 @@ impl fmt::Display for Colours {
[Colours]\n\ [Colours]\n\
{}\n\ {}\n\
", ",
utils::join_display_values(self.0.clone(), "\n") utils::join_display_values(self.to_vec(), "\n")
) )
} }
} }
@ -542,7 +544,7 @@ impl fmt::Display for HitObject {
} }
} }
#[derive(Clone)] #[derive(Clone, Deref, DerefMut)]
pub struct HitObjects(pub Vec<HitObject>); pub struct HitObjects(pub Vec<HitObject>);
impl fmt::Display for HitObjects { impl fmt::Display for HitObjects {
@ -553,7 +555,7 @@ impl fmt::Display for HitObjects {
[HitObjects]\n\ [HitObjects]\n\
{}\n\ {}\n\
", ",
utils::join_display_values(self.0.clone(), "\n") utils::join_display_values(self.to_vec(), "\n")
) )
} }
} }