0

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

ewoolsey
  • 91
  • 1
  • 7
  • Where are `foo::CreateMsg`, etc declared? Please provide those as a complete example. – PitaJ Oct 04 '22 at 17:57
  • How are you using `CreateMsg` and `DeleteMsg`? They are part of the trait's type or *"signature"* if you will. If you don't use them in method signatures, then you can avoid having them in your trait, but if you do then you can't unify them. You'd either need a trait that omits those portions or abstracts over their differences. – kmdreko Oct 04 '22 at 18:08
  • @kmdreko Yes all MyTrait methods will depend on the associated types, they must be included. – ewoolsey Oct 04 '22 at 18:39
  • How do you plan to pass a `MyTrait::CreateMsg` to a trait method of `dyn ContractTrait` if you don't know what type it is? Please include how you want to use `CreateMsg` and `DeleteMsg`. You can't use a dynamic trait object without specifying the associated types, so you'll need some kind of workaround or alternative design, which will be easier to apply and explain if there's an example. – kmdreko Oct 04 '22 at 18:54
  • @kmdreko I assume I'm doing something in the wrong way. will edit in 1 sec – ewoolsey Oct 04 '22 at 19:13

0 Answers0