@@ -2,10 +2,17 @@ use std::{fmt::Debug, ops::Deref, sync::Arc};
22
33use crate :: protocol:: { Publish , PublishProperties } ;
44
5+ /// Filter for [`Publish`] packets
56pub 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 ) ]
1017pub enum PublishFilterRef {
1118 Owned ( Arc < dyn PublishFilter + Send + Sync > ) ,
@@ -15,8 +22,8 @@ pub enum PublishFilterRef {
1522impl 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
3543impl < F > PublishFilter for F
3644where
3745 F : Fn ( & mut Publish , Option < & mut PublishProperties > ) -> bool + Send + Sync ,
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+ /// ```
4462impl < F > From < & ' static F > for PublishFilterRef
4563where
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
5489where
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
6298where
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) ]
71107mod 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