erickt / rust-zmq

Rust zeromq bindings.
Apache License 2.0
887 stars 190 forks source link

zmq::Socket doesn't implement copy or clone trait #250

Closed ZePedroResende closed 5 years ago

ZePedroResende commented 5 years ago

I have a struct with a zmq socket inside another struct. i want to loop read from the socket but the zmq::socket doesnt implement the copy trait so i get a cannot move out of borrow content error. Is there a a way to handle this on the lib?

jean-airoldie commented 5 years ago

@ZePedroResende This seems to be an issue that you have with rust rather than the lib itself.

Your questions is not particularly clear, you should post a minimal reproducible example.

rotty commented 5 years ago

@ZePedroResende zmq::Socket cannot be Copy, please read up the documentation of the Copy trait to see why it is not possible. Regarding Clone, it's not immediately obvious, but here's the gist:

The socket types currently supported by zmq are all not thread-safe, but they can be passed from one thread to another (i.e., they are Send), but there can be only one thread at a time manipulating a given Socket. If Socket implemented Clone, you could have one thread send off a handle to the same underlying libzmq socket to another thread, allowing both threads accessing the same libzmq socket, violating libzmq's rules.

Rust is about ensuring thread-safety through the type system, so implementing Clone would throw the guarantees Rust gives you out of the window.

Now, regarding the actually thread-safe socket types (CLIENT, SERVER, DISH, RADIO, SCATTER, GATHER, according to the zmq_socket manpage), they could implement Clone, but, for that reason, they'd need to be a different type in Rust. I'm not yet sure on how to introduce support for these socket types, but it could probably be done without (much) code duplication by making use of generics.