There's more...

When reading a malformed protocol, we reuse io::ErrorKind to show what exactly went wrong. In the recipes of Chapter 6, Handling Errors, you will learn how to provide your own error to better separate your areas of failure. If you want, you could read them now and then return here to improve our code.

The errors that need to be pushed into an own variant are:
  • InvalidStart
  • InvalidEndianness
  • UnexpectedEndOfPayload

Another improvement to the code would be to put all of our strings, namely MyProtocol, LE, and BE, into their own constants, as in the following line:

const PROTOCOL_START: &[u8] = b"MyProtocol";

The provided code in this recipe and some others doesn't use many constants, as they proved to be somewhat harder to understand in printed form. In real code bases, however, be sure to always put strings that you find yourself copy-pasting into own constants!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.35.122