pub struct HookManager { /* private fields */ }Expand description
钩子管理器
专门管理运行时回调列表。
§设计理由(v1.2.1)
- Config vs Context 分离:
PipelineConfig应该是 POD(Plain Old Data),用于序列化PiperContext管理运行时状态和动态组件(如回调)
§线程安全
使用 std::sync::Arc 确保回调可以跨线程共享。
回调列表本身不是线程安全的,需要外部同步(通常通过 RwLock<HookManager>)。
§示例
use piper_driver::hooks::{HookManager, FrameCallback};
use piper_driver::recording::AsyncRecordingHook;
use piper_protocol::PiperFrame;
use std::sync::{Arc, RwLock};
// 在 PiperContext 中
pub struct PiperContext {
pub hooks: RwLock<HookManager>,
}
// 创建上下文并添加回调
let context = PiperContext { hooks: RwLock::new(HookManager::new()) };
let (hook, _rx) = AsyncRecordingHook::new();
let callback = Arc::new(hook) as Arc<dyn FrameCallback>;
if let Ok(mut hooks) = context.hooks.write() {
hooks.add_callback(callback);
}
// 触发回调(在 rx_loop 中)
let frame = PiperFrame::new_standard(0x251, &[1, 2, 3, 4]);
if let Ok(hooks) = context.hooks.read() {
hooks.trigger_all(&frame);
}Implementations§
Source§impl HookManager
impl HookManager
Sourcepub const fn new() -> HookManager
pub const fn new() -> HookManager
创建新的钩子管理器
Sourcepub fn add_callback(&mut self, callback: Arc<dyn FrameCallback>)
pub fn add_callback(&mut self, callback: Arc<dyn FrameCallback>)
添加回调
§线程安全
此方法不是线程安全的,需要外部同步(通常通过 RwLock)。
§参数
callback: 要添加的回调(必须实现FrameCallback)
§示例
use piper_driver::hooks::{HookManager, FrameCallback};
use piper_driver::recording::AsyncRecordingHook;
use std::sync::Arc;
let mut hooks = HookManager::new();
let (hook, _rx) = AsyncRecordingHook::new();
let callback = Arc::new(hook) as Arc<dyn FrameCallback>;
hooks.add_callback(callback);Sourcepub fn trigger_all(&self, frame: &PiperFrame)
pub fn trigger_all(&self, frame: &PiperFrame)
触发所有回调(在 rx_loop 中调用)
§性能要求
- 总耗时 <1μs(假设每个回调 <100ns)
- 非阻塞:所有回调必须使用
try_send而非send
§参数
frame: 接收到的 CAN 帧
§示例
use piper_driver::hooks::HookManager;
use piper_driver::recording::AsyncRecordingHook;
use piper_protocol::PiperFrame;
use std::sync::Arc;
let mut hooks = HookManager::new();
let (hook, _rx) = AsyncRecordingHook::new();
hooks.add_callback(Arc::new(hook));
// 在 rx_loop 中触发
let frame = PiperFrame::new_standard(0x251, &[1, 2, 3, 4]);
hooks.trigger_all(&frame);Sourcepub fn trigger_all_sent(&self, frame: &PiperFrame)
pub fn trigger_all_sent(&self, frame: &PiperFrame)
触发所有 TX 回调(在 tx_loop 发送成功后调用)
§时机
仅在 tx.send() 成功后调用,确保录制的是实际发送的帧。
§参数
frame: 成功发送的 CAN 帧
§示例
use piper_driver::hooks::HookManager;
use piper_protocol::PiperFrame;
let hooks = HookManager::new();
// 在 tx_loop 中,发送成功后触发回调
let frame = PiperFrame::new_standard(0x123, &[1, 2, 3, 4]);
// 假设 tx.send(&frame) 返回 Ok(())
hooks.trigger_all_sent(&frame);Trait Implementations§
Source§impl Default for HookManager
impl Default for HookManager
Source§fn default() -> HookManager
fn default() -> HookManager
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for HookManager
impl !RefUnwindSafe for HookManager
impl Send for HookManager
impl Sync for HookManager
impl Unpin for HookManager
impl !UnwindSafe for HookManager
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more