Hi everyone, I'm looking for a way to compare clones of mpsc::Sender<T>
s with each other.
I'm using channels to implement something of a subscription model where clients send a clone of their Sender<T>
down a separate channel, When a message needs to be sent, each client's Sender<T>
can be invoked individually, repeatedly.
My issue now is though, that when clients which to unsubscribe, they send the request, along with a copy of their Sender<T>
, which I now need to look up in my list of Sender<T>
s and remove it.
My current solution (which doesn't work, hence my question) is to use std::ptr::eq()
.
fn unsubscribe(&mut self, sender: mpsc::Sender<Command>) {
if let Some(index) = self.subscribers
.iter()
.position(|subscriber| std::ptr::eq(subscriber, &sender)) {
self.subscribers.remove(index);
}
}
After much hair-out-pulling I realised that this can't work because by nature, Sender<T>
are passed by value - they get copied!!!
So I did some googling, and Rust apparently has no standard ways to binary compare values, unless the value implements Eq
or PartialEq
.
So what might be my best approach here?
Thanks