Crates.io | derive-with |
lib.rs | derive-with |
version | |
source | src |
created_at | 2023-10-18 16:32:16.070477+00 |
updated_at | 2025-02-20 03:00:01.77225+00 |
description | `#[derive(With)]` generates with-constructor for each field in struct. |
homepage | |
repository | https://github.com/systemxlabs/derive-with |
max_upload_size | |
id | 1006947 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
#[derive(With)]
1.Generate with-constructor for each field on named struct.
#[derive(With)]
pub struct Foo {
pub a: i32,
pub b: String,
}
This will generate code
#[automatically_derived]
impl Foo {
pub fn with_a(self, a: impl Into<i32>) -> Self {
Self {
a: a.into(),
..self
}
}
pub fn with_b(self, b: impl Into<String>) -> Self {
Self {
b: b.into(),
..self
}
}
}
2.Generate with-constructor for each field on tuple struct.
#[derive(With)]
pub struct Bar (i32, String);
This will generate code
#[automatically_derived]
impl Bar {
pub fn with_0(mut self, field_0: impl Into<i32>) -> Self {
self.0 = field_0.into();
self
}
pub fn with_1(mut self, field_1: impl Into<String>) -> Self {
self.1 = field_1.into();
self
}
}
3.Generate with-constructor for specific fields on named struct.
#[derive(With)]
#[with(a)]
pub struct Foo {
pub a: i32,
pub b: String,
}
This will generate code
#[automatically_derived]
impl Foo {
pub fn with_a(self, a: impl Into<i32>) -> Self {
Self {
a: a.into(),
..self
}
}
}
4.Generate with-constructor for specific fields on tuple struct.
#[derive(With)]
#[with(1)]
pub struct Bar (i32, String);
This will generate code
#[automatically_derived]
impl Bar {
pub fn with_1(mut self, field_1: impl Into<String>) -> Self {
self.1 = field_1.into();
self
}
}
More examples can be found in tests