-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow to access already set fields within pattern of partial shared builder #111
Comments
I could have refactor to make bigger objects to consist of smaller, or write my own builders, or custom test inputs data objects. |
Tried to quickly get things done, so as I see now So need some refactor to allow builder to generate many impls so each getter added iff value was set as witnessed by non generic parameter constrained specialezed. Seems required changes are more broad than just for this single feature? |
Let's discuss the design for this feature first. First of all, I expect adding getters is not something that everyone needs. Having them generated additionally will increase the compilation time (we have crates where there are ~320 structs with builders, where compile time is important). So getters should definitely be an
Then what about For For Then there is probably a use case where one would like to deconstruct the builder into raw parts entirely. In this case, it would be cool if Rust had anonymous structs (rust-lang/rfcs#2584), but alas there aren't, so this would need to be a workaround. The macro should generate some projection struct that can hold any state of the members (set or unset). In general, this looks like a big feature that will evolve substantially. If you'd like to have some subset of this feature quickly, I'm fine to have it but granted that it's added under an My first naive take at this would be replicating some of the API of |
Subset like thisEnabling only for specific fields sounds great, because people can pick exact places where common splits in shared partial builder happen (likely will compose nice with groups - so splits can be made around these points - as i do in manual builder anyway) with proper Option handling. Readonly pub reference only. For mutation people use builder. Feature only for comparison and decision making, not for getting clone of value out. QuestionsCurrently as I see I can set once, rls shows me I can set value second time, but compilation fails. What about life time? My assumption is that holding reference to property will not prevent setting other fields. Any changes envisioned here? |
I don't think so. I think it's more of an issue with rust-analyzer because it doesn't take into account trait bounds when showing completions. Maybe there is a way to work around that but I don't think it's worth the effort at least now. We have a readable compile error in case the field is set the second time. This is good enough. Especially, I want to keep the current design where there is a single impl block for the builder. This is because it makes the
Unfortunately, Rust doesn't work that way. The getter will take |
i need to dig into this design. never new it is possible to do without several impls.
so here unsafe needed. for sure no references could be hold if we when build is called, that is fine. also double set same field prevented. so after field is set, it is set. fine to continue mutate rest of builder state and hold reference to old state, so just need to use unsafe internally within well known safe patter, same as array split into 2 reference (one is mut and other not, as long as slices do not overlap). so need to check if this #97 desing works well with getters. |
So I guess next step could be send proof of concept of manually modified expand of bon builder doing Getter and #97 ? |
That is currently a deal breaker for me. There is no unsafe code in Better if you could provide an example case that solves some particular problem where you'd think |
what about defaults? simplest is not to have getter for default which was not set explicitly. any other options? |
Yes, uny unset member should have no getter generated for it. We have a guarantee that default values are evaluated lazily only when the final |
@Veetaha i am trying to thing up getter code to look like. For example this simple builder expanded https://github.com/dzmitry-lahoda-forks/bon/blob/dz/2/bon-get/src/main.rs As I see specialization of setter is done via return value. Unfortunately I do not have return value in just field value to return. Any ideas how to make it nice? I tinkering next:
So is it possible at all to keep single impl or go right away with many? |
Mat be |
with
|
@Veetaha what about String vs str? I feel would not change String to str? So will return &String. |
What return value do you mean? Could you give more context or examples?
I'd like to keep a single impl block. We have For example an
I wonder what |
From remaining questions I have only deref, as I see getset does not do it jbaublitz/getset#35 . So it can be added later if needed. Things like copy/deref can be added later. |
please reopen when feature would have more broad usage. |
real world usage
proptest
allows to chain into hierarchy generated properties viaprop_flat_map
(with optimized shrinking retained).so each
prop_flat_map
in chain depends on data to be generated from upper layer, uses that data to constraint its out put.prop_flat_map(|prev| prev, prev+1 ).prop_flat_map(|(prev_0, prev_1|, ..., 0..prev_1).
so final test function gets
do_test(prev_0, prev_1, prev_2)
.so I thin of using builder on each stage and path only partial builder, instead of many parameters. but need access to already set fields. in the end I do call build and produce final data struct.
example without builder:
https://altsysrq.github.io/proptest-book/proptest/tutorial/higher-order.html
A note for the community from the maintainers
Please vote on this issue by adding a 👍 reaction to help the maintainers with prioritizing it. You may add a comment describing your real use case related to this issue for us to better understand the problem domain.
The text was updated successfully, but these errors were encountered: