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
    capture::{Activated, Capture},
13
    codec::PacketCodec,
14
    Error,
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
2
    pub fn stream<C: PacketCodec>(self, codec: C) -> Result<PacketStream<T, C>, Error> {
25
2
        if !self.is_nonblock() {
26
2
            return Err(Error::NonNonBlock);
27
        }
28
        PacketStream::new(self, codec)
29
2
    }
30
}
31

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

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

            
44
        let mut dummy: isize = 777;
45
        let pcap = as_pcap_t(&mut dummy);
46

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

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