Hi,
For my project I am trying to create a newtypes like this -
pub struct Length (u64);
pub struct SeqNum (u32);
pub struct TagNum (u32);
pub struct Amt (f64);
pub struct Qty (f64);
I have defined traits for getter & setter -
pub trait FixType {
type output;
fn get_val(&self) -> Self::output;
fn set_val<T>(&mut self, val: T);
}
pub trait Setter<Other=Self> {
fn set(&mut self, other: Other);
}
pub trait Getter {
type output;
fn get(&self) -> Self::output;
}
Sample implementation is as follow -
impl Getter for SeqNum {
type output = u32;
fn get(&self) -> u32 {
self.0
}
}
impl Setter for SeqNum {
fn set(&mut self, other: Int) {
self.0 = other.0;
}
}
impl Setter<u16> for SeqNum {
fn set(&mut self, other: u16) {
self.0 = other as u32;
}
}
impl Setter<u64> for SeqNum {
fn set(&mut self, other: u64) {
self.0 = other as u32;
}
}
I want to give default implement FixType trait for any types which implement Setter and Getter like below -
impl<I: Getter + Setter> FixType for I {
type output = I::output; // I want to write something which say that the output of this return should be the output return by the Getter implementation.
fn get_val(&self) -> Self::output {
self.get()
}
fn set_val<T>(&mut self, val: T ) {
self.set(val);
}
}
I want to have FixType implementation because later I want to use hashmaps or Vec which are generic over any type which is FixType.
I get below error in set_val -
"mismatched types\n\nexpected type parameter, found a different type parameter\n\nnote: expected type I
\nfound type T
",
Also, not sure if my get_val and output parameters are correct. Could someone please in understanding how to model this better?
Regards,
Gaurav