Skip to main content

HookManager

Struct HookManager 

Source
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

Source

pub const fn new() -> HookManager

创建新的钩子管理器

Source

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);
Source

pub fn clear(&mut self)

移除所有回调

§用途

主要用于测试或清理场景。

Source

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);
Source

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);
Source

pub fn len(&self) -> usize

获取回调数量

§用途

主要用于调试和监控。

Source

pub fn is_empty(&self) -> bool

检查是否为空

Trait Implementations§

Source§

impl Default for HookManager

Source§

fn default() -> HookManager

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more