Preferred way to receive events?

52 points MrPepperioni

I'm in the process of writing a library to perform certain network operations, and it needs to be able to provide a way for users to receive events (i.e. for them to be notified when data is received from a socket). I've done a good bit of Rust programming, but I'm still unsure what would be the preferred way to do this. From what I can tell, there are three options:

I can have users

  1. register callbacks that are called when events are emitted,
  2. implement a trait containing methods that are called when events are emitted, or
  3. receive events via a channel/stream.

I'm very much leaning away from option 1, since it doesn't really fit Rust's design like it does for other languages (i.e. JavaScript). So between options 2 and 3, what is the preferred method? Or is there a reason I've overlooked that makes option 1 the preferred route to go? Or is there some option 4 that I'm not seeing?

Any and all opinions welcome.