dtolnay / proc-macro-workshop

Learn to write Rust procedural macros  [Rust Latam conference, Montevideo Uruguay, March 2019]
Apache License 2.0
4k stars 1.01k forks source link

Builder test 2 - Option intended? #29

Closed tjkirch closed 4 years ago

tjkirch commented 4 years ago

builder/tests/02-create-builder.rs says "Before moving on, have the macro also generate:" and it shows the CommandBuilder struct based on the input Command type, but with each field made Optional if it's not already.

I understand why they'd be optional -- the builder pattern requires adding individual fields before construction of the output type -- but is dynamically making fields optional an intended piece of test 2? It comes quite early in the workshop, but doing so seems rather complicated.

In particular, it seems to assume knowledge gained in test 6 (see #12) regarding detection of whether a field has Option (because one field is already Option) plus knowledge of modifying field types.

Simpler tasks, like building an ident, have a resource link, but there's no comment about type modification.

Sorry if I'm missing an obvious alternative for adding Option!

dtolnay commented 4 years ago

because one field is already Option

This is not the case in test 2. You can put Option\<#ty> around every field.

tjkirch commented 4 years ago

Ah, ok. I had assumed that the basic structure wasn't changed between tests. Thanks!