2

I need to send the same message object to other actors. It will be very costly to clone it each time. And it shouldn't actually be necessary. And I get the error as expected. "value moved here, in previous iteration of loop".

Passing parameters with & does not work. I couldn't do it with Weak / Arc reference. Actix does not seem to support it (Weak/Arc message).

How could it be the best solution for this case?

let myMessageObject: MyMessageType = MyMessageType {};
for user in self.users.iter() {
    user.Addr.do_send(myMessageObject);
}

impl Handler<MyMessageType> for User {
    type Result = ();
    fn handle(&mut self, setStartMessage: MyMessageType, ctx: &mut ws::WebsocketContext<Self>) {
    }
}
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Mesut
  • 21
  • 3
  • 1
    You can use inner pattern, https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=96e80519e9d03ed1cd85cd6dd6462884, constructing the message depends on your logic. – Ömer Erden Aug 13 '20 at 06:32
  • So we will have a cost. I was dreaming zero cost. Thanks.. – Mesut Aug 13 '20 at 10:49
  • The solution I've proposed is not different from Actix's `Arc` support(it is roughly same in a cost context). Not sure about the zero cost btw, but I don't think it can be done. I thought you were trying to avoid cloning large collections, maybe you should edit the question to emphasize zero cost problem. – Ömer Erden Aug 13 '20 at 11:49

0 Answers0