Hi,
So I am implementing some network code to handle buffering of received data. Basically, I'll need to buffer either data packets or some chat messages. The details behind those don't really matter I suppose. I wrote a buffer_item(t: T)
function with the hopes that it could handle either a Packet
or a ChatMessage
, which it actually does. Awesome.
The only problem is I have two full implementations of the same code, the only difference is that they are two distinct impls
. Stated differently, I have a full implementation of buffer_item(t: T)
where T: Packet
for an implementation of RXQueue<Packet>
, and another one of buffer_item(t: T)
where T: ChatMessage
for an implementation of RXQueue<ChatMessage>
. The bodies of the functions is essentially the exact same code. The only difference lies in the type.
Link to playground explaining (hopefully) what I mean. Note, this won't compile since I omitted quite a bit for readability.
What I am trying to do is to only have one single instance of that function buffer_item(t: T)
in impl NetworkQueue<T> for RXQueue<T>
. The type T
handle the type-specific details via Sequencer
, but other than that it's all the same. That's what you see the Sequencer
trait in the link I pasted above.
Is this possible at all? Ideally I'd like to avoid two identical implementations because that's a headache to maintain and can leak bugs easily. I know this may have been asked before in this thread so forgive me if this is a duplicate.
Any help would be appreciated. Thanks.
Edit: Probably should mention I tried fiddling with impl<T> NetworkQueue<T> for RXQueue<T>
but couldn't get it working. Wish I had saved the code but I never committed it anywhere.