I have a program where I need to implement the same logic to plot a struct of data. Something like this:
struct TimeData {
x_data: Vec<i64>,
y_data: Vec<f32>,
}
where the x-axis here represents time stamps, and also I need to plot fourier frequency data, that has a similar struct:
struct FFTData {
x_data: Vec<f32>,
y_data: Vec<f32>,
}
There's more information than these simple xy data, so I have a "Plottable" trait that represents things that can be plotted:
trait Plottable<XAxisType: num_traits::Num> {
fn get_first_x_point(&self) -> Option<&XAxisType>;
fn get_last_x_point(&self) -> Option<&XAxisType>;
fn get_x_axis<'a>(&'a self) -> &'a Vec<XAxisType>;
fn get_y_axis<'a>(&'a self) -> &'a Vec<DataType>;
}
The plotting function, that takes a generic Vec of plottables shouldn't care whether the x-axis is integer or f32, but rust can't let it go. For example, for a vector of plots, I need to determine the min and max for a collection of plots. So I run this:
let min = plots
.iter()
.filter(|el| el.get_first_x_point().is_some())
.map(|el| {
el.get_first_x_point()
.expect("We filtered for first points")
})
.min()
.ok_or_else(|| {
Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Failed to get min",
))
})?;
let range_margin = 0.02f32;
And then... I do the sin of trying to do this:
let min_m = (1f32 - range_margin) * min;
and everything falls apart. It's like there's no way to make rust understand that I want to do all the casts necessary to make this happen. What's the right way to do this?
My question:
How can I do arithmetic operations on both i64 and f32 with no issues.
Is there a better rustacean way of doing this?
Thank you. I really appreciate your help.