I have the following Parameter trait and struct definitions:
pub trait Parameter<T: Copy> {
fn get(&self) -> T;
}
pub struct ParameterAdd<T: Add> {
first_parameter: Box<dyn Parameter<T>>,
second_parameter: Box<dyn Parameter<T>>,
}
impl<T: Add<Output = T> + Copy> Parameter<T> for ParameterAdd<T> {
fn get(&self) -> T {
return self.first_parameter.get() + self.second_parameter.get();
}
}
impl<T: Add<Output = T> + Copy> Add for Box<dyn Parameter<T>> {
type Output = ParameterAdd<T>;
fn add(self, other: Self) -> ParameterAdd<T> {
ParameterAdd{
first_parameter: self,
second_parameter: other,
}
}
}
And the following implementation of the Parameter trait:
pub struct Constant<T: Copy> {
pub value: T,
}
impl<T: Copy> Parameter<T> for Constant<T> {
fn get(&self) -> T {
self.value
}
}
With this, I expect to be able to add together any objects which implement Parameter<T: Add<Output = T>>
however, trying to run the following test:
#[test]
fn parameter_addition() {
let c1: Box<Constant<i64>> = Box::new(Constant{value: 7});
let c2: Box<Constant<i64>> = Box::new(Constant{value: 3});
assert_eq!(10, (c1.add(c2)).get());
}
Gives the following error:
Compiling walnut v0.1.0 (/home/armand/Documents/art/walnut)
error[E0599]: no method named `add` found for struct `Box<Constant<i64>>` in the current scope
--> tests/parameter_test.rs:7:24
|
7 | assert_eq!(10, (c1.add(c2)).get());
| ^^^ method not found in `Box<Constant<i64>>`
In the first block of code, I believe I have implemented Add for Box<dyn Parameter<T: <Output = T>>
which should cover Box<Constant<i64>>
but, it evidently doesn't. How do I go about implementing the Addition operator on Parameter<T>
correctly?