@@ -2,10 +2,17 @@ use std::{fmt::Debug, ops::Deref, sync::Arc};
2
2
3
3
use crate :: protocol:: { Publish , PublishProperties } ;
4
4
5
+ /// Filter for [`Publish`] packets
5
6
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
6
12
fn filter ( & self , packet : & mut Publish , properties : Option < & mut PublishProperties > ) -> bool ;
7
13
}
8
14
15
+ /// Container for either an owned [`PublishFilter`] or an `'static` reference
9
16
#[ derive( Clone ) ]
10
17
pub enum PublishFilterRef {
11
18
Owned ( Arc < dyn PublishFilter + Send + Sync > ) ,
@@ -15,8 +22,8 @@ pub enum PublishFilterRef {
15
22
impl Debug for PublishFilterRef {
16
23
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
17
24
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 ( ) ,
20
27
}
21
28
}
22
29
}
@@ -32,6 +39,7 @@ impl Deref for PublishFilterRef {
32
39
}
33
40
}
34
41
42
+ /// Implements [`PublishFilter`] for any ordinary function
35
43
impl < F > PublishFilter for F
36
44
where
37
45
F : Fn ( & mut Publish , Option < & mut PublishProperties > ) -> bool + Send + Sync ,
41
49
}
42
50
}
43
51
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
+ /// ```
44
62
impl < F > From < & ' static F > for PublishFilterRef
45
63
where
46
64
F : Fn ( & mut Publish , Option < & mut PublishProperties > ) -> bool + Send + Sync ,
@@ -50,34 +68,52 @@ where
50
68
}
51
69
}
52
70
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
54
89
where
55
90
T : PublishFilter + ' static + Send + Sync ,
56
91
{
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)
59
94
}
60
95
}
61
- impl < T > From < Arc < T > > for PublishFilterRef
96
+
97
+ impl < T > From < Box < T > > for PublishFilterRef
62
98
where
63
99
T : PublishFilter + ' static + Send + Sync ,
64
100
{
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) )
67
103
}
68
104
}
69
105
70
106
#[ cfg( test) ]
71
107
mod tests {
72
108
use super :: * ;
73
109
74
- fn filter_static ( packet : & mut Publish , properties : Option < & mut PublishProperties > ) -> bool {
110
+ fn filter_static ( _packet : & mut Publish , _properties : Option < & mut PublishProperties > ) -> bool {
75
111
true
76
112
}
77
113
struct Prejudiced ( bool ) ;
78
114
79
115
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 {
81
117
self . 0
82
118
}
83
119
}
0 commit comments