1
#[cfg(unix)]
2
pub mod unix;
3
#[cfg(unix)]
4
pub use unix::PacketStream;
5

            
6
#[cfg(windows)]
7
pub mod windows;
8
#[cfg(windows)]
9
pub use windows::PacketStream;
10

            
11
use crate::{
12
    Error,
13
    capture::{Activated, Capture},
14
    codec::PacketCodec,
15
};
16

            
17
impl<T: Activated + ?Sized> Capture<T> {
18
    /// Returns this capture as a [`futures::Stream`] of packets.
19
    ///
20
    /// # Errors
21
    ///
22
    /// If this capture is set to be blocking, or if the network device
23
    /// does not support `select()`, an error will be returned.
24
4
    pub fn stream<C: PacketCodec>(self, codec: C) -> Result<PacketStream<T, C>, Error> {
25
4
        if !self.is_nonblock() {
26
2
            return Err(Error::NonNonBlock);
27
2
        }
28
2
        PacketStream::new(self, codec)
29
4
    }
30
}
31

            
32
#[cfg(test)]
33
mod tests {
34
    use crate::{
35
        capture::{Active, testmod::test_capture},
36
        codec::testmod::Codec,
37
        raw::testmod::{RAWMTX, as_pcap_t},
38
    };
39

            
40
    use super::PacketStream;
41

            
42
    #[test]
43
    fn test_stream_error() {
44
        let _m = RAWMTX.lock();
45

            
46
        let mut dummy: isize = 777;
47
        let pcap = as_pcap_t(&mut dummy);
48

            
49
        let test_capture = test_capture::<Active>(pcap);
50
        let capture = test_capture.capture;
51
        assert!(!capture.is_nonblock());
52

            
53
        let result = capture.stream(Codec);
54
        assert!(result.is_err());
55
    }
56

            
57
    // On Windows the stream drives an event HANDLE, which is a raw pointer and so not Send on
58
    // its own. Callers hand the stream to an executor, so make sure it stays Send.
59
    #[test]
60
    fn test_stream_is_send() {
61
        fn assert_send<T: Send>() {}
62
        assert_send::<PacketStream<Active, Codec>>();
63
    }
64
}