if_watch/
lib.rs

1//! IP address watching.
2#![deny(missing_docs)]
3#![deny(warnings)]
4
5pub use ipnet::{IpNet, Ipv4Net, Ipv6Net};
6
7#[cfg(target_os = "macos")]
8mod apple;
9#[cfg(target_os = "ios")]
10mod apple;
11#[cfg(not(any(
12    target_os = "ios",
13    target_os = "linux",
14    target_os = "macos",
15    target_os = "windows",
16)))]
17mod fallback;
18#[cfg(target_os = "linux")]
19mod linux;
20#[cfg(target_os = "windows")]
21mod win;
22
23#[cfg(any(target_os = "macos", target_os = "ios"))]
24#[cfg(feature = "tokio")]
25pub use apple::tokio;
26
27#[cfg(any(target_os = "macos", target_os = "ios"))]
28#[cfg(feature = "smol")]
29pub use apple::smol;
30
31#[cfg(feature = "smol")]
32#[cfg(not(any(
33    target_os = "ios",
34    target_os = "linux",
35    target_os = "macos",
36    target_os = "windows",
37)))]
38pub use fallback::smol;
39
40#[cfg(feature = "tokio")]
41#[cfg(not(any(
42    target_os = "ios",
43    target_os = "linux",
44    target_os = "macos",
45    target_os = "windows",
46)))]
47pub use fallback::tokio;
48
49#[cfg(target_os = "windows")]
50#[cfg(feature = "tokio")]
51pub use win::tokio;
52
53#[cfg(target_os = "windows")]
54#[cfg(feature = "smol")]
55pub use win::smol;
56
57#[cfg(target_os = "linux")]
58#[cfg(feature = "tokio")]
59pub use linux::tokio;
60
61#[cfg(target_os = "linux")]
62#[cfg(feature = "smol")]
63pub use linux::smol;
64
65/// An address change event.
66#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
67pub enum IfEvent {
68    /// A new local address has been added.
69    Up(IpNet),
70    /// A local address has been deleted.
71    Down(IpNet),
72}
73
74#[cfg(test)]
75mod tests {
76    use futures::StreamExt;
77    use std::pin::Pin;
78
79    #[test]
80    fn test_smol_ip_watch() {
81        use super::smol::IfWatcher;
82
83        smol::block_on(async {
84            let mut set = IfWatcher::new().unwrap();
85            let event = set.select_next_some().await.unwrap();
86            println!("Got event {:?}", event);
87        });
88    }
89
90    #[tokio::test]
91    async fn test_tokio_ip_watch() {
92        use super::tokio::IfWatcher;
93
94        let mut set = IfWatcher::new().unwrap();
95        let event = set.select_next_some().await.unwrap();
96        println!("Got event {:?}", event);
97    }
98
99    #[test]
100    fn test_smol_is_send() {
101        use super::smol::IfWatcher;
102
103        smol::block_on(async {
104            fn is_send<T: Send>(_: T) {}
105            is_send(IfWatcher::new());
106            is_send(IfWatcher::new().unwrap());
107            is_send(Pin::new(&mut IfWatcher::new().unwrap()));
108        });
109    }
110
111    #[tokio::test]
112    async fn test_tokio_is_send() {
113        use super::tokio::IfWatcher;
114
115        fn is_send<T: Send>(_: T) {}
116        is_send(IfWatcher::new());
117        is_send(IfWatcher::new().unwrap());
118        is_send(Pin::new(&mut IfWatcher::new().unwrap()));
119    }
120}