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.
  • Barbacamanitu@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    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.

    • nerdblood@programming.devOP
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      1 year ago

      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)’

      • Barbacamanitu@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        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…