From: Ian Jackson Date: Mon, 17 May 2021 13:47:00 +0000 (+0100) Subject: timedfd: Refactor to prep for writing too (2) X-Git-Tag: otter-0.6.0~221 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=f6b47c64c482271f4c33e2c4e71a739f83c5a92f;p=otter.git timedfd: Refactor to prep for writing too (2) Signed-off-by: Ian Jackson --- diff --git a/src/timedfd.rs b/src/timedfd.rs index 67869c16..c16aa136 100644 --- a/src/timedfd.rs +++ b/src/timedfd.rs @@ -85,15 +85,15 @@ impl TimedFd where RW: TimedFdReadWrite { Instant::now() + timeout })); } -} -impl Read for TimedFd { #[throws(io::Error)] - fn read(&mut self, buf: &mut [u8]) -> usize { + fn rw(&mut self, mut f: F) -> O + where F: FnMut(i32) -> Result + { 'again: loop { for event in &self.events { if event.token() == Token(0) { - match unistd::read(self.fd.as_raw_fd(), buf) { + match f(self.fd.as_raw_fd()) { Ok(got) => { break 'again got }, Err(NE::Sys(Errno::EINTR)) => { continue 'again } Err(NE::Sys(Errno::EAGAIN)) => break, @@ -121,6 +121,13 @@ impl Read for TimedFd { } } +impl Read for TimedFd { + #[throws(io::Error)] + fn read(&mut self, buf: &mut [u8]) -> usize { + self.rw(|fd| unistd::read(fd, buf))? + } +} + impl Drop for Fd { fn drop(&mut self) { let fd = self.extract_raw_fd();