From 5f160a025c79d86a5bd0c5ab514f722902249891 Mon Sep 17 00:00:00 2001 From: Padraic Fanning Date: Wed, 10 Feb 2021 20:48:19 -0500 Subject: [PATCH] Add 81974 Most likely low-priority; feel free to ignore Issue: rust-lang/rust#81974 --- ices/81974.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ices/81974.rs diff --git a/ices/81974.rs b/ices/81974.rs new file mode 100644 index 00000000..5e3c6532 --- /dev/null +++ b/ices/81974.rs @@ -0,0 +1,51 @@ +#![feature(unboxed_closures)] +#![feature(fn_traits)] + +use std::collections::HashMap; +use std::hash::Hash; + +struct CachedFun{ + cache: HashMap, + fun: fn(&mut CachedFun, A) -> B +} + +impl CachedFun { + fn new(fun: fn(&mut Self, A) -> B) -> Self { + CachedFun { + cache: HashMap::new(), + fun + } + } +} + +impl FnOnce for CachedFun where + A: Eq + Hash + Clone, + B: Clone, +{ + type Output = B; + extern "rust-call" fn call_once(mut self, a: A) -> Self::Output { + self.call_mut(a) + } +} + + +impl FnMut for CachedFun where + A: Eq + Hash + Clone, + B: Clone, +{ + extern "rust-call" fn call_mut(&mut self, a: A) -> Self::Output { + self.cache.get(&a) + .map(|a| a.clone()) + .unwrap_or_else(|| { + let b = (self.fun)(self, a.clone()); + self.cache.insert(a, b.clone()); + b + }) + } +} + +fn main() -> () { + let pesce = |y: &mut CachedFun, x| x + 1; + let cachedcoso = CachedFun::new(pesce); + cachedcoso.call_once(1); +}