Skip to main content

canlink_hal/filter/
traits.rs

1//! `MessageFilter` trait definition (FR-005)
2//!
3//! Defines the interface for message filtering.
4
5use crate::message::CanMessage;
6
7/// Message filter trait
8///
9/// All filter implementations must implement this trait.
10/// Filters can be either hardware-accelerated or software-based.
11///
12/// # Thread Safety
13///
14/// Implementations must be `Send + Sync` to allow use across threads.
15///
16/// # Example
17///
18/// ```rust,ignore
19/// use canlink_hal::filter::MessageFilter;
20/// use canlink_hal::message::CanMessage;
21///
22/// struct MyFilter {
23///     target_id: u32,
24/// }
25///
26/// impl MessageFilter for MyFilter {
27///     fn matches(&self, message: &CanMessage) -> bool {
28///         message.id() == self.target_id
29///     }
30/// }
31/// ```
32pub trait MessageFilter: Send + Sync {
33    /// Check if a message matches this filter
34    ///
35    /// Returns `true` if the message should be accepted.
36    fn matches(&self, message: &CanMessage) -> bool;
37
38    /// Get the filter priority
39    ///
40    /// Higher priority filters are evaluated first.
41    /// Default is 0 (lowest priority).
42    fn priority(&self) -> u32 {
43        0
44    }
45
46    /// Check if this is a hardware filter
47    ///
48    /// Hardware filters are executed by the CAN controller,
49    /// reducing CPU load. Returns `false` by default.
50    fn is_hardware(&self) -> bool {
51        false
52    }
53}