Skip to main content

Crate objs

Crate objs 

Source
Expand description

§objs

objs is a tiny macro library for creating named and unnamed (anonymous) struct-backed objects on the fly, without having to declare a struct up front.

It provides three macros:

  • obj! — build an anonymous, explicitly-typed object (great for one-off values, test fixtures, or small “record” types).
  • infer_obj! — build an anonymous object whose field types are inferred generically from the values you give it.
  • let_obj! — declare a named struct type and bind an instance of it to a local variable (with the same name as the type) in a single expression, optionally mutable, optionally with impl blocks.

All three macros expand to a real, ordinary Rust struct under the hood, so the objects you get back are fully-typed, zero-cost, and can implement traits like any other struct.

§Quick example

use objs::obj;

let point = obj! {
    x: i32 = 10,
    y: i32 = 20,
};

assert_eq!(point.x, 10);
assert_eq!(point.y, 20);

Macros§

infer_obj
Creates an anonymous object with field types inferred from their values.
let_obj
Declares a named struct type and binds an instance of it to a local variable in one step.
obj
Creates an anonymous, explicitly-typed object.