erickt / rust-zmq

Rust zeromq bindings.
Apache License 2.0
886 stars 189 forks source link

Socketevent bitflags macro #353

Open tosti007 opened 1 year ago

tosti007 commented 1 year ago

This PR replaces the SocketEvent struct with a macro call to the (existing) bitflags crate. Additionally, monitor's event argument now takes a SocketEvent rather than the previous raw integer.

The docstrings come from the docs and the header file.

Notably, I removed the SocketEvent::ALL option in favor of SocketEvent::all(), due to two reasons: removing the bitflags all function is not possible and by using the all function, all rust-defined events are passed only, rather than 0xFFFF. The original behaviour is still possible by using: SocketEvent::from_bits_unchecked(0xFFFF).

An example of the change in this PR (with type annotation), before:

let events: u32 = SocketEvent::CONNECTED.to_raw()
        | SocketEvent::CONNECT_DELAYED.to_raw()
        | SocketEvent::CONNECT_RETRIED.to_raw()
        | SocketEvent::DISCONNECTED.to_raw();
socket.monitor(&endpoint, events as i32)?;

After:

let events: SocketEvent = SocketEvent::CONNECTED
        | SocketEvent::CONNECT_DELAYED
        | SocketEvent::CONNECT_RETRIED
        | SocketEvent::DISCONNECTED;
socket.monitor(&endpoint, events)?;