Skip to content

Commit

Permalink
API tweaks surrounding generic types.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cjordan committed Jun 14, 2022
1 parent d3d404b commit dd52fdf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions src/correlator_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -132,9 +133,16 @@ impl CorrelatorContext {
/// * Result containing a populated CorrelatorContext object if Ok.
///
///
pub fn new<T: AsRef<std::path::Path>>(
metafits_filename: T,
gpubox_filenames: &[T],
pub fn new<P: AsRef<Path>, P2: AsRef<Path>>(
metafits_filename: P,
gpubox_filenames: &[P2],
) -> Result<Self, MwalibError> {
Self::new_inner(metafits_filename.as_ref(), gpubox_filenames)
}

fn new_inner<P: AsRef<Path>>(
metafits_filename: &Path,
gpubox_filenames: &[P],
) -> Result<Self, MwalibError> {
let mut metafits_context = MetafitsContext::new_internal(metafits_filename)?;

Expand Down
4 changes: 2 additions & 2 deletions src/correlator_context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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<PathBuf> = Vec::new();

// No gpubox files provided
let context = CorrelatorContext::new(metafits_filename, &gpuboxfiles);
Expand Down
12 changes: 9 additions & 3 deletions src/metafits_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -467,10 +469,14 @@ impl MetafitsContext {
/// * Result containing a populated MetafitsContext object if Ok.
///
///
pub fn new<T: AsRef<std::path::Path>>(
metafits: T,
pub fn new<P: AsRef<Path>>(
metafits: P,
mwa_version: Option<MWAVersion>,
) -> Result<Self, MwalibError> {
Self::new_inner(metafits.as_ref(), mwa_version)
}

fn new_inner(metafits: &Path, mwa_version: Option<MWAVersion>) -> Result<Self, MwalibError> {
// Call the internal new metafits method
let mut new_context = MetafitsContext::new_internal(metafits)?;

Expand Down
14 changes: 11 additions & 3 deletions src/voltage_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -153,9 +154,16 @@ impl VoltageContext {
/// * Result containing a populated VoltageContext object if Ok.
///
///
pub fn new<T: AsRef<std::path::Path>>(
metafits_filename: T,
voltage_filenames: &[T],
pub fn new<P: AsRef<Path>, P2: AsRef<Path>>(
metafits_filename: P,
voltage_filenames: &[P2],
) -> Result<Self, MwalibError> {
Self::new_inner(metafits_filename.as_ref(), voltage_filenames)
}

fn new_inner<P: AsRef<Path>>(
metafits_filename: &Path,
voltage_filenames: &[P],
) -> Result<Self, MwalibError> {
let mut metafits_context = MetafitsContext::new_internal(metafits_filename)?;

Expand Down
3 changes: 2 additions & 1 deletion src/voltage_context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<PathBuf> = Vec::new();

// No gpubox files provided
let context = VoltageContext::new(&metafits_filename, &voltagefiles);
Expand Down

0 comments on commit dd52fdf

Please sign in to comment.