I think I am running into two issues understanding impl Trait to a struct and passing a function parameter with certain Trait bounds.
I have several structs that derives Serialize, Debug, Default. I also created a Trait TypeOf to determine the type of struct:
#[derive(Serialize, Debug, Default)]
pub struct TriggerOutput {
id: String,
name: String,
data_type: String,
}
impl TypeOf for TriggerOutput {
fn type_of(&self) -> &'static str {
"trigger"
}
}
Now, I am pushing data of type TriggerOutput to a Vec, and then passing the Vec to a function:
pub fn to_csv<S: TypeOf + Serialize + std::fmt::Debug>(&self, game_object: S) {
let mut path = String::new();
path.push_str(&self.iop());
path.push_str("_");
path.push_str(game_object.type_of());
path.push_str(".csv");
//let mut csv_writer = Writer::from_path(Path::new(&path)).unwrap();
println!("PATH = {:?}\nOBJECT = {:?}", path, &game_object);
}
The compiler will recognize and pass the TriggerOutput struct to pass to the function. But, it will not allow a Vec to pass. It wants me to impl TypeOf for Vec. I'm a little confused. Because TriggerOutput is the type and implements TypeOf, shouldn't that implementation be passed to the Vec also?
When I impl TypeOf for Vec and try to iterate through the elements of the Vec, it says 'S' is not an iterator. Yet, 'S' is a Vec. Shouldn't a Vec automatically impl IntoIterator?
So, I add the ' + std::iter::Iterator' trait bound to the function signature:
pub fn to_csv<S: TypeOf + Serialize + std::fmt::Debug + std::iter::Iterator>(
&self,
game_object: S,
)
The compiler errors for
<S as Iterator>::Item` doesn't implement `Debug
Now, I am totally lost and confused....my function has a trait bound for Debug and my TriggerOutput derives Debug. Isn't the item of Vec, a Trigger Output? And, TriggerOutput derives Debug......Totally confused here.