Dynamically-dispatched type parameters

if self.is_error {
     // below, CoRunnerBridge's types A, T, and C are constant w.r.t to this closure's container, but the 4th is unknown except for the fact that it implements Packet
     let actor = CoRunnerBridge::<A,T,C, dyn Packet>::new(self.get_and_increment_global_counter(), 
     self.vehicle_id, packet, ctx.address(), self.global_state_ptr.clone(), true);
     let _ = actor.start();
} else {
     // below, the type parameters in CoRunnerBridge are implicit within this closure's container
      let actor = CoRunnerBridge::new(self.get_and_increment_global_counter(), self.vehicle_id, packet, ctx.address(), self.global_state_ptr.clone(), false);
      let _ = actor.start();
}

I have an inbound packet type that can be one of many possible types. As seen on line 4, I customly set the 4th type parameter to by any type of Packet. Is this acceptable? If not, what is the next most idiomatic way to handle the situation?

If you want type inference to fill out the missing type, you can just use an underscore:

CoRunnerBridge::<A, T, C, _>::new(...

Otherwise if you truly want dyn Packet, i.e. the trait object, as the type, then it's fine to explicitly specify that.

1 Like