Skip to content

Commit

Permalink
Avoid TileId collisions when using insert_new
Browse files Browse the repository at this point in the history
  • Loading branch information
jleibs committed Dec 19, 2023
1 parent 1418e65 commit dcdcde1
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,24 @@ impl<Pane> Tiles<Pane> {
}
}

fn next_free_id(&mut self) -> TileId {
let mut id = TileId::from_u64(self.next_tile_id);

// Make sure it doesn't collide with an existing id
while self.tiles.get(&id).is_some() {
self.next_tile_id += 1;
id = TileId::from_u64(self.next_tile_id);
}

// Final increment the next_id
self.next_tile_id += 1;

id
}

#[must_use]
pub fn insert_new(&mut self, tile: Tile<Pane>) -> TileId {
let id = TileId::from_u64(self.next_tile_id);
self.next_tile_id += 1;
let id = self.next_free_id();
self.tiles.insert(id, tile);
id
}
Expand Down

0 comments on commit dcdcde1

Please sign in to comment.