datastruct 0.1.1

A pure-data structure builder.
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented1 out of 1 items with examples
  • Size
  • Source code size: 24.87 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.56 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Embers-of-the-Fire/datastruct-rs
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Embers-of-the-Fire

DataStruct.rs

This is a procedural macro library to automatically generate duplicate code for pure data structures.

What can this lib do?

The library provides a derive macro to automatically implement "plain methods" for data structures.

Currently Available:

  • Default: Standard Default, lib-specific DataStruct::data_default and constant default ConstDataStruct::DEFAULT.
  • Debug: Manual Debug filter.
  • Comparison: Standard Eq, PartialEq, Ord, PartialOrd.
  • Operations: Standard Add(Assign), Sub(Assign), Mul(Assign), Div(Assign).

Unlike standard derive macros, the DataStruct macro accepts user-defined behaviors without writing implementation code.

Quick Start

For full documentation, read it here.

Let's start with this example structure:

struct Person {
    age: u8,
    name: String,
    private_key: u32,
}

First, add datastruct to your dependencies. The core entry point of the library is DataStruct macro.

use datastruct::DataStruct;
#[derive(DataStruct)]
#[dstruct(debug)]
struct Person {
    age: u8,
    name: String,
    #[dfield(no_debug)]
    private_key: u32,
}

The #[dstruct(xxx)] is used to configure the basic options of the code-generator. In this example, debug means that the Debug trait will be implemented.

The #[dfield(xxx)] is used to configure field-specific options of the code-generator. In this example, no_debug means that this field will not be included in the debug output.

use datastruct::DataStruct;
#[derive(DataStruct)]
#[dstruct(debug)]
struct Person {
    age: u8,
    name: String,
    #[dfield(no_debug)]
    private_key: u32,
}

let person = Person { age: 22, name: "James".to_string(), private_key: 42 };
println!("{:#?}", person);
// Output:
// Person {
//     age: 22,
//     name: "James",
// }

Limitations

Currently, the library can only generate code for typical structure, and tuple structure is not supported.

Besides, most IDE-support cannot offer full completion for macro-generated code, compared with manual implementation.