dataload_rs/batch_function.rs
1use async_trait::async_trait;
2
3/// A `BatchFunction` defines the method through which some `Loader` may fetch
4/// batched data from some resource. The `BatchFunction` receives a slice of keys
5/// that have been requested during the `Loader`'s most recent execution frame, and some user
6/// defined context struct.
7///
8/// Unlike the reference facebook dataloader implementation, the BatchFunction is not required to
9/// return a result for all keys that were provided. Instead, it can return any set of loaded key
10/// value pairs, in any order it chooses. Requesters of keys whose values are not returned by the
11/// `BatchFunction` will receive a `None`. Error handling and reporting is expected to be done
12/// within the BatchFunction (i.e. through some error sink in the context).
13///
14/// Multiple `BatchFunctions` (and therefore loaders) can share the same context (likely through an
15/// `Arc`).
16#[async_trait]
17pub trait BatchFunction<K, V> {
18 type Context;
19 async fn load(keys: &[K], context: &Self::Context) -> Vec<(K, V)>;
20}