Skip to content

Commit

Permalink
Implement AsFd for all fd types
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasdezeeuw committed Jan 1, 2024
1 parent 964df81 commit 0063ec4
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 4 deletions.
7 changes: 7 additions & 0 deletions rt/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! To open a [`File`] use [`File::open`] or [`OpenOptions`].

use std::os::fd::{AsFd, BorrowedFd};
use std::path::PathBuf;
use std::time::SystemTime;
use std::{fmt, io};
Expand Down Expand Up @@ -231,6 +232,12 @@ impl File {
impl_read!(File, &File);
impl_write!(File, &File);

impl AsFd for File {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

impl fmt::Debug for File {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fd.fmt(f)
Expand Down
8 changes: 7 additions & 1 deletion rt/src/fs/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::ffi::{CString, OsStr, OsString};
use std::mem::{size_of, take};
use std::os::fd::{AsFd, AsRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::path::{Path, PathBuf};
use std::{fmt, io, ptr};

Expand Down Expand Up @@ -191,6 +191,12 @@ impl Watch {
}
}

impl AsFd for Watch {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

/// Iterator behind [`Watch::events`].
pub struct Events<'w> {
/// `Watch::Watching`.
Expand Down
7 changes: 7 additions & 0 deletions rt/src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::async_iter::AsyncIterator;
use std::net::SocketAddr;
use std::os::fd::{AsFd, BorrowedFd};
use std::pin::Pin;
use std::task::{self, Poll};
use std::{fmt, io};
Expand Down Expand Up @@ -239,6 +240,12 @@ impl<'a> AsyncIterator for Incoming<'a> {
}
}

impl AsFd for TcpListener {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

impl fmt::Debug for TcpListener {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fd.fmt(f)
Expand Down
7 changes: 7 additions & 0 deletions rt/src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::io;
use std::net::{Shutdown, SocketAddr};
use std::os::fd::{AsFd, BorrowedFd};

use a10::{AsyncFd, Extract};
use socket2::{Domain, Protocol, SockRef, Type};
Expand Down Expand Up @@ -375,3 +376,9 @@ impl TcpStream {

impl_read!(TcpStream, &TcpStream);
impl_write!(TcpStream, &TcpStream);

impl AsFd for TcpStream {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}
7 changes: 7 additions & 0 deletions rt/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::marker::PhantomData;
use std::net::SocketAddr;
use std::os::fd::{AsFd, BorrowedFd};
use std::{fmt, io};

use a10::{AsyncFd, Extract};
Expand Down Expand Up @@ -287,6 +288,12 @@ impl UdpSocket<Connected> {
}
}

impl<M> AsFd for UdpSocket<M> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

impl<M> fmt::Debug for UdpSocket<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fd.fmt(f)
Expand Down
8 changes: 7 additions & 1 deletion rt/src/net/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::marker::PhantomData;
use std::net::Shutdown;
use std::os::fd::IntoRawFd;
use std::os::fd::{AsFd, BorrowedFd, IntoRawFd};
use std::{fmt, io};

use a10::{AsyncFd, Extract};
Expand Down Expand Up @@ -267,6 +267,12 @@ impl UnixDatagram<Connected> {
}
}

impl<M> AsFd for UnixDatagram<M> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

impl<M> fmt::Debug for UnixDatagram<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fd.fmt(f)
Expand Down
7 changes: 7 additions & 0 deletions rt/src/net/uds/listener.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Module with [`UnixListener`] and related types.

use std::async_iter::AsyncIterator;
use std::os::fd::{AsFd, BorrowedFd};
use std::pin::Pin;
use std::task::{self, Poll};
use std::{fmt, io};
Expand Down Expand Up @@ -215,6 +216,12 @@ impl<'a> AsyncIterator for Incoming<'a> {
}
}

impl AsFd for UnixListener {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

impl fmt::Debug for UnixListener {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fd.fmt(f)
Expand Down
8 changes: 7 additions & 1 deletion rt/src/net/uds/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::io;
use std::net::Shutdown;
use std::os::fd::IntoRawFd;
use std::os::fd::{AsFd, BorrowedFd, IntoRawFd};

use a10::{AsyncFd, Extract};
use socket2::{Domain, SockRef, Type};
Expand Down Expand Up @@ -310,3 +310,9 @@ impl UnixStream {

impl_read!(UnixStream, &UnixStream);
impl_write!(UnixStream, &UnixStream);

impl AsFd for UnixStream {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}
14 changes: 13 additions & 1 deletion rt/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
//! ```

use std::io;
use std::os::fd::{IntoRawFd, RawFd};
use std::os::fd::{AsFd, BorrowedFd, IntoRawFd, RawFd};
use std::process::{ChildStderr, ChildStdin, ChildStdout};

use a10::AsyncFd;
Expand Down Expand Up @@ -140,6 +140,12 @@ impl Sender {

impl_write!(Sender, &Sender);

impl AsFd for Sender {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

/// Receiving end of an Unix pipe.
///
/// Created by calling [`new`] or converted from [`ChildStdout`] or
Expand Down Expand Up @@ -172,3 +178,9 @@ impl Receiver {
}

impl_read!(Receiver, &Receiver);

impl AsFd for Receiver {
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}

0 comments on commit 0063ec4

Please sign in to comment.