use std::{any::Any, collections::HashMap, rc::Rc};
use crate::common::generic_key::GenericKeyPart;
pub struct GenericFakeStore<const GENERIC_COUNT: usize> {
_name: &'static str,
impls: HashMap<[GenericKeyPart; GENERIC_COUNT], Rc<dyn Any>>,
}
impl<const GENERIC_COUNT: usize> GenericFakeStore<GENERIC_COUNT> {
pub fn new(name: &'static str) -> Self {
Self {
_name: name,
impls: HashMap::new(),
}
}
pub fn setup_for<WrappedClosure: 'static>(
&mut self,
generic_keys: [GenericKeyPart; GENERIC_COUNT],
f: WrappedClosure,
) {
self.impls.insert(generic_keys, Rc::new(f));
}
pub fn clear_for(&mut self, generic_keys: [GenericKeyPart; GENERIC_COUNT]) {
self.impls.remove(&generic_keys);
}
pub fn is_set_for(&self, generic_keys: [GenericKeyPart; GENERIC_COUNT]) -> bool {
self.impls.contains_key(&generic_keys)
}
pub fn get_for<WrappedClosure: 'static>(
&self,
generic_keys: [GenericKeyPart; GENERIC_COUNT],
) -> Option<Rc<WrappedClosure>> {
self.impls
.get(&generic_keys)
.cloned()
.and_then(|c| c.downcast::<WrappedClosure>().ok())
}
}
#[cfg(test)]
mod tests {
use std::any::TypeId;
use crate::common::generic_key::ConstValue;
use super::*;
#[test]
fn test_generic_fake_store() {
let mut store = GenericFakeStore::<2>::new("test_fn");
let i32_string_key = [
GenericKeyPart::Type(TypeId::of::<i32>()),
GenericKeyPart::Type(TypeId::of::<String>()),
];
let u32_string_key = [
GenericKeyPart::Type(TypeId::of::<u32>()),
GenericKeyPart::Type(TypeId::of::<String>()),
];
assert!(!store.is_set_for(i32_string_key.clone()));
assert!(!store.is_set_for(u32_string_key.clone()));
store.setup_for::<Box<dyn Fn(i32, String) -> String>>(
i32_string_key.clone(),
Box::new(|a: i32, b: String| format!("Fake for i32, String: {} {}", a, b)),
);
store.setup_for::<Box<dyn Fn(u32, String) -> String>>(
u32_string_key.clone(),
Box::new(|a: u32, b: String| format!("Fake for u32, String: {} {}", a, b)),
);
assert!(store.is_set_for(i32_string_key.clone()));
assert!(store.is_set_for(u32_string_key.clone()));
let f1 = store.get_for::<Box<dyn Fn(i32, String) -> String>>(i32_string_key.clone());
let f2 = store.get_for::<Box<dyn Fn(u32, String) -> String>>(u32_string_key.clone());
println!("Calling f1:");
println!("{}", f1.unwrap()(42, "Alice".into()));
println!("Calling f2:");
println!("{}", f2.unwrap()(42, "Bob".into()));
store.clear_for(i32_string_key.clone());
store.clear_for(u32_string_key.clone());
assert!(!store.is_set_for(i32_string_key));
assert!(!store.is_set_for(u32_string_key));
}
#[test]
fn test_const_generic_values_do_not_collide() {
let mut store = GenericFakeStore::<1>::new("test_fn");
store.setup_for::<Box<dyn Fn() -> &'static str>>(
[GenericKeyPart::Const(ConstValue::new(5usize))],
Box::new(|| "fake for C=5"),
);
assert!(store.is_set_for([GenericKeyPart::Const(ConstValue::new(5usize))]));
assert!(!store.is_set_for([GenericKeyPart::Const(ConstValue::new(7usize))]));
}
#[test]
fn test_const_value_supports_arbitrary_hash_eq_types() {
#[derive(Hash, PartialEq, Eq)]
struct CustomKey(u8, bool);
let mut store = GenericFakeStore::<1>::new("test_fn");
store.setup_for::<Box<dyn Fn() -> &'static str>>(
[GenericKeyPart::Const(ConstValue::new(CustomKey(1, true)))],
Box::new(|| "fake"),
);
assert!(store.is_set_for([GenericKeyPart::Const(ConstValue::new(CustomKey(1, true)))]));
assert!(!store.is_set_for([GenericKeyPart::Const(ConstValue::new(CustomKey(2, true)))]));
}
}