//! A mapper based on a function from keys to values. use std::pin::Pin; use crate::lazy_mapping::{LazyMapper, LazyMappingBackend}; /// A function from keys to values. /// /// Should be pure, except for use of other mappings. This ensures recomputation is done when needed. pub trait Thunk: Send + 'static { fn compute(&self, key: K) -> Pin + Send + '_>>; } #[derive(Debug, Default)] pub struct ThunkBackend { thunk: T, } impl ThunkBackend { pub fn new(thunk: T) -> Self { Self { thunk } } } impl LazyMappingBackend for ThunkBackend where T: Thunk, { async fn is_dirty(&self, _: &K) -> bool { false // only dirty if marked dirty by someting else } fn compute(&self, key: K) -> impl std::future::Future + Send + '_ { self.thunk.compute(key) } } pub type ThunkMapper = LazyMapper>;