@@ -202,13 +202,14 @@ impl<T: Activated + ?Sized> Capture<T> {
202202 PacketIter :: new ( self , codec)
203203 }
204204
205- pub fn for_each < F > ( & mut self , count : Option < usize > , handler : F )
205+ pub fn for_each < F > ( & mut self , count : Option < usize > , handler : F ) -> Result < ( ) , Error >
206206 where
207207 F : FnMut ( Packet ) ,
208208 {
209209 let cnt = match count {
210210 // Actually passing 0 down to pcap_loop would mean read forever.
211- Some ( 0 ) => return ,
211+ // We interpret it as "read nothing", so we just succeed immediately.
212+ Some ( 0 ) => return Ok ( ( ) ) ,
212213 Some ( cnt) => cnt
213214 . try_into ( )
214215 . expect ( "count of packets to read cannot exceed c_int::MAX" ) ,
@@ -220,7 +221,7 @@ impl<T: Activated + ?Sized> Capture<T> {
220221 panic_payload : None ,
221222 handle : self . handle ,
222223 } ;
223- unsafe {
224+ let return_code = unsafe {
224225 raw:: pcap_loop (
225226 self . handle . as_ptr ( ) ,
226227 cnt,
@@ -231,6 +232,7 @@ impl<T: Activated + ?Sized> Capture<T> {
231232 if let Some ( e) = handler. panic_payload {
232233 resume_unwind ( e) ;
233234 }
235+ self . check_err ( return_code == 0 )
234236 }
235237
236238 /// Compiles the string into a filter program using `pcap_compile`.
@@ -1022,9 +1024,11 @@ mod tests {
10221024 } ) ;
10231025
10241026 let mut packets = 0 ;
1025- capture. for_each ( None , |_| {
1026- packets += 1 ;
1027- } ) ;
1027+ capture
1028+ . for_each ( None , |_| {
1029+ packets += 1 ;
1030+ } )
1031+ . unwrap ( ) ;
10281032 assert_eq ! ( packets, 1 ) ;
10291033 }
10301034
@@ -1061,7 +1065,9 @@ mod tests {
10611065 . withf_st ( move |arg1| * arg1 == pcap)
10621066 . return_once_st ( move |_| { } ) ;
10631067
1064- capture. for_each ( None , |_| panic ! ( "panic in callback" ) ) ;
1068+ capture
1069+ . for_each ( None , |_| panic ! ( "panic in callback" ) )
1070+ . unwrap ( ) ;
10651071 }
10661072
10671073 #[ test]
@@ -1093,9 +1099,11 @@ mod tests {
10931099 } ) ;
10941100
10951101 let mut packets = 0 ;
1096- capture. for_each ( Some ( 2 ) , |_| {
1097- packets += 1 ;
1098- } ) ;
1102+ capture
1103+ . for_each ( Some ( 2 ) , |_| {
1104+ packets += 1 ;
1105+ } )
1106+ . unwrap ( ) ;
10991107 assert_eq ! ( packets, 2 ) ;
11001108 }
11011109
@@ -1110,9 +1118,11 @@ mod tests {
11101118 let mut capture: Capture < dyn Activated > = test_capture. capture . into ( ) ;
11111119
11121120 let mut packets = 0 ;
1113- capture. for_each ( Some ( 0 ) , |_| {
1114- packets += 1 ;
1115- } ) ;
1121+ capture
1122+ . for_each ( Some ( 0 ) , |_| {
1123+ packets += 1 ;
1124+ } )
1125+ . unwrap ( ) ;
11161126 assert_eq ! ( packets, 0 ) ;
11171127 }
11181128}
0 commit comments