Skip to content

Commit c909271

Browse files
committed
docs: document filter
1 parent 64a8540 commit c909271

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

rumqttd/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::pin::Pin;
66
use std::sync::Arc;
77
use std::{collections::HashMap, path::Path};
88

9-
use router::PublishFilterRef;
109
use serde::{Deserialize, Serialize};
1110
use tracing_subscriber::{
1211
filter::EnvFilter,
@@ -22,7 +21,7 @@ use tracing_subscriber::{
2221
pub use link::alerts;
2322
pub use link::local;
2423
pub use link::meters;
25-
pub use router::{Alert, IncomingMeter, Meter, Notification, OutgoingMeter};
24+
pub use router::{Alert, IncomingMeter, Meter, Notification, OutgoingMeter, PublishFilter, PublishFilterRef};
2625
use segments::Storage;
2726
pub use server::Broker;
2827

rumqttd/src/router/filter.rs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ use std::{fmt::Debug, ops::Deref, sync::Arc};
22

33
use crate::protocol::{Publish, PublishProperties};
44

5+
/// Filter for [`Publish`] packets
56
pub trait PublishFilter {
7+
/// Determines weather an [`Publish`] packet should be processed
8+
/// Arguments:
9+
/// * `packet`: to be published, may be modified if necessary
10+
/// * `properties`: received along with the packet, may be `None` for older MQTT versions
11+
/// Returns: [`bool`] indicating if the packet should be processed
612
fn filter(&self, packet: &mut Publish, properties: Option<&mut PublishProperties>) -> bool;
713
}
814

15+
/// Container for either an owned [`PublishFilter`] or an `'static` reference
916
#[derive(Clone)]
1017
pub enum PublishFilterRef {
1118
Owned(Arc<dyn PublishFilter + Send + Sync>),
@@ -15,8 +22,8 @@ pub enum PublishFilterRef {
1522
impl Debug for PublishFilterRef {
1623
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1724
match self {
18-
Self::Owned(arg0) => f.debug_tuple("Owned").finish(),
19-
Self::Static(arg0) => f.debug_tuple("Static").finish(),
25+
Self::Owned(_arg0) => f.debug_tuple("Owned").finish(),
26+
Self::Static(_arg0) => f.debug_tuple("Static").finish(),
2027
}
2128
}
2229
}
@@ -32,6 +39,7 @@ impl Deref for PublishFilterRef {
3239
}
3340
}
3441

42+
/// Implements [`PublishFilter`] for any ordinary function
3543
impl<F> PublishFilter for F
3644
where
3745
F: Fn(&mut Publish, Option<&mut PublishProperties>) -> bool + Send + Sync,
@@ -41,6 +49,16 @@ where
4149
}
4250
}
4351

52+
/// Implements the conversion
53+
/// ```rust
54+
/// # use rumqttd::{protocol::{Publish, PublishProperties}, PublishFilterRef};
55+
/// fn filter_static(packet: &mut Publish, properties: Option<&mut PublishProperties>) -> bool {
56+
/// todo!()
57+
/// }
58+
///
59+
/// let filter = PublishFilterRef::from(&filter_static);
60+
/// # assert!(matches!(filter, PublishFilterRef::Static(_)));
61+
/// ```
4462
impl<F> From<&'static F> for PublishFilterRef
4563
where
4664
F: Fn(&mut Publish, Option<&mut PublishProperties>) -> bool + Send + Sync,
@@ -50,34 +68,52 @@ where
5068
}
5169
}
5270

53-
impl<T> From<Box<T>> for PublishFilterRef
71+
/// Implements the conversion
72+
/// ```rust
73+
/// # use std::boxed::Box;
74+
/// # use rumqttd::{protocol::{Publish, PublishProperties}, PublishFilter, PublishFilterRef};
75+
/// #[derive(Clone)]
76+
/// struct MyFilter {}
77+
///
78+
/// impl PublishFilter for MyFilter {
79+
/// fn filter(&self, packet: &mut Publish, properties: Option<&mut PublishProperties>) -> bool {
80+
/// todo!()
81+
/// }
82+
/// }
83+
/// let boxed: Box<MyFilter> = Box::new(MyFilter {});
84+
///
85+
/// let filter = PublishFilterRef::from(boxed);
86+
/// # assert!(matches!(filter, PublishFilterRef::Owned(_)));
87+
/// ```
88+
impl<T> From<Arc<T>> for PublishFilterRef
5489
where
5590
T: PublishFilter + 'static + Send + Sync,
5691
{
57-
fn from(value: Box<T>) -> Self {
58-
Self::Owned(Arc::<T>::from(value))
92+
fn from(value: Arc<T>) -> Self {
93+
Self::Owned(value)
5994
}
6095
}
61-
impl<T> From<Arc<T>> for PublishFilterRef
96+
97+
impl<T> From<Box<T>> for PublishFilterRef
6298
where
6399
T: PublishFilter + 'static + Send + Sync,
64100
{
65-
fn from(value: Arc<T>) -> Self {
66-
Self::Owned(value)
101+
fn from(value: Box<T>) -> Self {
102+
Self::Owned(Arc::<T>::from(value))
67103
}
68104
}
69105

70106
#[cfg(test)]
71107
mod tests {
72108
use super::*;
73109

74-
fn filter_static(packet: &mut Publish, properties: Option<&mut PublishProperties>) -> bool {
110+
fn filter_static(_packet: &mut Publish, _properties: Option<&mut PublishProperties>) -> bool {
75111
true
76112
}
77113
struct Prejudiced(bool);
78114

79115
impl PublishFilter for Prejudiced {
80-
fn filter(&self, packet: &mut Publish, properties: Option<&mut PublishProperties>) -> bool {
116+
fn filter(&self, _packet: &mut Publish,_propertiess: Option<&mut PublishProperties>) -> bool {
81117
self.0
82118
}
83119
}

0 commit comments

Comments
 (0)