method_chaining

Crates.iomethod_chaining
lib.rsmethod_chaining
version0.1.1
created_at2025-08-05 21:55:20.127077+00
updated_at2025-11-08 06:38:14.760578+00
descriptionA Rust procedural macro that automatically makes functions and structs chainable.
homepage
repository
max_upload_size
id1783123
size34,427
Yan Huang (copi143)

documentation

README

Method Chaining

A Rust procedural macro that automatically makes functions and structs chainable.

Usage

For Functions

The #[chainable] attribute transforms functions to return self, enabling method chaining:

use method_chaining::chainable;

struct Builder {
    name: String,
    age: u32,
}

impl Builder {
    #[chainable]
    fn set_name(&mut self, name: String) {
        self.name = name;
        // No need to return self - it's added automatically
    }

    #[chainable]
    fn set_age(&mut self, age: u32) {
        self.age = age;
    }
}

// Now you can chain methods:
let builder = Builder::default()
    .set_name("Alice".to_string())
    .set_age(30);

For Structs

The #[chainable] attribute automatically generates with_* methods for all fields:

#[chainable]
struct Config {
    host: String,
    port: u16,
    timeout: u64,
}

// Automatically generates:
// - with_host(self, value: String) -> Self
// - with_port(self, value: u16) -> Self
// - with_timeout(self, value: u64) -> Self

let config = Config::default()
    .with_host("localhost".to_string())
    .with_port(8080)
    .with_timeout(5000);

Requirements

  • Functions must have &mut self or mut self as the first parameter
  • Functions should not have explicit return types
  • Functions should not contain explicit return statements with values
Commit count: 0

cargo fmt