Skip to content

Commit 4947472

Browse files
committed
Accept AsFd in ioctl macros
1 parent ba63e90 commit 4947472

File tree

6 files changed

+69
-55
lines changed

6 files changed

+69
-55
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ This project adheres to [Semantic Versioning](https://semver.org/).
33

44
# Change Log
55

6+
## [0.31.0] - unreleased
7+
8+
### Changed
9+
10+
- Accept AsFd in ioctl macros
11+
612
## [0.30.1] - 2025-05-04
713

814
### Fixed

src/sys/ioctl/mod.rs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ macro_rules! convert_ioctl_res {
261261
/// The generated function has the following signature:
262262
///
263263
/// ```rust,ignore
264-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int) -> Result<libc::c_int>
264+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd) -> Result<libc::c_int>
265265
/// ```
266266
///
267267
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -285,8 +285,9 @@ macro_rules! convert_ioctl_res {
285285
macro_rules! ioctl_none {
286286
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
287287
$(#[$attr])*
288-
pub unsafe fn $name(fd: $crate::libc::c_int)
288+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd)
289289
-> $crate::Result<$crate::libc::c_int> {
290+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
290291
unsafe {
291292
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_none!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type))
292293
}
@@ -304,7 +305,7 @@ macro_rules! ioctl_none {
304305
/// The generated function has the following signature:
305306
///
306307
/// ```rust,ignore
307-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int) -> Result<libc::c_int>
308+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd) -> Result<libc::c_int>
308309
/// ```
309310
///
310311
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -315,20 +316,20 @@ macro_rules! ioctl_none {
315316
/// # #[macro_use] extern crate nix;
316317
/// # use libc::TIOCNXCL;
317318
/// # use std::fs::File;
318-
/// # use std::os::unix::io::AsRawFd;
319319
/// ioctl_none_bad!(tiocnxcl, TIOCNXCL);
320320
/// fn main() {
321321
/// let file = File::open("/dev/ttyUSB0").unwrap();
322-
/// unsafe { tiocnxcl(file.as_raw_fd()) }.unwrap();
322+
/// unsafe { tiocnxcl(&file) }.unwrap();
323323
/// }
324324
/// ```
325325
// TODO: add an example using request_code_*!()
326326
#[macro_export(local_inner_macros)]
327327
macro_rules! ioctl_none_bad {
328328
($(#[$attr:meta])* $name:ident, $nr:expr) => (
329329
$(#[$attr])*
330-
pub unsafe fn $name(fd: $crate::libc::c_int)
330+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd)
331331
-> $crate::Result<$crate::libc::c_int> {
332+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
332333
unsafe {
333334
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
334335
}
@@ -348,7 +349,7 @@ macro_rules! ioctl_none_bad {
348349
/// The generated function has the following signature:
349350
///
350351
/// ```rust,ignore
351-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
352+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *mut DATA_TYPE) -> Result<libc::c_int>
352353
/// ```
353354
///
354355
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -366,9 +367,10 @@ macro_rules! ioctl_none_bad {
366367
macro_rules! ioctl_read {
367368
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
368369
$(#[$attr])*
369-
pub unsafe fn $name(fd: $crate::libc::c_int,
370+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
370371
data: *mut $ty)
371372
-> $crate::Result<$crate::libc::c_int> {
373+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
372374
unsafe {
373375
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
374376
}
@@ -387,7 +389,7 @@ macro_rules! ioctl_read {
387389
/// The generated function has the following signature:
388390
///
389391
/// ```rust,ignore
390-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
392+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *mut DATA_TYPE) -> Result<libc::c_int>
391393
/// ```
392394
///
393395
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -404,9 +406,10 @@ macro_rules! ioctl_read {
404406
macro_rules! ioctl_read_bad {
405407
($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
406408
$(#[$attr])*
407-
pub unsafe fn $name(fd: $crate::libc::c_int,
409+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
408410
data: *mut $ty)
409411
-> $crate::Result<$crate::libc::c_int> {
412+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
410413
unsafe {
411414
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
412415
}
@@ -426,7 +429,7 @@ macro_rules! ioctl_read_bad {
426429
/// The generated function has the following signature:
427430
///
428431
/// ```rust,ignore
429-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *const DATA_TYPE) -> Result<libc::c_int>
432+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *const DATA_TYPE) -> Result<libc::c_int>
430433
/// ```
431434
///
432435
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -443,9 +446,10 @@ macro_rules! ioctl_read_bad {
443446
macro_rules! ioctl_write_ptr {
444447
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
445448
$(#[$attr])*
446-
pub unsafe fn $name(fd: $crate::libc::c_int,
449+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
447450
data: *const $ty)
448451
-> $crate::Result<$crate::libc::c_int> {
452+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
449453
unsafe {
450454
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
451455
}
@@ -464,7 +468,7 @@ macro_rules! ioctl_write_ptr {
464468
/// The generated function has the following signature:
465469
///
466470
/// ```rust,ignore
467-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *const DATA_TYPE) -> Result<libc::c_int>
471+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *const DATA_TYPE) -> Result<libc::c_int>
468472
/// ```
469473
///
470474
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -481,9 +485,10 @@ macro_rules! ioctl_write_ptr {
481485
macro_rules! ioctl_write_ptr_bad {
482486
($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
483487
$(#[$attr])*
484-
pub unsafe fn $name(fd: $crate::libc::c_int,
488+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
485489
data: *const $ty)
486490
-> $crate::Result<$crate::libc::c_int> {
491+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
487492
unsafe {
488493
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
489494
}
@@ -504,7 +509,7 @@ cfg_if! {
504509
/// The generated function has the following signature:
505510
///
506511
/// ```rust,ignore
507-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
512+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
508513
/// ```
509514
///
510515
/// `nix::sys::ioctl::ioctl_param_type` depends on the OS:
@@ -524,9 +529,10 @@ cfg_if! {
524529
macro_rules! ioctl_write_int {
525530
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
526531
$(#[$attr])*
527-
pub unsafe fn $name(fd: $crate::libc::c_int,
532+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
528533
data: $crate::sys::ioctl::ioctl_param_type)
529534
-> $crate::Result<$crate::libc::c_int> {
535+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
530536
unsafe {
531537
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write_int!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type, data))
532538
}
@@ -545,7 +551,7 @@ cfg_if! {
545551
/// The generated function has the following signature:
546552
///
547553
/// ```rust,ignore
548-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
554+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
549555
/// ```
550556
///
551557
/// `nix::sys::ioctl::ioctl_param_type` depends on the OS:
@@ -567,9 +573,10 @@ cfg_if! {
567573
macro_rules! ioctl_write_int {
568574
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
569575
$(#[$attr])*
570-
pub unsafe fn $name(fd: $crate::libc::c_int,
576+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
571577
data: $crate::sys::ioctl::ioctl_param_type)
572578
-> $crate::Result<$crate::libc::c_int> {
579+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
573580
unsafe {
574581
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$crate::libc::c_int>()) as $crate::sys::ioctl::ioctl_num_type, data))
575582
}
@@ -589,7 +596,7 @@ cfg_if! {
589596
/// The generated function has the following signature:
590597
///
591598
/// ```rust,ignore
592-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: libc::c_int) -> Result<libc::c_int>
599+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: libc::c_int) -> Result<libc::c_int>
593600
/// ```
594601
///
595602
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -613,9 +620,10 @@ cfg_if! {
613620
macro_rules! ioctl_write_int_bad {
614621
($(#[$attr:meta])* $name:ident, $nr:expr) => (
615622
$(#[$attr])*
616-
pub unsafe fn $name(fd: $crate::libc::c_int,
623+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
617624
data: $crate::libc::c_int)
618625
-> $crate::Result<$crate::libc::c_int> {
626+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
619627
unsafe {
620628
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
621629
}
@@ -635,7 +643,7 @@ macro_rules! ioctl_write_int_bad {
635643
/// The generated function has the following signature:
636644
///
637645
/// ```rust,ignore
638-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
646+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *mut DATA_TYPE) -> Result<libc::c_int>
639647
/// ```
640648
///
641649
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -652,9 +660,10 @@ macro_rules! ioctl_write_int_bad {
652660
macro_rules! ioctl_readwrite {
653661
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
654662
$(#[$attr])*
655-
pub unsafe fn $name(fd: $crate::libc::c_int,
663+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
656664
data: *mut $ty)
657665
-> $crate::Result<$crate::libc::c_int> {
666+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
658667
let ioty = $ioty;
659668
let nr = $nr;
660669
unsafe {
@@ -675,7 +684,7 @@ macro_rules! ioctl_readwrite {
675684
/// The generated function has the following signature:
676685
///
677686
/// ```rust,ignore
678-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
687+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *mut DATA_TYPE) -> Result<libc::c_int>
679688
/// ```
680689
///
681690
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -684,9 +693,10 @@ macro_rules! ioctl_readwrite {
684693
macro_rules! ioctl_readwrite_bad {
685694
($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
686695
$(#[$attr])*
687-
pub unsafe fn $name(fd: $crate::libc::c_int,
696+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
688697
data: *mut $ty)
689698
-> $crate::Result<$crate::libc::c_int> {
699+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
690700
unsafe {
691701
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
692702
}
@@ -706,7 +716,7 @@ macro_rules! ioctl_readwrite_bad {
706716
/// The generated function has the following signature:
707717
///
708718
/// ```rust,ignore
709-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
719+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
710720
/// ```
711721
///
712722
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -715,9 +725,10 @@ macro_rules! ioctl_readwrite_bad {
715725
macro_rules! ioctl_read_buf {
716726
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
717727
$(#[$attr])*
718-
pub unsafe fn $name(fd: $crate::libc::c_int,
728+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
719729
data: &mut [$ty])
720730
-> $crate::Result<$crate::libc::c_int> {
731+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
721732
unsafe {
722733
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
723734
}
@@ -737,7 +748,7 @@ macro_rules! ioctl_read_buf {
737748
/// The generated function has the following signature:
738749
///
739750
/// ```rust,ignore
740-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &[DATA_TYPE]) -> Result<libc::c_int>
751+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: &[DATA_TYPE]) -> Result<libc::c_int>
741752
/// ```
742753
///
743754
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -756,9 +767,10 @@ macro_rules! ioctl_read_buf {
756767
macro_rules! ioctl_write_buf {
757768
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
758769
$(#[$attr])*
759-
pub unsafe fn $name(fd: $crate::libc::c_int,
770+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
760771
data: &[$ty])
761772
-> $crate::Result<$crate::libc::c_int> {
773+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
762774
unsafe {
763775
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_ptr()))
764776
}
@@ -778,7 +790,7 @@ macro_rules! ioctl_write_buf {
778790
/// The generated function has the following signature:
779791
///
780792
/// ```rust,ignore
781-
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
793+
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
782794
/// ```
783795
///
784796
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
@@ -787,9 +799,10 @@ macro_rules! ioctl_write_buf {
787799
macro_rules! ioctl_readwrite_buf {
788800
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
789801
$(#[$attr])*
790-
pub unsafe fn $name(fd: $crate::libc::c_int,
802+
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
791803
data: &mut [$ty])
792804
-> $crate::Result<$crate::libc::c_int> {
805+
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
793806
unsafe {
794807
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
795808
}

src/sys/socket/addr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ pub mod sys_control {
17321732
use crate::sys::socket::addr::AddressFamily;
17331733
use libc::{self, c_uchar};
17341734
use std::{fmt, mem, ptr};
1735-
use std::os::unix::io::RawFd;
1735+
17361736
use crate::{Errno, Result};
17371737
use super::{private, SockaddrLike};
17381738

@@ -1801,7 +1801,7 @@ pub mod sys_control {
18011801

18021802
/// Construct a new `SysControlAddr` from its human readable name and
18031803
/// unit number.
1804-
pub fn from_name(sockfd: RawFd, name: &str, unit: u32) -> Result<SysControlAddr> {
1804+
pub fn from_name<Fd: ::std::os::fd::AsFd>(sockfd: Fd, name: &str, unit: u32) -> Result<SysControlAddr> {
18051805
if name.len() > MAX_KCTL_NAME {
18061806
return Err(Errno::ENAMETOOLONG);
18071807
}

0 commit comments

Comments
 (0)