jetro_core/eval/
methods.rs1use std::sync::Arc;
6use indexmap::IndexMap;
7
8use super::value::Val;
9use super::EvalError;
10
11pub trait Method: Send + Sync {
18 fn call(&self, recv: Val, args: &[Val]) -> Result<Val, EvalError>;
19}
20
21impl<F> Method for F
23where
24 F: Fn(Val, &[Val]) -> Result<Val, EvalError> + Send + Sync,
25{
26 #[inline]
27 fn call(&self, recv: Val, args: &[Val]) -> Result<Val, EvalError> {
28 self(recv, args)
29 }
30}
31
32#[derive(Clone)]
35pub struct MethodRegistry {
36 methods: IndexMap<String, Arc<dyn Method>>,
37}
38
39impl MethodRegistry {
40 pub fn new() -> Self {
41 Self { methods: IndexMap::new() }
42 }
43
44 pub fn register(&mut self, name: impl Into<String>, method: impl Method + 'static) {
47 self.methods.insert(name.into(), Arc::new(method));
48 }
49
50 #[inline]
52 pub fn get(&self, name: &str) -> Option<&Arc<dyn Method>> {
53 self.methods.get(name)
54 }
55
56 pub fn is_empty(&self) -> bool { self.methods.is_empty() }
57
58 pub fn iter(&self) -> impl Iterator<Item = (&str, &Arc<dyn Method>)> {
60 self.methods.iter().map(|(n, m)| (n.as_str(), m))
61 }
62
63 pub fn register_arc(&mut self, name: impl Into<String>, method: Arc<dyn Method>) {
66 self.methods.insert(name.into(), method);
67 }
68}
69
70impl Default for MethodRegistry {
71 fn default() -> Self { Self::new() }
72}