this post was submitted on 25 Mar 2024
8 points (100.0% liked)

Rust

5953 readers
16 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 have a struct that looks like this:

pub struct Game {
    /// A HashSet with the players waiting to play as account strings.
    lobby: HashSet<String>,
    /// capacity determines  how many people a match contains.
    capacity: u8,
    /// A vector of ongoing matches.
    matches: Vec<Match>,
    /// HashSet indicating for each player which match they are in.
    players: HashMap<String, usize>,
}

I realised that this won't work because if there are 3 matches (0, 1, 2) and I remove 1 because it ends, the players that used to point at 2 will be pointing outside the vector or to an incorrect match.

So I thought the obvious solution was to use a reference to the match: players: HashMap<String, &Match>. But this makes lifetimes very complicated.

What's a good way to deal with a case like these where data are interrelated in the same struct?

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 5 points 7 months ago (1 children)

I don't think there is a good way of having references within the same struct, but you could store reference counted matches:

matches: Vec<Rc<Match>>,
players: HashMap<String, Rc<Match>>,

You would still have to make sure that the players map is updated, maybe weak references are useful there.

Maybe you could also consider storing the players of a match in the match itself, not outside.

[โ€“] [email protected] 1 points 7 months ago

Thanks, the RC is a possible approach. It seems to violate DRY a bit but maybe there's no way around it.

The reason I had the players outside the match is that I need them there anyway, because when I get a player action I need to check in which match they are, who are their opponent(s) and so on. So even if they're in, they'll have to be out too as there are concurrent matches and the player actions come all through the same network stream.