xur_

> built-in type

bytes

methods of bytes values

in the browser

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).

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.

> methods

# bytes.decode() -> Result<str, Error>

The bytes decoded as UTF-8, or Err(bytes::Error::Utf8) when they aren't valid UTF-8.

# bytes.extend(other: bytes)

Appends the bytes of other. data.extend(data) doubles the buffer.

# bytes.find(needle: bytes) -> Option<i64>

The offset of the first occurrence of needle, or None. An empty needle is found at 0.

# bytes.get(index: i64) -> Option<i64>

The byte at index as an integer 0–255, or None when the index is negative or past the end.

# bytes.is_empty() -> bool

Whether the buffer has no bytes.

# bytes.len() -> i64

The number of bytes in the buffer.

# bytes.push(byte: i64)

Appends one byte. Only the low 8 bits of byte are kept, so 256 appends 0.

# bytes.read_f32_be(offset: usize) -> f32

Reads a 4-byte IEEE 754 float.

# bytes.read_f32_le(offset: usize) -> f32

Reads a 4-byte IEEE 754 float.

# bytes.read_f64_be(offset: usize) -> f64

Reads an 8-byte IEEE 754 float.

# bytes.read_f64_le(offset: usize) -> f64

Reads an 8-byte IEEE 754 float.

# bytes.read_i16_be(offset: usize) -> i16

Reads a 2-byte integer, unsigned or signed.

# bytes.read_i16_le(offset: usize) -> i16

Reads a 2-byte integer, unsigned or signed.

# bytes.read_i32_be(offset: usize) -> i32

Reads a 4-byte integer, unsigned or signed.

# bytes.read_i32_le(offset: usize) -> i32

Reads a 4-byte integer, unsigned or signed.

# bytes.read_i64_be(offset: usize) -> i64

Reads an 8-byte integer, unsigned or signed.

# bytes.read_i64_le(offset: usize) -> i64

Reads an 8-byte integer, unsigned or signed.

# bytes.read_i8(offset: usize) -> i8

Reads one byte, unsigned or signed.

# bytes.read_u16_be(offset: usize) -> u16

Reads a 2-byte integer, unsigned or signed.

# bytes.read_u16_le(offset: usize) -> u16

Reads a 2-byte integer, unsigned or signed.

# bytes.read_u32_be(offset: usize) -> u32

Reads a 4-byte integer, unsigned or signed.

# bytes.read_u32_le(offset: usize) -> u32

Reads a 4-byte integer, unsigned or signed.

# bytes.read_u64_be(offset: usize) -> u64

Reads an 8-byte integer, unsigned or signed.

# bytes.read_u64_le(offset: usize) -> u64

Reads an 8-byte integer, unsigned or signed.

# bytes.read_u8(offset: usize) -> u8

Reads one byte, unsigned or signed.

# bytes.slice(start: i64, end: i64) -> Result<bytes, Error>

A new buffer holding a copy of the bytes from start up to, not including, end. A negative index, start after end, or end past the length gives Err(bytes::Error::OutOfRange).

# bytes.starts_with(prefix: bytes) -> bool

Whether the buffer begins with prefix.

# bytes.to_hex() -> str

The bytes as lowercase hex, two digits per byte with no separators: "4d5a".

# bytes.to_list() -> list<i64>

A list of the bytes as integers 0–255.

# bytes.write_f32_be(offset: usize, value: f32)

Writes a 4-byte IEEE 754 float.

# bytes.write_f32_le(offset: usize, value: f32)

Writes a 4-byte IEEE 754 float.

# bytes.write_f64_be(offset: usize, value: f64)

Writes an 8-byte IEEE 754 float.

# bytes.write_f64_le(offset: usize, value: f64)

Writes an 8-byte IEEE 754 float.

# bytes.write_i16_be(offset: usize, value: i16)

Writes a 2-byte integer.

# bytes.write_i16_le(offset: usize, value: i16)

Writes a 2-byte integer.

# bytes.write_i32_be(offset: usize, value: i32)

Writes a 4-byte integer.

# bytes.write_i32_le(offset: usize, value: i32)

Writes a 4-byte integer.

# bytes.write_i64_be(offset: usize, value: i64)

Writes an 8-byte integer.

# bytes.write_i64_le(offset: usize, value: i64)

Writes an 8-byte integer.

# bytes.write_i8(offset: usize, value: i8)

Writes one byte: 0–255 for write_u8, -128–127 for write_i8.

# bytes.write_u16_be(offset: usize, value: u16)

Writes a 2-byte integer.

# bytes.write_u16_le(offset: usize, value: u16)

Writes a 2-byte integer.

# bytes.write_u32_be(offset: usize, value: u32)

Writes a 4-byte integer.

# bytes.write_u32_le(offset: usize, value: u32)

Writes a 4-byte integer.

# bytes.write_u64_be(offset: usize, value: u64)

Writes an 8-byte integer.

# bytes.write_u64_le(offset: usize, value: u64)

Writes an 8-byte integer.

# bytes.write_u8(offset: usize, value: u8)

Writes one byte: 0–255 for write_u8, -128–127 for write_i8.

> 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));
}