if_watch/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! IP address watching.
#![deny(missing_docs)]
#![deny(warnings)]

pub use ipnet::{IpNet, Ipv4Net, Ipv6Net};

#[cfg(target_os = "macos")]
mod apple;
#[cfg(target_os = "ios")]
mod apple;
#[cfg(not(any(
    target_os = "ios",
    target_os = "linux",
    target_os = "macos",
    target_os = "windows",
)))]
mod fallback;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "windows")]
mod win;

#[cfg(any(target_os = "macos", target_os = "ios"))]
#[cfg(feature = "tokio")]
pub use apple::tokio;

#[cfg(any(target_os = "macos", target_os = "ios"))]
#[cfg(feature = "smol")]
pub use apple::smol;

#[cfg(feature = "smol")]
#[cfg(not(any(
    target_os = "ios",
    target_os = "linux",
    target_os = "macos",
    target_os = "windows",
)))]
pub use fallback::smol;

#[cfg(feature = "tokio")]
#[cfg(not(any(
    target_os = "ios",
    target_os = "linux",
    target_os = "macos",
    target_os = "windows",
)))]
pub use fallback::tokio;

#[cfg(target_os = "windows")]
#[cfg(feature = "tokio")]
pub use win::tokio;

#[cfg(target_os = "windows")]
#[cfg(feature = "smol")]
pub use win::smol;

#[cfg(target_os = "linux")]
#[cfg(feature = "tokio")]
pub use linux::tokio;

#[cfg(target_os = "linux")]
#[cfg(feature = "smol")]
pub use linux::smol;

/// An address change event.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum IfEvent {
    /// A new local address has been added.
    Up(IpNet),
    /// A local address has been deleted.
    Down(IpNet),
}

#[cfg(test)]
mod tests {
    use futures::StreamExt;
    use std::pin::Pin;

    #[test]
    fn test_smol_ip_watch() {
        use super::smol::IfWatcher;

        smol::block_on(async {
            let mut set = IfWatcher::new().unwrap();
            let event = set.select_next_some().await.unwrap();
            println!("Got event {:?}", event);
        });
    }

    #[tokio::test]
    async fn test_tokio_ip_watch() {
        use super::tokio::IfWatcher;

        let mut set = IfWatcher::new().unwrap();
        let event = set.select_next_some().await.unwrap();
        println!("Got event {:?}", event);
    }

    #[test]
    fn test_smol_is_send() {
        use super::smol::IfWatcher;

        smol::block_on(async {
            fn is_send<T: Send>(_: T) {}
            is_send(IfWatcher::new());
            is_send(IfWatcher::new().unwrap());
            is_send(Pin::new(&mut IfWatcher::new().unwrap()));
        });
    }

    #[tokio::test]
    async fn test_tokio_is_send() {
        use super::tokio::IfWatcher;

        fn is_send<T: Send>(_: T) {}
        is_send(IfWatcher::new());
        is_send(IfWatcher::new().unwrap());
        is_send(Pin::new(&mut IfWatcher::new().unwrap()));
    }
}