Skip to content

Commit

Permalink
do it w/o Deref
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Jun 14, 2022
1 parent 45dee41 commit c912781
Showing 1 changed file with 29 additions and 41 deletions.
70 changes: 29 additions & 41 deletions src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::fmt::Display;
use std::io::Read;
use std::ops::{Deref, DerefMut};
use std::ops::Deref;
use std::rc::Rc;

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -547,19 +547,6 @@ impl From<Migration> for UnresolvedMigration {
#[repr(transparent)]
pub struct Pulse(PulseData);

impl Deref for Pulse {
type Target = PulseData;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Pulse {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

#[derive(Clone, Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct PulseData {
Expand All @@ -574,7 +561,7 @@ impl Pulse {
match deme_map.get(deme) {
Some(d) => {
let t = d.time_interval();
let time = match self.time {
let time = match self.0.time {
Some(t) => t,
None => return Err(DemesError::PulseError("time is None".to_string())),
};
Expand All @@ -594,7 +581,7 @@ impl Pulse {
}

fn validate_pulse_time(&self, deme_map: &DemeMap) -> Result<(), DemesError> {
match self.time {
match self.0.time {
Some(time) => {
if !time.is_valid_pulse_time() {
return Err(DemesError::PulseError(format!(
Expand All @@ -606,7 +593,7 @@ impl Pulse {
None => return Err(DemesError::PulseError("time is None".to_string())),
}

for source_name in self.sources.as_ref().unwrap() {
for source_name in self.0.sources.as_ref().unwrap() {
let source = deme_map.get(source_name).unwrap();

let ti = source.time_interval();
Expand Down Expand Up @@ -634,15 +621,15 @@ impl Pulse {
}

fn validate_proportions(&self) -> Result<(), DemesError> {
if self.proportions.is_none() {
if self.0.proportions.is_none() {
return Err(DemesError::PulseError("proportions is None".to_string()));
}
if self.sources.is_none() {
if self.0.sources.is_none() {
return Err(DemesError::PulseError("sources is None".to_string()));
}

let proportions = self.proportions.as_ref().unwrap();
let sources = self.sources.as_ref().unwrap();
let proportions = self.0.proportions.as_ref().unwrap();
let sources = self.0.sources.as_ref().unwrap();
if proportions.len() != sources.len() {
return Err(DemesError::PulseError(format!("number of sources must equal number of proportions; got {} source and {} proportions", sources.len(), proportions.len())));
}
Expand All @@ -662,8 +649,8 @@ impl Pulse {
}

fn dest_is_not_source(&self) -> Result<(), DemesError> {
let dest = self.dest.as_ref().unwrap();
if self.sources.as_ref().unwrap().contains(dest) {
let dest = self.0.dest.as_ref().unwrap();
if self.0.sources.as_ref().unwrap().contains(dest) {
Err(DemesError::PulseError(format!(
"dest: {} is also listed as a source",
dest
Expand All @@ -675,7 +662,7 @@ impl Pulse {

fn sources_are_unique(&self) -> Result<(), DemesError> {
let mut sources = HashSet::<String>::default();
for source in self.sources.as_ref().unwrap() {
for source in self.0.sources.as_ref().unwrap() {
if sources.contains(source) {
return Err(DemesError::PulseError(format!(
"source: {} listed multiple times",
Expand All @@ -692,18 +679,19 @@ impl Pulse {

// NOTE: validate proportions is taking care of
// returning Err if this is not true
assert!(self.sources.is_some());
assert!(self.0.sources.is_some());

let sources = self.sources.as_ref().unwrap();
let sources = self.0.sources.as_ref().unwrap();
sources
.iter()
.try_for_each(|source| self.validate_deme_existence(source, deme_map))?;

self.dest
self.0
.dest
.as_ref()
.ok_or_else(|| DemesError::PulseError("dest is None".to_string()))?;

self.validate_deme_existence(self.dest.as_ref().unwrap(), deme_map)?;
self.validate_deme_existence(self.0.dest.as_ref().unwrap(), deme_map)?;
self.dest_is_not_source()?;
self.sources_are_unique()?;
self.validate_pulse_time(deme_map)
Expand All @@ -715,28 +703,28 @@ impl Pulse {
}

pub fn time(&self) -> Time {
match self.time {
match self.0.time {
Some(time) => time,
None => panic!("pulse time is None"),
}
}

pub fn sources(&self) -> &[String] {
match &self.sources {
match &self.0.sources {
Some(sources) => sources,
None => panic!("sources are None"),
}
}

pub fn dest(&self) -> &str {
match &self.dest {
match &self.0.dest {
Some(dest) => dest,
None => panic!("pulse dest is None"),
}
}

pub fn proportions(&self) -> &[Proportion] {
match &self.proportions {
match &self.0.proportions {
Some(proportions) => proportions,
None => panic!("proportions are None"),
}
Expand Down Expand Up @@ -1647,17 +1635,17 @@ impl GraphDefaults {
}

fn apply_pulse_defaults(&self, other: &mut Pulse) {
if other.time.is_none() {
other.time = self.pulse.time;
if other.0.time.is_none() {
other.0.time = self.pulse.time;
}
if other.sources.is_none() {
other.sources = self.pulse.sources.clone();
if other.0.sources.is_none() {
other.0.sources = self.pulse.sources.clone();
}
if other.dest.is_none() {
other.dest = self.pulse.dest.clone();
if other.0.dest.is_none() {
other.0.dest = self.pulse.dest.clone();
}
if other.proportions.is_none() {
other.proportions = self.pulse.proportions.clone();
if other.0.proportions.is_none() {
other.0.proportions = self.pulse.proportions.clone();
}
}
}
Expand Down Expand Up @@ -2143,7 +2131,7 @@ impl Graph {
// NOTE: the sort_by flips the order to b, a
// to put more ancient events at the front.
self.pulses
.sort_by(|a, b| b.time.partial_cmp(&a.time).unwrap());
.sort_by(|a, b| b.0.time.partial_cmp(&a.0.time).unwrap());
Ok(())
}

Expand Down

0 comments on commit c912781

Please sign in to comment.