this post was submitted on 11 Jul 2023
8 points (100.0% liked)

Rust

5981 readers
65 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

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
 

I'm wondering with my game project how best to handle types. I'm basically fetching all the rows in a particular table and creating a struct to represent that data. I then have like 5 or 6 sequential steps I'm trying to go through where I need to modify those initial values from the db.

My first thought was to have a base type called 'BaseArmy', then I needed to add new temporary properties that are not in the db and not represented in the original struct, so I decided to create a new struct 'Army'. The problem is I keep running into errors when passing converting from BaseArmy to Army. I tried writing a From impl, but it wasn't working (and I'm not even sure if it's the approach I should be taking).

So should I:

  1. Have multiple different types to handle these kinds of cases
  2. Have just one type somehow where I add properties to it? If so, how? I recently tried using Options for the fields that are not initially available, and that seems to be working but it feels weird.
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 4 points 1 year ago (1 children)

What errors are you getting when converting from BaseArmy to Army? If you're storing references to other structs in your struct, things can get complicated. Using Clone/Copy types will make things easier.

I'd also recommend the builder pattern for this. It let's you build up a type incrementally, with a final build step that produces the final type.

[–] [email protected] 1 points 1 year ago* (last edited 1 year ago) (1 children)

It was basically me passing BaseArmy in as a param to a fucntion, then returning an Army type. I tried a few different things, but what I really wanted to do was just spread out the struct like I would in Typescript. Rust seems to support this UNLESS there's one field that's different.

Let me give builder pattern a try. I was literally just learning more about it, but didn't think to apply it here.

EDIT:

Here's what' I'm trying to do: Battalion { count: count, position, ..db_battalion_template }

Then the error I get:

mismatched types expected Battalion, found BattalionTemplate

EDIT2:

After more fiddling around and adding the from conversion:

Battalion { count: count, position, ..Battalion::from(db_battalion_template) } impl From<BattalionTemplate> for Battalion { fn from(a: BattalionTemplate) -> Self { let serialized = serde_json::to_string(&a).unwrap(); Self { position: 0, ..serde_json::from_str(&serialized).unwrap() } } }

I get this error: thread 'main' panicked at 'called Result::unwrap() on an Err value: Error("missing field position", line: 1, column: 227)'

Edit 3:

I at least got the from conversion to work by just manually specifying the Battalion fields. Should I just accept that I can't spread struct properties from one type on to another unless they have exactly the same fields?

[–] [email protected] 2 points 1 year ago

Yeah, you can't pass one type when the function expects another type. You have either use generics and trait bounds or provide the exact type that a function expects..