Here is my setup
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub struct Foo {}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub struct Bar {}
pub trait MyTrait {
type CreateMsg;
type DeleteMsg;
}
impl MyTrait for Foo {
type CreateMsg = foo::CreateMsg;
type DeleteMsg = foo::DeleteMsg;
}
impl MyTrait for Bar {
type CreateMsg = bar::CreateMsg;
type DeleteMsg = bar::DeleteMsg;
}
pub fn contains_create_msg(my_trait: impl MyTrait, string: String) -> bool {
my_trait::CreateMsg::contains(string)
}
lazy_static! {
pub static ref MY_TRAIT_OBJECTS:[Box<dyn ContractTrait + Sync>; 2] = [
Box::new(Foo::default()),
Box::new(Bar::default()),
];
}
In foo.rs:
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub enum CreateMsg {
CreateThis { a_field: String },
CreateThat { a_field: String }
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub enum DeleteMsg {
DeleteThis { a_field: String },
DeleteThat { a_field: String }
}
In bar.rs:
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub enum CreateMsg {
CreateOne { a_different_field: String },
CreateTwo { a_different_field: String }
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub enum DeleteMsg {
DeleteOne { a_different_field: String },
DeleteTwo { a_different_field: String }
}
I get an error, "the value of the associated types must be specified". How can I pass a list of MyTrait objects to other places in my program? My intention is to iterate of these My Traits, and perform different functions using the types that they are associated to. Here is an example of something I would like to do:
let msg = "hello".to_string();
for object in MY_TRAIT_OBJECTS {
println!("{}", object.contains_create_msg(msg.clone()));
}
Sorry if there are syntax errors in there, wrote without an editor