Skip to main content

mln_engine/storage/
file_source.rs

1use crate::storage::request::{Request, Responder};
2use crate::storage::resource::Resource;
3use crate::storage::response::Response;
4
5/// A source of resources. File sources are shared between threads, hence
6/// `Send + Sync`. Each takes its configuration when it's made, or through
7/// methods of its own type.
8pub trait FileSource: Send + Sync {
9    /// Starts loading `resource`, and sends its responses to `responder`
10    /// from any thread. It returns at once.
11    fn load(&self, resource: &Resource, responder: Responder);
12
13    /// Whether this file source loads `resource`.
14    fn can_request(&self, resource: &Resource) -> bool;
15
16    /// Whether this file source answers cache-only requests: ones that are
17    /// worth answering only cheaply, from a local cache, to cover part of
18    /// the map while it loads.
19    fn supports_cache_only(&self) -> bool {
20        false
21    }
22}
23
24/// Requests through a [`FileSource`].
25pub trait FileSourceExt: FileSource {
26    /// Requests `resource`. `callback` receives the responses on the
27    /// threads that send them (see [`Request`]), until the request drops.
28    fn request(&self, resource: &Resource, callback: impl FnMut(Response) + Send + 'static) -> Request {
29        let (request, responder) = Request::new(callback);
30        self.load(resource, responder);
31        request
32    }
33}
34
35impl<T: FileSource + ?Sized> FileSourceExt for T {}