use orx_closure::*; use std::{collections::HashMap, rc::Rc}; fn test_fun usize>(fun: F) { assert_eq!(fun('a'), 1); assert_eq!(fun('b'), 2); assert_eq!(fun('c'), 42); } #[test] fn no_capture() { let fun = Capture(()).fun(|_: &(), c: char| match c { 'a' => 1, 'b' => 2, _ => 42, }); test_fun(fun.as_fn()); test_fun(|x| fun.call(x)); } #[test] fn capture_data() { let map = HashMap::::from_iter([('a', 1), ('b', 2)]); let fun = Capture(map).fun(|map, c| *map.get(&c).unwrap_or(&42)); test_fun(fun.as_fn()); test_fun(|x| fun.call(x)); } #[test] fn capture_ref() { let map = HashMap::::from_iter([('a', 1), ('b', 2)]); let fun = Capture(&map).fun(|map, c| *map.get(&c).unwrap_or(&42)); test_fun(fun.as_fn()); test_fun(|x| fun.call(x)); } #[test] fn capture_deref() { let map = HashMap::::from_iter([('a', 1), ('b', 2)]); let map = Rc::new(map); let fun = Capture(map.clone()).fun(|map, c| *map.get(&c).unwrap_or(&42)); test_fun(fun.as_fn()); test_fun(|x| fun.call(x)); let map = HashMap::::from_iter([('a', 1), ('b', 2)]); let map = Box::new(map); let fun = Capture(map).fun(|map, c| *map.get(&c).unwrap_or(&42)); test_fun(fun.as_fn()); test_fun(|x| fun.call(x)); }