Skip to content

Commit

Permalink
Revert "Support wsl. (libp2p#9)"
Browse files Browse the repository at this point in the history
This reverts commit 9eb63c1.
  • Loading branch information
dvc94ch committed Apr 21, 2021
1 parent d109af5 commit f4e4c37
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ description = "crossplatform asynchronous network watcher"
repository = "https://github.com/dvc94ch/if-watch"

[dependencies]
async-io = "1.3.1"
futures-lite = "1.11.3"
if-addrs = "0.6.5" # used by fallback and windows
ipnet = "2.3.0"
libc = "0.2.86"
log = "0.4.14"

[target.'cfg(not(windows))'.dependencies]
async-io = "1.3.1"

[target.'cfg(not(target_os = "linux"))'.dependencies]
if-addrs = "0.6.5"

[target.'cfg(windows)'.dependencies]
futures = { version = "0.3.13", default-features = false }
winapi = { version = "0.3.9", features = ["netioapi", "ntdef", "winerror", "ws2def"] }
Expand Down
6 changes: 3 additions & 3 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use async_io::Timer;
use futures_lite::Stream;
use if_addrs::IfAddr;
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use std::io::Result;
use std::{
collections::{hash_set::Iter, HashSet, VecDeque},
collections::{HashSet, VecDeque},
future::Future,
io::Result,
pin::Pin,
task::{Context, Poll},
time::{Duration, Instant},
Expand Down Expand Up @@ -51,7 +51,7 @@ impl IfWatcher {
Ok(())
}

pub fn iter(&self) -> Iter<'_, IpNet> {
pub fn iter(&self) -> impl Iterator<Item = &IpNet> {
self.addrs.iter()
}
}
Expand Down
27 changes: 8 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use std::io::Result;
use std::pin::Pin;
use std::task::{Context, Poll};

#[cfg(not(any(unix, windows)))]
compile_error!("Only Unix and Windows are supported");

#[cfg(not(any(target_os = "linux", windows)))]
mod fallback;
#[cfg(target_os = "linux")]
mod unix;
Expand All @@ -32,40 +36,25 @@ pub enum IfEvent {

/// Watches for interface changes.
#[derive(Debug)]
pub enum IfWatcher {
/// Uses platform api.
Platform(platform_impl::IfWatcher),
/// Polling fallback.
Fallback(fallback::IfWatcher),
}
pub struct IfWatcher(platform_impl::IfWatcher);

impl IfWatcher {
/// Create a watcher
pub async fn new() -> Result<Self> {
if std::env::var_os("WSL_DISTRO_NAME").is_none() {
Ok(Self::Platform(platform_impl::IfWatcher::new().await?))
} else {
Ok(Self::Fallback(fallback::IfWatcher::new().await?))
}
Ok(Self(platform_impl::IfWatcher::new().await?))
}

/// Iterate over current networks.
pub fn iter(&self) -> impl Iterator<Item = &IpNet> {
match self {
Self::Platform(watcher) => watcher.iter(),
Self::Fallback(watcher) => watcher.iter(),
}
self.0.iter()
}
}

impl Future for IfWatcher {
type Output = Result<IfEvent>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
match &mut *self {
Self::Platform(watcher) => Pin::new(watcher).poll(cx),
Self::Fallback(watcher) => Pin::new(watcher).poll(cx),
}
Pin::new(&mut self.0).poll(cx)
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ use crate::IfEvent;
use async_io::Async;
use futures_lite::Future;
use ipnet::IpNet;
use std::{
collections::{hash_set::Iter, HashSet, VecDeque},
io::Result,
os::unix::prelude::*,
pin::Pin,
task::{Context, Poll},
};
use std::collections::{HashSet, VecDeque};
use std::io::Result;
use std::os::unix::prelude::*;
use std::pin::Pin;
use std::task::{Context, Poll};

mod aligned_buffer;

Expand Down Expand Up @@ -85,7 +83,7 @@ impl IfWatcher {
}

/// Returns an iterator of ip's.
pub fn iter(&self) -> Iter<'_, IpNet> {
pub fn iter(&self) -> impl Iterator<Item = &IpNet> {
self.addrs.iter()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use futures::task::AtomicWaker;
use if_addrs::IfAddr;
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use std::{
collections::{hash_set::Iter, HashSet, VecDeque},
collections::{HashSet, VecDeque},
future::Future,
io::Result,
pin::Pin,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl IfWatcher {
Ok(())
}

pub fn iter(&self) -> Iter<'_, IpNet> {
pub fn iter(&self) -> impl Iterator<Item = &IpNet> {
self.addrs.iter()
}
}
Expand Down

0 comments on commit f4e4c37

Please sign in to comment.