1.) I have made a big knot in my code it seems. I defined my own structs e.g.
struct State { // some float values }
and require them be multiplied by f64 + Complex64 and added together member-wise. Now I tried to abstract away the f64 and Complex64 into a trait called "Weight" and I have two structs that need to implement them:
struct WeightReal {
strength: f64,
}
struct WeightComplex {
strength: num_complex::Complex<f64>,
}
Now it's more complicated as I need custom multiplication for these Weights with my struct "State" AND also with f64 itself (because I do other things as well). So I need "Weight" x State and "Weight" x f64 for both possible weight-types. Do I have to define all of these multiplications myself now? I used the derive_more-crate in the past, but I think now it's at its limits. Or I fundamentally misunderstood something here. Another question is: Do I need to define a struct here? I tried type-aliases before but I think there was an error, because I couldn't define custom multiplication on type aliases (at least it seemed so to me). It could've just been me doing it incorrectly.
- The Rust way of defining multiplication / overloading of the "*"-operator somehow flies right over my head. With "cargo expand" I looked at a multiplication derived through the derive_more-crate:
impl<__RhsT: ::core::marker::Copy> ::core::ops::Mul<__RhsT> for State
where
f64: ::core::ops::Mul<__RhsT, Output = f64>,
{
type Output = State;
#[inline]
fn mul(self, rhs: __RhsT) -> State {
State {
value1: <f64 as ::core::ops::Mul<__RhsT>>::mul(self.value1, rhs),
value2: <f64 as ::core::ops::Mul<__RhsT>>::mul(self.value2, rhs),
}
}
}
If someone could explain a few of the parts here: what does the "<f64 as ::core.... "-part mean? I understand that "__RhsT" means "Right-Hand-Side-Type", but I don't understand why it is still generic, because in this example shouldn't it be specifically f64? The third line is also puzzling me, why is it necessary?
I am really confused. The rust docs regarding multiplication are also unclear to me as they seem to be abstracted away in some macro.