xur_

> module

std::reflect

runtime reflection and binary layout

use std::reflect; in the browser

std::reflect inspects values at runtime: it lists fields and methods, reads and writes fields by name, and calls methods by name. It works on Xur structs and enum variants, records, tuples, native objects and built-in values.

Its Layout type says where a struct's fields sit in memory, for reading and writing binary data. std::reflect::func inspects functions.

> functions

# call(value: any, name: str, args: list<any>) -> any

Calls the method name on value with args and returns its result.

# fields(value: any) -> list<str>

The names of value's fields: numbers ("0", "1", ...) for tuples and tuple structs, and an empty list for values without fields.

# get(value: any, name: str) -> any

The field called name. A missing field is a runtime error.

# has_field(value: any, name: str) -> bool

Whether value has a field called name.

# has_method(value: any, name: str) -> bool

Whether methods would list name.

# layout() -> Layout

The layout of the type argument, called as reflect::layout::<T>(). T must be a struct.

# layout_of(value: any) -> Layout

The layout of a struct value's type.

# methods(value: any) -> list<str>

The names of the methods of value's type. The to_bytes and byte_size methods every struct has are not listed.

# set(value: T, name: str, new_value: any) -> T

Returns a copy of value with one field changed; value itself is unchanged.

# try_get(value: any, name: str) -> Option<any>

Like get, but None if there is no such field.

# type_name(value: any) -> str

The name of value's type, such as "Point", "Shape::Circle" or "list".

# variant(value: any) -> Option<str>

The variant name of an enum value, such as "Circle" for Shape::Circle(1.0); None for anything that isn't an enum variant.

> types

┌ type Layout

Building methods return a new layout; the original is unchanged.

A struct's layout. Layouts never change once built, so they are shared.

# Layout::new(name: str) -> Layout

An empty layout called name, to add fields to.

# layout.align() -> usize

Its size and alignment in bytes.

# layout.aligned(align: usize) -> Layout

Sets a minimum alignment, which must be a power of two.

# layout.big_endian() -> Layout

Sets the byte order of integer and float fields (little-endian by default).

# layout.bytes(name: str, len: usize) -> Layout

Adds a field of len raw bytes.

# layout.field(name: str, ty: type | str) -> Layout

Adds a field of a primitive type, given as a type (u16) or a name ("u16"): u8–u64, i8–i64, usize, isize, f32, f64 or bool. A duplicate name is a runtime error.

# layout.field_at(name: str, ty: type | str, offset: usize) -> Layout

Like field, but at an explicit byte offset.

# layout.fields() -> list<record>

One record per field, with name, ty (a type), offset and size.

# layout.from_bytes(data: bytes) -> Result<any, str>

Like read at offset 0.

# layout.is_big_endian() -> bool

Whether fields are big-endian.

# layout.little_endian() -> Layout

Sets the byte order of integer and float fields (little-endian by default).

# layout.name() -> str

The layout's name.

# layout.nested(name: str, inner: Layout) -> Layout

Adds a field laid out by inner.

# layout.offset_of(name: str) -> i64

The byte offset of the field name; a missing field is a runtime error.

# layout.packed() -> Layout

No padding between fields, alignment 1.

# layout.read(data: bytes, offset: usize) -> Result<any, str>

Reads a value from data at offset. Returns Err if there are too few bytes.

# layout.size() -> usize

Its size and alignment in bytes.

# layout.to_bytes(value: any) -> bytes

Encodes value as a new buffer of size() bytes.

# layout.to_string() -> str

A one-line description, such as Point (size 16, align 8) { x: i64 @ 0, y: i64 @ 8 }.

# layout.write(data: bytes, offset: usize, value: any)

Writes value's fields into data at offset, in place. value can be a struct or record with the layout's fields. Too few bytes, a missing field or a value that doesn't fit its field is a runtime error, and then data is unchanged.

> layouts

A layout comes from a struct's type and attributes, or is built at runtime. A layout built at runtime reads as a record; one from a struct reads as that struct.

> struct attributes

#[repr(C)]             // C layout (default)
#[repr(packed)]        // no padding
#[repr(align(8))]      // minimum alignment
#[endian(Big)]         // byte order (Little by default)
#[field(offset = 8)]   // explicit field offset
#[field(size = 16)]    // fixed byte count (for bytes fields)

> examples

use std::reflect;
use std::reflect::func;
use std::io;

struct Point {
    x: i64
    y: i64
}

fn add(a: i64, b: i64) -> i64 {
    a + b
}

fn main() -> Result<()> {
    let p = Point { x: 1, y: 2 };
    io::println("fields: {}", reflect::fields(p));   // ["x", "y"]
    io::println("x = {}", reflect::get(p, "x"));     // 1

    let layout = reflect::layout::<Point>();
    io::println("{}", layout.to_string());
    let data = layout.to_bytes(p);
    let p2 = layout.from_bytes(data)?;
    io::println("round-trip: {}", p2);

    io::println("{}({})", func::name(add), func::param_names(add));
    Ok(())
}