| Crates.io | dataclasses |
| lib.rs | dataclasses |
| version | 0.1.0 |
| created_at | 2025-11-14 22:08:34.060581+00 |
| updated_at | 2025-11-14 22:08:34.060581+00 |
| description | A small, pragmatic procedural macro providing a Dataclass-like derive for Rust |
| homepage | https://github.com/lizelive/dataclasses |
| repository | https://github.com/lizelive/dataclasses |
| max_upload_size | |
| id | 1933544 |
| size | 29,866 |
A small, pragmatic #[derive(Dataclass)] procedural macro inspired by Python's dataclasses module. It generates common boilerplate for struct types, including constructors, Debug, Clone, PartialEq, Eq, and optional Default when all fields provide defaults.
This crate is intended for developers who like the ergonomics of Python dataclasses and want a lightweight, expressive Rust macro that handles common patterns. It's not an exhaustive reimplementation of Python dataclasses, but a carefully chosen set of features that map well to Rust idioms.
pub fn new(...) -> Self for named fields, with required fields first and fields with defaults omitted from the constructor#[dataclass(default)] - uses Default::default() for that field#[dataclass(default = "Expr")] - uses the provided Rust expression string as the default (e.g., #[dataclass(default = "Vec::new()")])Clone, Debug, PartialEq and Eq automatically for the structDefault when every field has a default providedAdd this crate as a dependency in your Cargo.toml and enable the proc-macro server if needed. This crate is a proc-macro, so import the derive attribute method like other derive macros.
Example usage:
use dataclasses::Dataclass;
#[derive(Dataclass)]
struct Person {
name: String,
age: i32,
#[dataclass(default)]
nickname: Option<String>,
#[dataclass(default = "Vec::new()")]
tags: Vec<String>,
}
let person = Person::new("Alice".to_string(), 30);
assert_eq!(person.nickname, None);
assert_eq!(person.tags.len(), 0);
When all fields provide defaults, the crate generates a Default impl too:
#[derive(Dataclass)]
struct Defaults {
#[dataclass(default = "1")]
a: i32,
#[dataclass(default)]
b: String,
}
let d: Defaults = Default::default();
assert_eq!(d.a, 1);
#[dataclass(default)] - Use Default::default() for this field#[dataclass(default = "Expr")] - Use the provided Expr (a string literal) as the defaultNotes:
default if the expression contains commas or spaces - the parser expects a single string for the expression.eq=false, skip for Debug/PartialEq, init=False, and repr are not yet availablepost_init hooks and frozen immutability are not implementeddefault = "Expr" currently requires a string literal for the expression; future work could parse expressions directlyIf you want additional features, please open an issue or a PR.
This crate is dual-licensed under MIT or Apache-2.0. You can select either license when using or distributing this crate.
Files: LICENSE-MIT and LICENSE-APACHE are included in this repository.