pub struct HandlerRegistry { /* private fields */ }Expand description
Zero-overhead handler registry with O(1) average-case lookup.
The registry uses FxHash (2x faster than SipHash for small keys) for efficient handler dispatch. It provides type-safe handler registration and JSON-based dispatch.
§Performance
- Lookup: O(1) average-case using FxHash
- Dispatch: <1μs target (hot path)
- Memory: ~256 bytes per registered handler
§Examples
use pforge_runtime::{Handler, HandlerRegistry, Result};
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
#[derive(Debug, Deserialize, JsonSchema)]
struct Input { value: i32 }
#[derive(Debug, Serialize, JsonSchema)]
struct Output { result: i32 }
struct DoubleHandler;
#[async_trait::async_trait]
impl Handler for DoubleHandler {
type Input = Input;
type Output = Output;
type Error = pforge_runtime::Error;
async fn handle(&self, input: Self::Input) -> Result<Self::Output> {
Ok(Output { result: input.value * 2 })
}
}
let mut registry = HandlerRegistry::new();
registry.register("double", DoubleHandler);
// Dispatch with JSON
let input = serde_json::json!({"value": 21});
let result_bytes = registry.dispatch("double", &serde_json::to_vec(&input)?).await?;
let result: serde_json::Value = serde_json::from_slice(&result_bytes)?;
assert_eq!(result["result"], 42);Implementations§
Source§impl HandlerRegistry
impl HandlerRegistry
Sourcepub fn register<H>(&mut self, name: impl Into<String>, handler: H)
pub fn register<H>(&mut self, name: impl Into<String>, handler: H)
Register a handler with a name
Sourcepub fn has_handler(&self, name: &str) -> bool
pub fn has_handler(&self, name: &str) -> bool
Check if handler exists
Sourcepub async fn dispatch(&self, tool: &str, params: &[u8]) -> Result<Vec<u8>>
pub async fn dispatch(&self, tool: &str, params: &[u8]) -> Result<Vec<u8>>
Dispatch to a handler by name
Sourcepub fn get_input_schema(&self, tool: &str) -> Option<RootSchema>
pub fn get_input_schema(&self, tool: &str) -> Option<RootSchema>
Get input schema for a tool
Sourcepub fn get_output_schema(&self, tool: &str) -> Option<RootSchema>
pub fn get_output_schema(&self, tool: &str) -> Option<RootSchema>
Get output schema for a tool
Trait Implementations§
Auto Trait Implementations§
impl Freeze for HandlerRegistry
impl !RefUnwindSafe for HandlerRegistry
impl Send for HandlerRegistry
impl Sync for HandlerRegistry
impl Unpin for HandlerRegistry
impl !UnwindSafe for HandlerRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more