Thanks to visit codestin.com
Credit goes to lib.rs

#struct-fields #generator #derive #generate #with-constructor

macro derive-with

#[derive(With)] generates with-constructor for each field in struct

8 releases (breaking)

Uses new Rust 2024

0.7.0 Nov 3, 2025
0.6.1 May 27, 2025
0.6.0 Feb 20, 2025
0.5.0 Jan 20, 2024
0.1.0 Oct 18, 2023

#534 in Procedural macros

Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App

260 downloads per month
Used in 23 crates (10 directly)

MIT license

22KB
337 lines

A custom derive implementation for #[derive(With)]

License Crates.io

Get started

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
    }
}

5.Generate with-constructor for generic fields

#[derive(With, Default)]
pub struct Foo<T: Default, Z: Default>
where
    Z: std::fmt::Debug,
{
    pub a: T,
    pub b: Z,
}

This will generate code

#[automatically_derived]
impl<T: Default, Z: Default> Foo<T, Z>
where
    Z: std::fmt::Debug,
{
    pub fn with_a<WT: Default>(self, a: WT) -> Foo<WT, Z> {
        Foo { a, b: self.b }
    }
    pub fn with_b<WZ: Default>(self, b: WZ) -> Foo<T, WZ>
    where
        WZ: std::fmt::Debug,
    {
        Foo { a: self.a, b }
    }
}

More examples can be found in tests

References

Dependencies

~150–550KB
~13K SLoC