xtruct 0.1.0

Anonymous structs made simple
Documentation
  • Coverage
  • 0%
    0 out of 1 items documented0 out of 0 items with examples
  • Size
  • Source code size: 10.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 242.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • talshorer/xtruct
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • talshorer

xtruct

Crates.io Documentation License: MIT

An anonymous struct literal macro for Rust. xtruct allows you to construct lightweight, ad-hoc struct types on the fly without declaring explicit struct types beforehand.

Features

  • Zero boilerplate: Create anonymous structs inline with simple key-value syntax.
  • Field shorthand: Infer fields directly from local variables in scope (e.g., xtruct! { name, count }).
  • Type inference: Automatically infers field types based on the passed expressions.

Usage Examples

Construction

use xtruct::xtruct;

let user = xtruct! {
    id: 1,
    is_admin: true,
    avatar_url: Some("https://example.com/avatar.png"),
};

assert_eq!(user.id, 1);
assert_eq!(user.is_admin, true);
assert_eq!(user.avatar_url, Some("https://example.com/avatar.png"));

Variable Field Shorthand

Like standard Rust struct initialization, you can use local variables directly without repeating the field name:

use xtruct::xtruct;

let retry_count = 4usize;

let options = xtruct! {
    timeout_ms: 2000,
    retry_count,
};

assert_eq!(options.timeout_ms, 2000);
assert_eq!(options.retry_count, 4);

Scoped Anonymous Structs

Construct anonymous structs inline within local scopes or expressions:

use xtruct::xtruct;

let payload = {
    let status_code = 200;
    let ok = true;
    xtruct! { status_code, ok }
};

assert_eq!(payload.status_code, 200);
assert_eq!(payload.ok, true);

Combining Expressions and Local Variables

Mix complex expressions and local variable shorthands in a single struct initialization:

use xtruct::xtruct;

let user_id = 101;
let is_active = true;

let session = xtruct! {
    user_id,
    auth_token: format!("token_{}", user_id),
    is_active,
    ttl_seconds: 3600,
};

assert_eq!(session.user_id, 101);
assert_eq!(session.auth_token, "token_101");

Nested Anonymous Structs

Instantiate nested anonymous structures by nesting xtruct! invocations:

use xtruct::xtruct;

let config = xtruct! {
    environment: "production",
    database: xtruct! {
        host: "localhost",
        port: 5432,
    },
};

assert_eq!(config.database.host, "localhost");
assert_eq!(config.database.port, 5432);

Installation

Add xtruct to your Cargo.toml:

[dependencies]
xtruct = "0.1.0"

Or run:

cargo add xtruct

License

This project is licensed under the MIT License.