rust-typed-builder icon indicating copy to clipboard operation
rust-typed-builder copied to clipboard

Support a Box<dyn IncompleteBuilder> trait to allow passing builders between functions

Open sonthonaxrk opened this issue 6 months ago • 1 comments

This library is great, but it can be a bit of a pain to use outside of closures that support type inference.

Just a bit of a quality of life thing:

When building things like in a function that returns a builder.

fn stage_1(input) -> MyContextBuilder<((f64,), ())> {
    MyContextBuilder<((f64,), (), ())> = MyContextBuilder::builder()
        .spot(calculated_spot)
}

fn stage_2(context: MyContextBuilder<((f64,), ())>, another_input) -> MyContextBuilder<((f64,), (f64))> {
    let vol = calculate_vol(another_input);

    MyContextBuilder<((f64,), (), ())> = MyContextBuilder::builder()
        .spot(calculated_spot)
        .vol(vol)
}


fn pipeline(quotes: Vec<Input>) {      
    quotes
        .into_iter()
        .map(stage_1)
        .map(stage_2)
        .map(MyContextBuilder::build)
        .collect::<Vec<MyContext>>()
}

The type is complex, it's a bit of pain if I have pipelines like this. It kinda precludes putting everything in functions without really nasty types.

I'm wondering if a dyn trait could be used with downcast at the end. I know this would make it fallible, but just an idea.


Is there a better pattern for me to use?

sonthonaxrk avatar Apr 30 '25 09:04 sonthonaxrk