> module
std::bytes
byte buffer operations
std::bytes creates byte buffers, and byte values have methods for reading and writing integers and floats at byte offsets in either byte order. A bytes value is shared: methods that change it change it in place, and every variable holding it sees the change.
Methods that read or write at an offset raise a runtime error when the range is outside the buffer. Methods that return Result fail with a bytes::Error value instead: OutOfRange (an offset or range is out of bounds), Utf8 (the bytes are not valid UTF-8) or BadHex (a hex string is malformed).
> functions
Parses pairs of hex digits, ignoring whitespace: "4d 5a" gives the two bytes 0x4d 0x5a. An odd number of digits or a character that isn't a hex digit gives Err(bytes::Error::BadHex).
A new buffer from a list of integers, each 0–255, or a copy of another buffer. A value outside 0–255 raises an error.
> types
A recoverable bytes error. Match it as `Err(bytes::Error::OutOfRange)` etc.
| Error::OutOfRange | An offset or range was out of bounds. |
| Error::Utf8 | The bytes are not valid UTF-8 (`decode`). |
| Error::BadHex | A hex string was malformed (`from_hex`). |
> bytes methods
Each read takes a byte offset and returns the value stored there with the width and signedness its name gives. Each write stores value at offset in place. The _le methods use little-endian byte order and the _be methods big-endian; the one-byte methods have no suffix.
They raise a runtime error when the value's bytes don't fit inside the buffer, when offset is negative, when an integer value doesn't fit the width (write_u8(0, 256)), or when an integer width is given a float or a float width an integer.
> examples
use std::bytes;
use std::io;
fn main() {
let buf = bytes::zeroed(8);
buf.write_u32_le(0, 0x00905a4d);
io::println("MZ header: {}", buf.to_hex());
io::println("u32 at 0: {}", buf.read_u32_le(0));
}