idanarye / rust-typed-builder

Compile-time type-checked builder derive
https://crates.io/crates/typed-builder
Apache License 2.0
904 stars 52 forks source link

build_method into type parameters #104

Closed rksm closed 4 months ago

rksm commented 1 year ago

Hello and thank you for this amazing project! It makes builder type state so much easier to use, it's a joy!

I've come across a small issue. I have

#[derive(typed_builder::TypedBuilder)]
#[builder(build_method(into = Result<Index>))]
pub struct IndexBuilder { ... }

impl From<IndexBuilder> for Result<Index> {
    fn from(value: IndexBuilder) -> Self { ... }
}

into = Result<Index> is not parsed correctly as apparently the macro does not expect type params. I can work around this with type IndexResult = Result<Index>; but it would be cool if this could be supported directly.

Thank you!

idanarye commented 1 year ago

95 is probably a better way to do what you want to do. It's still a WiP though, so you can't use it yet.

The problem is that I parse #[builder(...)] attribute by pretending it's a function call and the settings are assignments (Rust - or at least the syn crate - treats assignments as expressions at the parser level and only disallows them at a later stage). This allows me to use complex syntax instead of wrapping everything with strings, but the downside is cases like this.

Maybe some day I'll replace it with a manual parsing (that still uses the syn crate, but doesn't pretend the attribute is something it is not) which will make such syntax possible. Don't hold your breath though.

Luckily, the turbofish syntax - which is required in expressions - can also be used for types. This means that this works:

#[builder(build_method(into = Result::<Index>))]

Not as pretty, but it's going to have to do.

rksm commented 1 year ago

Yep, I understand, parsing macros can be gnarly. Your suggested workaround is good enough, thanks!