Skip to content

Commit

Permalink
Drop ArcFdGuard newtype
Browse files Browse the repository at this point in the history
This type existed because AsRawFd was not implemented for `Arc<T> where
T: AsRawFd`.

As for 1.63.0, the following implementation is provided:

    impl<T: AsRawFd> AsRawFd for Arc<T>

The current MSRV is 1.70, so we can rely on this new implementation.

Drop the newtype wrapper, and rely on the new stdlib implementation
instead.
  • Loading branch information
WhyNotHugo authored and hannobraun committed Jan 14, 2025
1 parent 44ae222 commit 16d867b
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
io,
os::unix::io::{AsRawFd, RawFd},
os::unix::io::AsRawFd,
pin::Pin,
sync::Arc,
task::{Context, Poll},
Expand All @@ -20,7 +20,7 @@ use crate::Inotify;
/// Allows for streaming events returned by [`Inotify::into_event_stream`].
#[derive(Debug)]
pub struct EventStream<T> {
fd: AsyncFd<ArcFdGuard>,
fd: AsyncFd<Arc<FdGuard>>,
buffer: T,
buffer_pos: usize,
unused_bytes: usize,
Expand All @@ -33,7 +33,7 @@ where
/// Returns a new `EventStream` associated with the default reactor.
pub(crate) fn new(fd: Arc<FdGuard>, buffer: T) -> io::Result<Self> {
Ok(EventStream {
fd: AsyncFd::new(ArcFdGuard(fd))?,
fd: AsyncFd::new(fd)?,
buffer,
buffer_pos: 0,
unused_bytes: 0,
Expand All @@ -43,13 +43,13 @@ where
/// Returns an instance of `Watches` to add and remove watches.
/// See [`Watches::add`] and [`Watches::remove`].
pub fn watches(&self) -> Watches {
Watches::new(self.fd.get_ref().0.clone())
Watches::new(self.fd.get_ref().clone())
}

/// Consumes the `EventStream` instance and returns an `Inotify` using the original
/// file descriptor that was passed from `Inotify` to create the `EventStream`.
pub fn into_inotify(self) -> Inotify {
Inotify::from_file_descriptor(self.fd.into_inner().0)
Inotify::from_file_descriptor(self.fd.into_inner())
}
}

Expand Down Expand Up @@ -79,7 +79,7 @@ where
// there, and we only take complete events out. That means we have at
// least one event in there and can call `from_buffer` to take it out.
let (bytes_consumed, event) = Event::from_buffer(
Arc::downgrade(&self_.fd.get_ref().0),
Arc::downgrade(self_.fd.get_ref()),
&self_.buffer.as_ref()[self_.buffer_pos..],
);
self_.buffer_pos += bytes_consumed;
Expand All @@ -89,17 +89,11 @@ where
}
}

// Newtype wrapper because AsRawFd isn't implemented for Arc<T> where T: AsRawFd.
#[derive(Debug)]
struct ArcFdGuard(Arc<FdGuard>);

impl AsRawFd for ArcFdGuard {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}

fn read(fd: &AsyncFd<ArcFdGuard>, buffer: &mut [u8], cx: &mut Context) -> Poll<io::Result<usize>> {
fn read(
fd: &AsyncFd<Arc<FdGuard>>,
buffer: &mut [u8],
cx: &mut Context,
) -> Poll<io::Result<usize>> {
let mut guard = ready!(fd.poll_read_ready(cx))?;
let result = guard.try_io(|_| {
let read = read_into_buffer(fd.as_raw_fd(), buffer);
Expand Down

0 comments on commit 16d867b

Please sign in to comment.