#![cfg(feature = "pyo3")]
use std::collections::HashMap;
use std::sync::Arc;
use parking_lot::RwLock;
use pyo3::prelude::*;
use smol_str::SmolStr;
use uni_plugin::PluginId;
#[derive(Debug)]
pub struct PyPluginRuntime {
pub plugin_id: PluginId,
callables: RwLock<HashMap<SmolStr, Py<PyAny>>>,
}
impl PyPluginRuntime {
#[must_use]
pub fn new(plugin_id: PluginId) -> Arc<Self> {
Arc::new(Self {
plugin_id,
callables: RwLock::new(HashMap::new()),
})
}
pub fn insert(&self, name: impl Into<SmolStr>, callable: Py<PyAny>) {
self.callables.write().insert(name.into(), callable);
}
#[must_use]
pub fn get(&self, name: &str) -> Option<Py<PyAny>> {
self.callables
.read()
.get(name)
.map(|p| Python::attach(|py| p.clone_ref(py)))
}
#[must_use]
pub fn len(&self) -> usize {
self.callables.read().len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.callables.read().is_empty()
}
#[must_use]
pub fn names(&self) -> Vec<SmolStr> {
self.callables.read().keys().cloned().collect()
}
}