# tile_request_state.rs - source
Source of the Rust file `maplibre/src/io/tile_request_state.rs`.
Keywords: rust, rustlang, rust-lang

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50

    //! Tile request state.

    use crate::coords::WorldTileCoords;
    use crate::io::{TileRequest, TileRequestID};
    use std::collections::{HashMap, HashSet};

    /// Stores a map of pending requests, coords and the current tile being requested.
    #[derive(Default)]
    pub struct TileRequestState {
        current_id: TileRequestID,
        pending_tile_requests: HashMap<TileRequestID, TileRequest>,
        pending_coords: HashSet<WorldTileCoords>,
    }

    impl TileRequestState {
        pub fn new() -> Self {
            Self {
                current_id: 1,
                pending_tile_requests: Default::default(),
                pending_coords: Default::default(),
            }
        }

        pub fn is_tile_request_pending(&self, coords: &WorldTileCoords) -> bool {
            self.pending_coords.contains(coords)
        }

        pub fn start_tile_request(&mut self, tile_request: TileRequest) -> Option<TileRequestID> {
            if self.is_tile_request_pending(&tile_request.coords) {
                return None;
            }

            self.pending_coords.insert(tile_request.coords);
            let id = self.current_id;
            self.pending_tile_requests.insert(id, tile_request);
            self.current_id += 1;
            Some(id)
        }

        pub fn finish_tile_request(&mut self, id: TileRequestID) -> Option<TileRequest> {
            self.pending_tile_requests.remove(&id).map(|request| {
                self.pending_coords.remove(&request.coords);
                request
            })
        }

        pub fn get_tile_request(&self, id: TileRequestID) -> Option<&TileRequest> {
            self.pending_tile_requests.get(&id)
        }
    }