Doing asynnchronous serial

You're on the right track! I'm assuming that you're working with serial as a custom file descriptor, and you're wondering how to get that custom file descriptor (set to async mode) hooked up to futures and everything. To do that, you'd follow steps that look like:

  1. Verify your file descriptor works with epoll (assuming you're on Linux)
  2. Create a type and implement Evented for it. This will likely use EventedFd internally, probably like so. Remember that if you're passing ownership of the file descriptor to this type implementing Evented that you'll want to implement a destructor that closes it.
  3. When working with tokio-core, you'll want to then pass this type that implements Evented to the PollEvented type. This is what hooks up your file descriptor to the tokio-core event loop.
  4. If you interact with the file descriptor via read and write then you're done! The Read and Write trait implementations on PollEvented are all you'll need.
  5. If you need functionality other than read and write, then you'll need to implement your own methods that call poll_read, poll_write, need_read, and need_write manually (all methods on PollEvented). You can see an example of this in UdpSocket::recv_from.

Hope that helps!

8 Likes