From 0aba3fd6e81824af143b99bc0f1a45dde16ca2ee Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 3 Sep 2024 10:29:21 +0200 Subject: [PATCH 1/2] Deprecate unidiomatic get_() prefix --- README.md | 2 +- src/lib.rs | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a6b1f41..2be56a7 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A crate to return the maximum transmission unit (MTU) of the local network inter This crate exports a single function ```rust -pub fn get_interface_mtu(remote: &SocketAddr) -> Result +pub fn interface_mtu(remote: &SocketAddr) -> Result ``` that returns the MTU of the local network interface towards the `remote` destination, or an `Error` when the MTU could not be determined. It supports both IPv4 and IPv6. diff --git a/src/lib.rs b/src/lib.rs index c2a939b..822b70b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,14 +31,14 @@ fn default_result() -> Result { /// /// ``` /// let saddr = "127.0.0.1:443".parse().unwrap(); -/// let mtu = mtu::get_interface_mtu(&saddr).unwrap(); +/// let mtu = mtu::interface_mtu(&saddr).unwrap(); /// println!("MTU towards {:?} is {}", saddr, mtu); /// ``` /// /// # Errors /// /// This function returns an error if the local interface MTU cannot be determined. -pub fn get_interface_mtu(remote: &SocketAddr) -> Result { +pub fn interface_mtu(remote: &SocketAddr) -> Result { #[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))] #[allow(unused_assignments)] // Yes, res is reassigned in the platform-specific code. let mut res = default_result(); @@ -59,12 +59,12 @@ pub fn get_interface_mtu(remote: &SocketAddr) -> Result { #[cfg(any(target_os = "macos", target_os = "linux"))] { - res = get_interface_mtu_linux_macos(&socket); + res = interface_mtu_linux_macos(&socket); } #[cfg(target_os = "windows")] { - res = get_interface_mtu_windows(&socket); + res = interface_mtu_windows(&socket); } } @@ -72,8 +72,14 @@ pub fn get_interface_mtu(remote: &SocketAddr) -> Result { res } +#[doc(hidden)] +#[deprecated(since = "0.1.2", note = "Use `interface_mtu()` instead")] +pub fn get_interface_mtu(remote: &SocketAddr) -> Result { + interface_mtu(remote) +} + #[cfg(any(target_os = "macos", target_os = "linux"))] -fn get_interface_mtu_linux_macos(socket: &UdpSocket) -> Result { +fn interface_mtu_linux_macos(socket: &UdpSocket) -> Result { use std::ffi::{c_int, CStr}; #[cfg(target_os = "linux")] use std::{ffi::c_char, mem, os::fd::AsRawFd}; @@ -175,7 +181,7 @@ fn get_interface_mtu_linux_macos(socket: &UdpSocket) -> Result { } #[cfg(target_os = "windows")] -fn get_interface_mtu_windows(socket: &UdpSocket) -> Result { +fn interface_mtu_windows(socket: &UdpSocket) -> Result { use std::{ffi::c_void, slice}; use windows::Win32::{ @@ -260,7 +266,7 @@ mod test { .unwrap() .find(|a| a.is_ipv4() == ipv4); if let Some(addr) = addr { - match super::get_interface_mtu(&addr) { + match super::interface_mtu(&addr) { Ok(mtu) => assert_eq!(mtu, expected), Err(e) => { // Some GitHub runners don't have IPv6. Just warn if we can't get the MTU. From 67f3dc47d5a29e4449b9446db995dc5af6c481f6 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 3 Sep 2024 10:33:01 +0200 Subject: [PATCH 2/2] Use inline format arguments --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 822b70b..7683906 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ fn default_result() -> Result { /// ``` /// let saddr = "127.0.0.1:443".parse().unwrap(); /// let mtu = mtu::interface_mtu(&saddr).unwrap(); -/// println!("MTU towards {:?} is {}", saddr, mtu); +/// println!("MTU towards {saddr:?} is {mtu}"); /// ``` /// /// # Errors @@ -68,7 +68,7 @@ pub fn interface_mtu(remote: &SocketAddr) -> Result { } } - trace!("MTU towards {:?} is {:?}", remote, res); + trace!("MTU towards {remote:?} is {res:?}"); res } @@ -271,13 +271,13 @@ mod test { Err(e) => { // Some GitHub runners don't have IPv6. Just warn if we can't get the MTU. assert!(addr.is_ipv6()); - warn!("Error getting MTU for {}: {}", sockaddr, e); + warn!("Error getting MTU for {sockaddr}: {e}"); } } } else { // Some GitHub runners don't have IPv6. Just warn if we can't get an IPv6 address. assert!(!ipv4); - warn!("No IPv6 address found for {}", sockaddr); + warn!("No IPv6 address found for {sockaddr}"); } }