pub trait ResourceSupport: NativeProcessorNode {
type Resource: Resource + 'static;
// Required methods
fn compute_resource_key(params: Option<&Value>) -> String;
fn init_resource(params: Option<Value>) -> Result<Self::Resource, String>;
// Provided method
fn deinit_resource(_resource: Self::Resource) { ... }
}Expand description
Optional trait for plugins that need shared resource management (e.g., ML models).
Plugins that implement this trait can have their resources (models) automatically cached and shared across multiple node instances. This avoids loading the same model multiple times in memory.
§Example
use streamkit_plugin_sdk_native::prelude::*;
use std::sync::Arc;
pub struct MyModelResource {
model_data: Vec<f32>,
}
impl Resource for MyModelResource {
fn size_bytes(&self) -> usize {
self.model_data.len() * std::mem::size_of::<f32>()
}
fn resource_type(&self) -> &str { "ml_model" }
}
pub struct MyPlugin {
resource: Arc<MyModelResource>,
}
// Note: MyPlugin must also implement NativeProcessorNode for this to compile
impl ResourceSupport for MyPlugin {
type Resource = MyModelResource;
fn compute_resource_key(params: Option<&serde_json::Value>) -> String {
// Hash only the params that affect resource creation
format!("{:?}", params)
}
fn init_resource(params: Option<serde_json::Value>) -> Result<Self::Resource, String> {
// Load model (can be expensive, but only happens once per unique params)
Ok(MyModelResource { model_data: vec![0.0; 1000] })
}
}Required Associated Types§
Required Methods§
Sourcefn compute_resource_key(params: Option<&Value>) -> String
fn compute_resource_key(params: Option<&Value>) -> String
Compute a cache key from parameters.
This should hash only the parameters that affect resource initialization (e.g., model path, GPU device ID). Different parameters that produce the same key will share the same cached resource.
Sourcefn init_resource(params: Option<Value>) -> Result<Self::Resource, String>
fn init_resource(params: Option<Value>) -> Result<Self::Resource, String>
Initialize/load the resource.
This is called once per unique cache key. The result is cached and shared across all node instances with matching parameters.
§Errors
Returns an error if resource initialization fails (e.g., model file not found, GPU initialization error).
§Note
This method may be called from a blocking thread pool to avoid blocking async execution during model loading.
Provided Methods§
Sourcefn deinit_resource(_resource: Self::Resource)
fn deinit_resource(_resource: Self::Resource)
Optional cleanup when the resource is being unloaded.
This is called when the last reference to the resource is dropped (typically during plugin unload or LRU eviction).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.