From 155fa5d752195bdc8a775663af69c721f5bd8f26 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 17 May 2021 14:38:37 +0100 Subject: [PATCH] timedfd: Refactor to prep for writing too (1) Signed-off-by: Ian Jackson --- src/timedfd.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/timedfd.rs b/src/timedfd.rs index b4278232..67869c16 100644 --- a/src/timedfd.rs +++ b/src/timedfd.rs @@ -12,11 +12,23 @@ use nix::errno::Errno; use mio::Token; -pub struct TimedFdReader { +pub struct TimedFd { fd: Fd, poll: mio::Poll, events: mio::event::Events, deadline: Option, + rw: PhantomData, +} + +pub trait TimedFdReadWrite { + const INTEREST: mio::Interest; +} + +pub type TimedFdReader = TimedFd; + +#[derive(Debug,Copy,Clone)] pub struct TimedFdRead; +impl TimedFdReadWrite for TimedFdRead { + const INTEREST : mio::Interest = mio::Interest::READABLE; } pub struct Fd(RawFd); @@ -43,15 +55,15 @@ impl nix::Error { } } -impl TimedFdReader { +impl TimedFd where RW: TimedFdReadWrite { #[throws(io::Error)] - pub fn new(fd: F) -> TimedFdReader where F: IntoRawFd { + pub fn new(fd: F) -> TimedFd where F: IntoRawFd { Self::from_fd( Fd::from_raw_fd( fd.into_raw_fd() ))? } /// Takes ownership of the fd #[throws(io::Error)] - pub fn from_fd(fd: Fd) -> Self { + fn from_fd(fd: Fd) -> Self { fcntl(fd.as_raw_fd(), FcntlArg::F_SETFL(OFlag::O_NONBLOCK)) .map_err(|e| e.as_ioe())?; @@ -59,10 +71,10 @@ impl TimedFdReader { poll.registry().register( &mut mio::unix::SourceFd(&fd.as_raw_fd()), Token(0), - mio::Interest::READABLE, + RW::INTEREST, )?; let events = mio::event::Events::with_capacity(1); - TimedFdReader { fd, poll, events, deadline: None } + TimedFd { fd, poll, events, deadline: None, rw: PhantomData } } pub fn set_deadline(&mut self, deadline: Option) { @@ -75,7 +87,7 @@ impl TimedFdReader { } } -impl Read for TimedFdReader { +impl Read for TimedFd { #[throws(io::Error)] fn read(&mut self, buf: &mut [u8]) -> usize { 'again: loop { -- 2.30.2