From dd52fdf46d2fbc9d1f84e7110202c63fdd031a76 Mon Sep 17 00:00:00 2001 From: "Christopher H. Jordan" Date: Tue, 14 Jun 2022 14:23:10 +0800 Subject: [PATCH] API tweaks surrounding generic types. This commit allows multiple generic types on public-facing new methods. A good example of why this is useful is for the correlator context; when using a &Path for the metafits, gpubox files must then be a slice of &Path, which is an unusual collection to have. This commit makes it possible to use a &Path for the metafits and &[PathBuf] for the gpuboxes. Also split the new method logic into new_inner functions. These should remain private, and this is done to help keep compilation complexity low. See "Compilation Model: Monomorphization" from this site for more info: https://matklad.github.io/2021/09/04/fast-rust-builds.html --- CHANGELOG.md | 4 ++++ src/correlator_context/mod.rs | 14 +++++++++++--- src/correlator_context/test.rs | 4 ++-- src/metafits_context/mod.rs | 12 +++++++++--- src/voltage_context/mod.rs | 14 +++++++++++--- src/voltage_context/test.rs | 3 ++- 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bceb75..f58fc47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Changes in each release are listed below. +## Unreleased +* API tweaks surrounding generic types. + * Things should be slightly more flexible than before. + ## 0.14.0 13-Apr-2022 (Pre-release) * Specify minimum-required dependency versions. * Clean up fits long-string functions. diff --git a/src/correlator_context/mod.rs b/src/correlator_context/mod.rs index 478eefc..c2c7278 100644 --- a/src/correlator_context/mod.rs +++ b/src/correlator_context/mod.rs @@ -7,6 +7,7 @@ The main interface to MWA data. */ use std::collections::BTreeMap; use std::fmt; +use std::path::Path; use crate::coarse_channel::*; use crate::convert::*; @@ -132,9 +133,16 @@ impl CorrelatorContext { /// * Result containing a populated CorrelatorContext object if Ok. /// /// - pub fn new>( - metafits_filename: T, - gpubox_filenames: &[T], + pub fn new, P2: AsRef>( + metafits_filename: P, + gpubox_filenames: &[P2], + ) -> Result { + Self::new_inner(metafits_filename.as_ref(), gpubox_filenames) + } + + fn new_inner>( + metafits_filename: &Path, + gpubox_filenames: &[P], ) -> Result { let mut metafits_context = MetafitsContext::new_internal(metafits_filename)?; diff --git a/src/correlator_context/test.rs b/src/correlator_context/test.rs index 369b38d..f43a4f4 100644 --- a/src/correlator_context/test.rs +++ b/src/correlator_context/test.rs @@ -5,9 +5,9 @@ /*! Unit tests for correlator context */ -#[cfg(test)] use super::*; use float_cmp::*; +use std::path::PathBuf; #[test] fn test_context_new_with_only_non000_batch() { @@ -63,7 +63,7 @@ fn test_context_new_with_000_and_001_batch() { #[test] fn test_context_new_missing_gpubox_files() { let metafits_filename = "test_files/1101503312_1_timestep/1101503312.metafits"; - let gpuboxfiles = Vec::new(); + let gpuboxfiles: Vec = Vec::new(); // No gpubox files provided let context = CorrelatorContext::new(metafits_filename, &gpuboxfiles); diff --git a/src/metafits_context/mod.rs b/src/metafits_context/mod.rs index 873f529..f58afbe 100644 --- a/src/metafits_context/mod.rs +++ b/src/metafits_context/mod.rs @@ -5,9 +5,11 @@ /*! The main interface to MWA data. */ +use std::fmt; +use std::path::Path; + use chrono::{DateTime, Duration, FixedOffset}; use num_derive::FromPrimitive; -use std::fmt; use crate::antenna::*; use crate::baseline::*; @@ -467,10 +469,14 @@ impl MetafitsContext { /// * Result containing a populated MetafitsContext object if Ok. /// /// - pub fn new>( - metafits: T, + pub fn new>( + metafits: P, mwa_version: Option, ) -> Result { + Self::new_inner(metafits.as_ref(), mwa_version) + } + + fn new_inner(metafits: &Path, mwa_version: Option) -> Result { // Call the internal new metafits method let mut new_context = MetafitsContext::new_internal(metafits)?; diff --git a/src/voltage_context/mod.rs b/src/voltage_context/mod.rs index 92fc34b..f5465fd 100644 --- a/src/voltage_context/mod.rs +++ b/src/voltage_context/mod.rs @@ -15,6 +15,7 @@ use std::fmt; use std::fs::File; use std::io::BufReader; use std::io::{Read, Seek, SeekFrom}; +use std::path::Path; #[cfg(test)] pub(crate) mod test; // It's pub crate because I reuse some test code in the ffi tests. @@ -153,9 +154,16 @@ impl VoltageContext { /// * Result containing a populated VoltageContext object if Ok. /// /// - pub fn new>( - metafits_filename: T, - voltage_filenames: &[T], + pub fn new, P2: AsRef>( + metafits_filename: P, + voltage_filenames: &[P2], + ) -> Result { + Self::new_inner(metafits_filename.as_ref(), voltage_filenames) + } + + fn new_inner>( + metafits_filename: &Path, + voltage_filenames: &[P], ) -> Result { let mut metafits_context = MetafitsContext::new_internal(metafits_filename)?; diff --git a/src/voltage_context/test.rs b/src/voltage_context/test.rs index b28cf2a..11c95b9 100644 --- a/src/voltage_context/test.rs +++ b/src/voltage_context/test.rs @@ -10,6 +10,7 @@ use super::*; use float_cmp::*; use std::fs::File; use std::io::{Error, Write}; +use std::path::PathBuf; use std::sync::Once; // Define two static "once" variables to control creation of VCS test data (so it only happens once, the first time it's needed) @@ -308,7 +309,7 @@ pub(crate) fn get_test_voltage_context(mwa_version: MWAVersion) -> VoltageContex #[test] fn test_context_new_missing_voltage_files() { let metafits_filename = "test_files/1101503312_1_timestep/1101503312.metafits"; - let voltagefiles = Vec::new(); + let voltagefiles: Vec = Vec::new(); // No gpubox files provided let context = VoltageContext::new(&metafits_filename, &voltagefiles);