scufflecloud_video_api_traits/
dataloader.rs

1use std::collections::HashMap;
2
3use db_types::models::{Stream, StreamId};
4use scuffle_batching::DataLoaderFetcher;
5
6pub trait DataloaderInterface {
7    fn stream_loader(
8        &self,
9    ) -> &scuffle_batching::DataLoader<impl DataLoaderFetcher<Key = StreamId, Value = Stream> + Send + Sync + 'static>;
10}
11
12pub trait DataLoader {
13    type Key;
14    type Value;
15    type Error;
16
17    fn load(&self, key: Self::Key) -> impl Future<Output = Result<Option<Self::Value>, Self::Error>>;
18    fn load_many(
19        &self,
20        keys: impl IntoIterator<Item = Self::Key> + Send,
21    ) -> impl Future<Output = Result<HashMap<Self::Key, Self::Value>, Self::Error>>;
22}
23
24impl<E> DataLoader for scuffle_batching::DataLoader<E>
25where
26    E: scuffle_batching::DataLoaderFetcher + Send + Sync + 'static,
27{
28    type Error = ();
29    type Key = E::Key;
30    type Value = E::Value;
31
32    async fn load(&self, key: Self::Key) -> Result<Option<Self::Value>, Self::Error> {
33        scuffle_batching::DataLoader::load(self, key).await
34    }
35
36    async fn load_many(
37        &self,
38        keys: impl IntoIterator<Item = Self::Key> + Send,
39    ) -> Result<HashMap<Self::Key, Self::Value>, Self::Error> {
40        scuffle_batching::DataLoader::load_many(self, keys).await
41    }
42}