Skip to content

Commit 63237d8

Browse files
committed
Review changes
1 parent 644fba8 commit 63237d8

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/handle.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use log::*;
44
use pcap_sys::{pcap_fileno, pcap_set_immediate_mode};
55
use std::os::raw::c_int;
66
use std::path::Path;
7+
use std::sync::atomic::{AtomicBool, Ordering};
78

89
fn compile_bpf(handle: *mut pcap_sys::pcap_t, bpf: &str) -> Result<Bpf, Error> {
910
let mut bpf_program = pcap_sys::bpf_program {
@@ -187,7 +188,7 @@ impl PendingHandle {
187188
let h = Handle {
188189
handle: self.handle,
189190
live_capture: self.live_capture,
190-
interrupted: std::sync::Arc::new(std::sync::Mutex::new(false)),
191+
interrupted: AtomicBool::new(false),
191192
};
192193
if self.live_capture {
193194
if 0 != unsafe { pcap_sys::pcap_activate(h.handle) } {
@@ -226,7 +227,7 @@ impl std::convert::TryFrom<&Config> for PendingHandle {
226227
pub struct Handle {
227228
handle: *mut pcap_sys::pcap_t,
228229
live_capture: bool,
229-
interrupted: std::sync::Arc<std::sync::Mutex<bool>>,
230+
interrupted: AtomicBool,
230231
}
231232

232233
unsafe impl Send for Handle {}
@@ -258,28 +259,19 @@ impl Handle {
258259
}
259260

260261
pub fn interrupted(&self) -> bool {
261-
self.interrupted.lock().map(|l| *l).unwrap_or(true)
262+
self.interrupted.load(Ordering::Relaxed)
262263
}
263264

264265
pub fn interrupt(&self) {
265-
let interrupted = self
266-
.interrupted
267-
.lock()
268-
.map(|mut l| {
269-
*l = true;
270-
false
271-
})
272-
.unwrap_or(true);
266+
let interrupted = self.interrupted.swap(true, Ordering::Relaxed);
273267
if !interrupted {
274268
unsafe {
275269
pcap_sys::pcap_breakloop(self.handle);
276270
}
277271
}
278272
}
279273

280-
pub fn set_bpf(self, bpf: Bpf) -> Result<Self, Error> {
281-
let mut bpf = bpf;
282-
274+
pub fn set_bpf(self, mut bpf: Bpf) -> Result<Self, Error> {
283275
let ret_code = unsafe { pcap_sys::pcap_setfilter(self.handle, bpf.inner_mut()) };
284276
if ret_code != 0 {
285277
return Err(pcap_util::convert_libpcap_error(self.handle));

src/stream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ mod tests {
247247

248248
assert!(
249249
stream.is_ok(),
250-
format!("Could not build stream {}", stream.err().unwrap())
250+
format!("Could not build stream {:?}", stream.err().unwrap())
251251
);
252252

253253
let mut stream = stream.unwrap();
@@ -272,7 +272,7 @@ mod tests {
272272

273273
assert!(
274274
stream.is_ok(),
275-
format!("Could not build stream {}", stream.err().unwrap())
275+
format!("Could not build stream {:?}", stream.err().unwrap())
276276
);
277277
}
278278
}

0 commit comments

Comments
 (0)