【发布时间】:2021-03-15 15:49:59
【问题描述】:
我有一个消息传递系统,想通用地解决所有问题。 消息可以发送给实体,实体可以处理消息。
// There are many messages that implement this trait
trait Message {
type Response;
}
// Messages can be sent to 'entities'
trait Entity {
type Error;
}
// Entities can implement handlers for specific messages
trait MessageHandler<M: Message>: Entity {
fn handle(
&mut self,
message: M,
) -> Result<M::Response, Self::Error>;
}
这将像这样实现:
struct SimpleEntity;
impl Entity for SimpleEntity {
type Error = ();
}
struct SimpleMessage;
impl Message for SimpleMessage {
type Response = ();
}
impl MessageHandler<SimpleMessage> for SimpleEntity {
fn handle(
&mut self,
message: SimpleMessage,
) -> Result<(), ()> {
Ok(())
}
}
所有实体都存储在一个系统中。系统只能存储一种类型的实体。对于该类型具有的每个消息处理程序,都应该有一个 send_message 函数来一般地接收消息。
我想它可能看起来像这样:
// A message system for one type of entity. This is an example. Normally there's all kinds of async multithreaded stuff here
struct MessageSystem<E: Entity> {
handlers: Vec<E>,
}
// For every message handler, we want to implement the send_message function
impl<M: Message, MH: MessageHandler<M>> MessageSystem<MH> {
pub fn send_message(&mut self, entity_id: (), message: M) -> Result<M::Response, MH::Error> {
unimplemented!();
}
}
然后可以这样使用:
// Example usage
fn main() {
let mut system = MessageSystem { handlers: vec![SimpleEntity] };
system.send_message((), SimpleMessage).unwrap();
}
但是,这会在 send_message 函数的 impl 块中产生编译错误:
error[E0207]: the type parameter `M` is not constrained by the impl trait, self type, or predicates
--> src/lib.rs:25:6
|
25 | impl<M: Message, MH: MessageHandler<M>> MessageSystem<MH> {
| ^ unconstrained type parameter
error: aborting due to previous error
For more information about this error, try `rustc --explain E0207`.
我怎样才能做到这一点?
目标是拥有这些不同的消息结构,让它们由实体可以实现的处理程序处理,并通过系统结构将消息发送到实体。
一个明显的事情是让消息成为MessageHandler trait 中的关联类型,但是你不能为一个实体实现它的多个版本。
【问题讨论】:
标签: rust