| Crates.io | method_chaining |
| lib.rs | method_chaining |
| version | 0.1.1 |
| created_at | 2025-08-05 21:55:20.127077+00 |
| updated_at | 2025-11-08 06:38:14.760578+00 |
| description | A Rust procedural macro that automatically makes functions and structs chainable. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1783123 |
| size | 34,427 |
A Rust procedural macro that automatically makes functions and structs chainable.
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);
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);
&mut self or mut self as the first parameter