Lifetime conflicting requirements

As long as it’s about lifetimes only, they can be replaced with ordinary traits and HRTBs easily. All you’ll need is

trait HasMessageType<'a> {
    type Message;
}

perhaps throw in an extra parameter in the mix to make Message only defined when Self: 'a:

trait HasMessageType<'a, _Outlives = &'a Self> {
    type Message;
}

Add it as a supertrait

trait EventSource: for<'a> HasMessageType<'a>

and perhaps use a convenience type synonym

type Message<'a, This> = <This as HasMessageType<'a>>::Message;

and use it


trait EventSource: for<'a> HasMessageType<'a> {
    fn recv(&self) -> Message<'_, Self>;

    fn msg_to_event(msg: &Message<'_, Self>) -> Event;

    fn post_hook(&self, msg: &Message<'_, Self>);

    fn run(&self) {
        let msg = self.recv();
        let event = Self::msg_to_event(&msg);
        println!("got event {:?}", event);
        self.post_hook(&msg);
    }
}

Here’s the full code in the playground.

10 Likes