this post was submitted on 11 Jul 2023
8 points (100.0% liked)
Rust
5953 readers
7 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Yeah, sort of. I probably didn't explain super well, and also probably don't fully understand the problem so here are some code snippets that might make things more concrete and you can tell me where my asumptions of your codebase are wrong
So first off we have what I assume you were suggesting with multiple options for the individual db props. I commented where things are painful and bug prone:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2ecb08dd5d54378c693a7014599e8645
If
Option
is indeed the aproach you want to take we can solve a lot of these problems by moving all the fields that go optional together into a separate struct:https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9eb2f896e171507acb64283d5b530673
We get even more type safety and clarity by making them separate types (and have ArmyWithDbProps wrap Army):
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c2e33aa1c09538ffdbe0cd440bbff3e1
Or we could use Into if it's not appropriate for
process_army_from_db
to turn anArmyWithDbProps
into anArmy
:https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f82043935625572cb08c240f23944f0e
Also this last example is using clone when if this is in-fact the direction you want to go we could be using pointers to avoid unnecessary clones. Let me know if this is the case and I can write a version with pointers and lifetimes.
I'm sure we've missed something here specific to your software (obviously the above links are all trivial examples), but I just wanted to help clarify my original point with some concrete code. If you can share some of your code we might be able to give you more specific advice.