colin-kiegel / rust-derive-builder

derive builder implementation for rust structs
https://colin-kiegel.github.io/rust-derive-builder/
Apache License 2.0
1.28k stars 82 forks source link

A shortcut for builder #289

Closed oriontvv closed 1 year ago

oriontvv commented 1 year ago

Hello, first of all thanks for awesome crate!

I haven't dig dive into the sources yet, but want to discuss - is it possible to add simple shortcut to built structure? It would be cool to get a Builder and allow us not to import it explicitly. Most editors can't detect and generate this import(maybe because of macro) and we have to write it by hands. User's code could be something like that:

#[derive(Default, Builder, Debug)]
#[builder(setter(into))]
struct Channel {
    token: i32,
}
// main.rs:
use crate::Channel;

fn main() {
    // let ch = Channel::Builder::default() or
    let ch = Channel::builder().default()
        .token(42)
        .build()
        .unwrap();
    println!("{:?}", ch);
}

What do you think guys?

TedDriggs commented 1 year ago

This is a duplicate of #73, which was closed for the rationale explained here.

oriontvv commented 1 year ago

Oh, so sad. Thanks for the answer. Looks like a lot of people want such a feature and we should think about switching to typed-builder.

TedDriggs commented 1 year ago

If you want a builder that's checked at compile time using session types, you should absolutely use typed-builder.

Or, if completely changing your builder strategy over this proves to be too dramatic, you could add the three lines needed to one of your impl blocks and provide the method.