Skip to content

Commit 5d9c99c

Browse files
committed
Fix cfg, take &str instead of String
1 parent e9c0a89 commit 5d9c99c

File tree

6 files changed

+34
-26
lines changed

6 files changed

+34
-26
lines changed

src/backend/libc/net/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) mod ext;
88
target_os = "wasi"
99
)))]
1010
pub(crate) mod msghdr;
11-
#[cfg(target_os = "linux")]
11+
#[cfg(all(target_os = "linux", feature = "alloc"))]
1212
pub(crate) mod netdevice;
1313
pub(crate) mod read_sockaddr;
1414
pub(crate) mod send_recv;

src/backend/libc/net/netdevice.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
use crate::backend::io::syscalls::ioctl;
44
use crate::fd::AsFd;
5-
use crate::ffi::{CStr, CString};
5+
use crate::ffi::CStr;
6+
#[cfg(all(target_os = "linux", feature = "alloc"))]
7+
use crate::ffi::CString;
68
use crate::io;
79
use crate::net::netdevice::open_socket;
810
use core::mem::MaybeUninit;
911
use core::ptr::{addr_of, addr_of_mut};
1012
use libc::{c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX, SIOCGIFNAME};
1113

12-
#[cfg(target_os = "linux")]
13-
pub(crate) fn index_to_name(if_name: String) -> io::Result<u32> {
14+
#[cfg(all(target_os = "linux", feature = "alloc"))]
15+
pub(crate) fn index_to_name(if_name: &str) -> io::Result<u32> {
1416
if if_name.len() >= IFNAMSIZ as usize {
1517
return Err(io::Errno::NODEV);
1618
}
@@ -36,7 +38,7 @@ pub(crate) fn index_to_name(if_name: String) -> io::Result<u32> {
3638
Ok(index as u32)
3739
}
3840

39-
#[cfg(target_os = "linux")]
41+
#[cfg(all(target_os = "linux", feature = "alloc"))]
4042
pub(crate) fn name_to_index(index: u32) -> io::Result<String> {
4143
let mut uninit_ifreq = MaybeUninit::<ifreq>::uninit();
4244
let uninit_ifreq_ptr = uninit_ifreq.as_mut_ptr();

src/backend/linux_raw/net/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub(crate) mod addr;
22
pub(crate) mod msghdr;
3-
#[cfg(target_os = "linux")]
3+
#[cfg(all(target_os = "linux", feature = "alloc"))]
44
pub(crate) mod netdevice;
55
pub(crate) mod read_sockaddr;
66
pub(crate) mod send_recv;

src/backend/linux_raw/net/netdevice.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
use crate::backend::io::syscalls::ioctl;
44
use crate::fd::AsFd;
5-
use crate::ffi::{CStr, CString};
5+
use crate::ffi::CStr;
6+
#[cfg(all(target_os = "linux", feature = "alloc"))]
7+
use crate::ffi::CString;
68
use crate::io;
79
use crate::net::netdevice::open_socket;
10+
use crate::path::Arg;
811
use core::mem::MaybeUninit;
912
use core::ptr::{addr_of, addr_of_mut};
1013
use linux_raw_sys::ctypes::c_char;
1114
use linux_raw_sys::ioctl::{SIOCGIFINDEX, SIOCGIFNAME};
1215
use linux_raw_sys::net::{ifreq, IFNAMSIZ};
1316

14-
#[cfg(target_os = "linux")]
15-
pub(crate) fn index_to_name(if_name: String) -> io::Result<u32> {
17+
#[cfg(all(target_os = "linux", feature = "alloc"))]
18+
pub(crate) fn index_to_name(if_name: &str) -> io::Result<u32> {
1619
if if_name.len() >= IFNAMSIZ as usize {
1720
return Err(io::Errno::NODEV);
1821
}
@@ -38,7 +41,7 @@ pub(crate) fn index_to_name(if_name: String) -> io::Result<u32> {
3841
Ok(index as u32)
3942
}
4043

41-
#[cfg(target_os = "linux")]
44+
#[cfg(all(target_os = "linux", feature = "alloc"))]
4245
pub(crate) fn name_to_index(index: u32) -> io::Result<String> {
4346
let mut uninit_ifreq = MaybeUninit::<ifreq>::uninit();
4447
let uninit_ifreq_ptr = uninit_ifreq.as_mut_ptr();

src/net/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ mod types;
1616
#[cfg(windows)]
1717
mod wsa;
1818

19-
#[cfg(target_os = "linux")]
19+
// Currently limited to feature = "alloc" as it only contains methods requiring alloc at this time.
20+
#[cfg(all(target_os = "linux", feature = "alloc"))]
2021
pub mod netdevice;
2122
pub mod sockopt;
2223

src/net/netdevice.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
//!
66
//! [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
77
8+
use crate::fd::OwnedFd;
89
use crate::io;
910
use crate::io::Errno;
1011
use crate::net::{socket, AddressFamily, SocketType};
11-
use std::os::fd::OwnedFd;
1212

1313
/// Creates a socket used to communicate with the kernel in the ioctl calls.
14-
#[cfg(target_os = "linux")]
14+
// Currently limited to feature = "alloc" as it is only used by methods
15+
// requiring alloc at this time.
16+
#[cfg(all(target_os = "linux", feature = "alloc"))]
1517
pub(crate) fn open_socket() -> io::Result<OwnedFd> {
1618
if let Ok(fd) = socket(AddressFamily::UNIX, SocketType::DGRAM, None) {
1719
Ok(fd)
@@ -24,38 +26,38 @@ pub(crate) fn open_socket() -> io::Result<OwnedFd> {
2426
}
2527
}
2628

27-
/// `ioctl(fd, SIOCGIFNAME, ifreq)`—Returns the interface name for a given index.
29+
/// `ioctl(fd, SIOCGIFINDEX, ifreq)`—Returns the interface index for a given name.
2830
///
2931
/// # References
3032
/// - [Linux]
3133
///
3234
/// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
3335
#[inline]
34-
#[doc(alias = "SIOCGIFNAME")]
35-
#[cfg(target_os = "linux")]
36-
pub fn name_to_index(index: u32) -> io::Result<String> {
37-
crate::backend::net::netdevice::name_to_index(index)
36+
#[doc(alias = "SIOCGIFINDEX")]
37+
#[cfg(all(target_os = "linux", feature = "alloc"))]
38+
pub fn index_to_name(if_name: &str) -> io::Result<u32> {
39+
crate::backend::net::netdevice::index_to_name(if_name)
3840
}
3941

40-
/// `ioctl(fd, SIOCGIFINDEX, ifreq)`—Returns the interface index for a given name.
42+
/// `ioctl(fd, SIOCGIFNAME, ifreq)`—Returns the interface name for a given index.
4143
///
4244
/// # References
4345
/// - [Linux]
4446
///
4547
/// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
4648
#[inline]
47-
#[doc(alias = "SIOCGIFINDEX")]
48-
#[cfg(target_os = "linux")]
49-
pub fn index_to_name(if_name: String) -> io::Result<u32> {
50-
crate::backend::net::netdevice::index_to_name(if_name)
49+
#[doc(alias = "SIOCGIFNAME")]
50+
#[cfg(all(target_os = "linux", feature = "alloc"))]
51+
pub fn name_to_index(index: u32) -> io::Result<String> {
52+
crate::backend::net::netdevice::name_to_index(index)
5153
}
5254

5355
#[cfg(test)]
5456
mod tests {
5557
use crate::backend::net::netdevice::{index_to_name, name_to_index};
5658

5759
#[test]
58-
#[cfg(target_os = "linux")]
60+
#[cfg(all(target_os = "linux", feature = "alloc"))]
5961
fn test_index_to_name() {
6062
let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex")
6163
.unwrap()
@@ -64,11 +66,11 @@ mod tests {
6466
.0
6567
.parse::<u32>()
6668
.unwrap();
67-
assert_eq!(Ok(loopback_index), index_to_name("lo".to_owned()));
69+
assert_eq!(Ok(loopback_index), index_to_name("lo"));
6870
}
6971

7072
#[test]
71-
#[cfg(target_os = "linux")]
73+
#[cfg(all(target_os = "linux", feature = "alloc"))]
7274
fn test_name_to_index() {
7375
let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex")
7476
.unwrap()

0 commit comments

Comments
 (0)