From 605f750806697eff3bba764f1cbbce7db7bf2050 Mon Sep 17 00:00:00 2001 From: Hannes <55623006+umgefahren@users.noreply.github.com> Date: Mon, 5 Aug 2024 10:32:02 +0200 Subject: [PATCH] feat: Expand platform support (#43) * Better Apple support * Rename job * Correct spelling mistakes * Newer actions checkout * Proper and * Happy * Install corret CI * Build std * version bump * Show nightly only feature when building documentation * Update CI * Add toolchain * Apply automated improvements * Cast mut was a mistake * Speed up the CI * Well that should work * Version is too old * Add FreeBSD CI * Support NetBSD and FreeBSD * Add corret bsd flags * Add other BSD unix * Remove openbsd * Remove unnecessary targets --- .github/workflows/CI.yml | 57 +++----- .github/workflows/Cross.yml | 52 ++++--- Cargo.toml | 8 +- src/lib.rs | 142 ++++++++++++++++--- src/{posix_not_mac.rs => posix_not_apple.rs} | 0 5 files changed, 178 insertions(+), 81 deletions(-) rename src/{posix_not_mac.rs => posix_not_apple.rs} (100%) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index c9a48c5..66e154c 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -7,15 +7,12 @@ jobs: name: Check runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable with: - profile: minimal toolchain: stable - override: true - - uses: actions-rs/cargo@v1 - with: - command: check + - name: Run check + run: cargo check test: name: Test Suite @@ -25,49 +22,39 @@ jobs: os: [ubuntu-latest, macos-latest, windows-latest] rust: ["1.56.0", stable] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: maxim-lobanov/setup-xcode@v1 if: ${{ matrix.os == 'macos-latest' && matrix.rust == '1.56.0' }} with: xcode-version: latest-stable - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: ${{ matrix.rust }} - override: true + - uses: dtolnay/rust-toolchain@stable + if: ${{ matrix.rust == 'stable' }} + - uses: dtolnay/rust-toolchain@1.56.0 + if: ${{ matrix.rust == '1.56.0' }} - name: Build - uses: actions-rs/cargo@v1 - with: - command: build + run: cargo build - name: Test - uses: actions-rs/cargo@v1 - with: - command: test - args: -- --show-output + run: cargo test -- --show-output fmt: name: Rustfmt runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - run: rustup component add rustfmt - - uses: actions-rs/cargo@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable with: - command: fmt - args: --all -- --check + components: rustfmt + - name: Run fmt + run: cargo fmt --all -- --check clippy_check: name: Clippy Check runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - run: rustup component add clippy - - uses: actions-rs/clippy-check@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable with: - token: ${{ secrets.GITHUB_TOKEN }} - args: --all-features + components: clippy + - uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0 + - name: Run clippy + run: cargo clippy --all-features diff --git a/.github/workflows/Cross.yml b/.github/workflows/Cross.yml index eff948e..22f6c81 100644 --- a/.github/workflows/Cross.yml +++ b/.github/workflows/Cross.yml @@ -13,36 +13,44 @@ jobs: matrix: target: - aarch64-linux-android + - x86_64-unknown-freebsd + - x86_64-unknown-netbsd steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable with: toolchain: stable target: ${{ matrix.target }} - override: true - - uses: actions-rs/cargo@v1 - with: - use-cross: true - command: build - args: --target=${{ matrix.target }} + - name: Build + run: cargo build --target=${{ matrix.target }} - ios: - name: iOS Build + apple: + name: Apple Build on ${{ matrix.target }} runs-on: macos-latest strategy: matrix: - target: - - aarch64-apple-ios - - x86_64-apple-ios + include: + - target: aarch64-apple-ios + tier3: false + - target: aarch64-apple-watchos + tier3: true + - target: aarch64-apple-tvos + tier3: true + - target: aarch64-apple-visionos + tier3: true steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + if: ${{ !matrix.tier3 }} with: - toolchain: stable - target: ${{ matrix.target }} - override: true - - uses: actions-rs/cargo@v1 + toolchain: 'stable' + targets: ${{ matrix.target }} + components: rust-src + - uses: dtolnay/rust-toolchain@master + if: ${{ matrix.tier3 }} with: - use-cross: true - command: build - args: --target=${{ matrix.target }} + toolchain: 'nightly' + components: rust-src + + - name: Build + run: cargo build --target ${{ matrix.target }} ${{ matrix.tier3 && '-Zbuild-std' || '' }} diff --git a/Cargo.toml b/Cargo.toml index d229e3a..b6ffca0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ license = "MIT OR BSD-3-Clause" name = "if-addrs" readme = "README.md" repository = "https://github.com/messense/if-addrs" -version = "0.13.1" +version = "0.13.2" edition = "2021" [target.'cfg(not(target_os = "windows"))'.dependencies] @@ -25,3 +25,9 @@ features = [ [features] link-local = [] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] +targets = ["x86_64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "aarch64-apple-ios", "aarch64-apple-watchos", "aarch64-apple-tvos", "aarch64-apple-visionos"] +cargo-args = ["-Z", "build-std"] diff --git a/src/lib.rs b/src/lib.rs index 8eb6de7..b7a0ab2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,11 +6,28 @@ // modified, or distributed except according to those terms. Please review the Licences for the // specific language governing permissions and limitations relating to use of the SAFE Network // Software. +#![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![cfg_attr(docsrs, feature(doc_cfg))] #[cfg(not(windows))] mod posix; -#[cfg(all(not(windows), not(any(target_os = "macos", target_os = "ios"))))] -mod posix_not_mac; +#[cfg(all( + not(windows), + not(all( + target_vendor = "apple", + any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos" + ) + )), + not(target_os = "freebsd"), + not(target_os = "netbsd"), + not(target_os = "openbsd") +))] +mod posix_not_apple; mod sockaddr; #[cfg(windows)] mod windows; @@ -37,17 +54,20 @@ pub struct Interface { impl Interface { /// Check whether this is a loopback interface. - pub fn is_loopback(&self) -> bool { + #[must_use] + pub const fn is_loopback(&self) -> bool { self.addr.is_loopback() } /// Check whether this is a link local interface. - pub fn is_link_local(&self) -> bool { + #[must_use] + pub const fn is_link_local(&self) -> bool { self.addr.is_link_local() } /// Get the IP address of this interface. - pub fn ip(&self) -> IpAddr { + #[must_use] + pub const fn ip(&self) -> IpAddr { self.addr.ip() } } @@ -63,7 +83,8 @@ pub enum IfAddr { impl IfAddr { /// Check whether this is a loopback address. - pub fn is_loopback(&self) -> bool { + #[must_use] + pub const fn is_loopback(&self) -> bool { match *self { IfAddr::V4(ref ifv4_addr) => ifv4_addr.is_loopback(), IfAddr::V6(ref ifv6_addr) => ifv6_addr.is_loopback(), @@ -71,7 +92,8 @@ impl IfAddr { } /// Check whether this is a link local interface. - pub fn is_link_local(&self) -> bool { + #[must_use] + pub const fn is_link_local(&self) -> bool { match *self { IfAddr::V4(ref ifv4_addr) => ifv4_addr.is_link_local(), IfAddr::V6(ref ifv6_addr) => ifv6_addr.is_link_local(), @@ -79,7 +101,8 @@ impl IfAddr { } /// Get the IP address of this interface address. - pub fn ip(&self) -> IpAddr { + #[must_use] + pub const fn ip(&self) -> IpAddr { match *self { IfAddr::V4(ref ifv4_addr) => IpAddr::V4(ifv4_addr.ip), IfAddr::V6(ref ifv6_addr) => IpAddr::V6(ifv6_addr.ip), @@ -102,12 +125,14 @@ pub struct Ifv4Addr { impl Ifv4Addr { /// Check whether this is a loopback address. - pub fn is_loopback(&self) -> bool { - self.ip.octets()[0] == 127 + #[must_use] + pub const fn is_loopback(&self) -> bool { + self.ip.is_loopback() } /// Check whether this is a link local address. - pub fn is_link_local(&self) -> bool { + #[must_use] + pub const fn is_link_local(&self) -> bool { self.ip.is_link_local() } } @@ -127,12 +152,14 @@ pub struct Ifv6Addr { impl Ifv6Addr { /// Check whether this is a loopback address. - pub fn is_loopback(&self) -> bool { - self.ip.segments() == [0, 0, 0, 0, 0, 0, 0, 1] + #[must_use] + pub const fn is_loopback(&self) -> bool { + self.ip.is_loopback() } /// Check whether this is a link local address. - pub fn is_link_local(&self) -> bool { + #[must_use] + pub const fn is_link_local(&self) -> bool { let bytes = self.ip.octets(); bytes[0] == 0xfe && bytes[1] == 0x80 @@ -386,7 +413,30 @@ pub fn get_if_addrs() -> io::Result> { getifaddrs_windows::get_if_addrs() } -#[cfg(not(any(target_os = "macos", target_os = "ios")))] +#[cfg(not(any( + all( + target_vendor = "apple", + any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos" + ) + ), + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" +)))] +#[cfg_attr( + docsrs, + doc(cfg(any( + not(target_vendor = "apple"), + not(target_os = "freebsd"), + not(target_os = "netbsd") + not(target_os = "openbsd") + ))) +)] mod if_change_notifier { use super::Interface; use std::collections::HashSet; @@ -402,7 +452,7 @@ mod if_change_notifier { #[cfg(windows)] type InternalIfChangeNotifier = crate::windows::WindowsIfChangeNotifier; #[cfg(not(windows))] - type InternalIfChangeNotifier = crate::posix_not_mac::PosixIfChangeNotifier; + type InternalIfChangeNotifier = crate::posix_not_apple::PosixIfChangeNotifier; /// (Not available on iOS/macOS) A utility to monitor for interface changes /// and report them, so you can handle events such as WiFi @@ -463,7 +513,30 @@ mod if_change_notifier { } } -#[cfg(not(any(target_os = "macos", target_os = "ios")))] +#[cfg(not(any( + all( + target_vendor = "apple", + any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos" + ) + ), + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" +)))] +#[cfg_attr( + docsrs, + doc(cfg(any( + not(target_vendor = "apple"), + not(target_os = "freebsd"), + not(target_os = "netbsd"), + not(target_os = "openbsd") + ))) +)] pub use if_change_notifier::{IfChangeNotifier, IfChangeType}; #[cfg(test)] @@ -477,7 +550,7 @@ mod tests { use std::time::Duration; fn list_system_interfaces(cmd: &str, arg: &str) -> String { - let start_cmd = if arg == "" { + let start_cmd = if arg.is_empty() { Command::new(cmd).stdout(Stdio::piped()).spawn() } else { Command::new(cmd).arg(arg).stdout(Stdio::piped()).spawn() @@ -485,7 +558,7 @@ mod tests { let mut process = match start_cmd { Err(why) => { println!("couldn't start cmd {} : {}", cmd, why); - return "".to_string(); + return String::new(); } Ok(process) => process, }; @@ -538,9 +611,18 @@ mod tests { #[cfg(any( target_os = "freebsd", - target_os = "macos", - target_os = "ios", - target_os = "tvos" + target_os = "netbsd", + target_os = "openbsd", + all( + target_vendor = "apple", + any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos" + ) + ) ))] fn list_system_addrs() -> Vec { list_system_interfaces("ifconfig", "") @@ -597,7 +679,21 @@ mod tests { } } - #[cfg(not(any(target_os = "macos", target_os = "ios")))] + #[cfg(not(any( + all( + target_vendor = "apple", + any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos" + ) + ), + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + )))] #[test] fn test_if_notifier() { // Check that the interface notifier can start up and time out. No easy diff --git a/src/posix_not_mac.rs b/src/posix_not_apple.rs similarity index 100% rename from src/posix_not_mac.rs rename to src/posix_not_apple.rs