use std::sync::Arc;
use arc_swap::ArcSwap;
use cc_lb_plugin_wire::metadata::PluginMetadata;
use cc_lb_plugin_wire::schema::HookKind;
use wasmtime::InstancePre;
use crate::engine::HostState;
pub struct PluginCell {
pub version_id: u64,
pub instance_pre: Arc<InstancePre<HostState>>,
pub metadata: PluginMetadata,
pub memory_max_pages: u32,
pub plugin_name: Arc<str>,
pub content_hash: [u8; 32],
}
impl std::fmt::Debug for PluginCell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PluginCell")
.field("version_id", &self.version_id)
.field("plugin_metadata", &self.metadata)
.field("content_hash_hex", &format_hex(&self.content_hash))
.field("memory_max_pages", &self.memory_max_pages)
.finish_non_exhaustive()
}
}
fn format_hex(bytes: &[u8; 32]) -> String {
let mut out = String::with_capacity(64);
for b in bytes {
use std::fmt::Write;
let _ = write!(out, "{:02x}", b);
}
out
}
pub struct PluginSlot {
pub name: String,
pub kind: HookKind,
pub current: ArcSwap<PluginCell>,
}
impl PluginSlot {
pub fn new(name: impl Into<String>, kind: HookKind, initial: PluginCell) -> Self {
Self {
name: name.into(),
kind,
current: ArcSwap::from_pointee(initial),
}
}
}