From: Ian Jackson Date: Sun, 16 May 2021 19:12:53 +0000 (+0100) Subject: timereader: Provide useful methods X-Git-Tag: otter-0.6.0~228 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e1320b10e28eaf8456544d1c5ad1ec30775fb1e9;p=otter.git timereader: Provide useful methods Signed-off-by: Ian Jackson --- diff --git a/src/timedread.rs b/src/timedread.rs index 4a7d27fd..122b59fd 100644 --- a/src/timedread.rs +++ b/src/timedread.rs @@ -44,6 +44,11 @@ impl nix::Error { } impl TimedFdReader { + #[throws(io::Error)] + pub fn new(fd: F) -> TimedFdReader 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 { @@ -59,6 +64,15 @@ impl TimedFdReader { let events = mio::event::Events::with_capacity(1); TimedFdReader { fd, poll, events, deadline: None } } + + pub fn set_deadline(&mut self, deadline: Option) { + self.deadline = deadline; + } + pub fn set_timeout(&mut self, timeout: Option) { + self.deadline = timeout.map(|timeout|{ + Instant::now() + timeout + }); + } } impl Read for TimedFdReader {