# populate_tile_store_stage.rs - source
Source of the Rust file `maplibre/src/stages/populate_tile_store_stage.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
    51
    52
    53

    //! Receives data from async threads and populates the [`crate::io::tile_repository::TileRepository`].

    use super::{MessageReceiver, SharedThreadState, TessellateMessage, TileTessellateMessage};
    use crate::context::MapContext;
    use crate::io::tile_repository::StoredLayer;
    use crate::schedule::Stage;
    use std::sync::mpsc;

    pub struct PopulateTileStore {
        shared_thread_state: SharedThreadState,
        message_receiver: MessageReceiver,
    }

    impl PopulateTileStore {
        pub fn new(shared_thread_state: SharedThreadState, message_receiver: MessageReceiver) -> Self {
            Self {
                shared_thread_state,
                message_receiver,
            }
        }
    }

    impl Stage for PopulateTileStore {
        fn run(
            &mut self,
            MapContext {
                tile_repository, ..
            }: &mut MapContext,
        ) {
            if let Ok(result) = self.message_receiver.try_recv() {
                match result {
                    TessellateMessage::Layer(layer_result) => {
                        let layer: StoredLayer = layer_result.into();
                        tracing::trace!(
                            "Layer {} at {} reached main thread",
                            layer.layer_name(),
                            layer.get_coords()
                        );
                        tile_repository.put_tessellated_layer(layer);
                    }
                    TessellateMessage::Tile(TileTessellateMessage { request_id, coords }) => loop {
                        if let Ok(mut tile_request_state) =
                            self.shared_thread_state.tile_request_state.try_lock()
                        {
                            tile_request_state.finish_tile_request(request_id);
                            tracing::trace!("Tile at {} finished loading", coords);
                            break;
                        }
                    },
                }
            }
        }
    }