xur_

> built-in type

struct

methods of struct values

in the browser

std::mem provides size and alignment queries, binary conversion for structs with a layout (see xur.std.reflect(7)), raw reads and writes of process memory, byte pattern scanning, and memory page allocation for assembling and executing machine code at runtime.

The raw memory functions trust their arguments: a bad address or size crashes the process instead of raising an error.

Every struct and enum value also has these methods. The layout used by to_bytes and byte_size is the type's layout from xur.std.reflect(7): fields in order with C alignment and padding unless attributes say otherwise. A field whose type has no layout, such as a str, raises a runtime error.

> methods

# struct.byte_size() -> i64

The size in bytes of the value's layout, padding included; the length to_bytes returns.

# struct.to_bytes() -> bytes

The value written into a new buffer using its layout: Header { magic: 0x5a4d, version: 1 }.to_bytes(), with magic: u32, version: u16, is the 8 bytes 4d5a000001000000.

# struct.to_str() -> str

The name of the value's struct, or of its enum variant without the enum name: Color::Red.to_str() is "Red".

> examples

use std::assert;
use std::io;
use std::mem;

#[repr(packed)]
#[endian(Big)]
struct Header {
    magic: u16
    version: u8
    flags: u8
    length: u32
}

fn main() {
    io::println("size: {}", mem::size_of::<Header>());   // 8
    let h = Header { magic: 0xCAFE, version: 1, flags: 0x80, length: 256 };
    let data = mem::to_bytes(h);
    io::println("bytes: {}", data.to_hex());
    let back = mem::from_bytes::<Header>(data) as Header;
    assert::is_eq(h, back);
}