From d5953e52f0df8ca6727e71fa07f147467a7369c1 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Sun, 3 May 2026 09:15:04 +0200 Subject: complete keybind overhaul, not working, afraid to loose it --- src/rw.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'src/rw.rs') diff --git a/src/rw.rs b/src/rw.rs index 601a2ec..89f6d76 100644 --- a/src/rw.rs +++ b/src/rw.rs @@ -13,12 +13,14 @@ use std::{ use nix::poll::{PollFd, PollFlags}; pub enum Input { + Null, Stdin, Pipe(PipeReader), File(File), } pub enum Output { + Null, Stdout, Pipe(PipeWriter), File(File), @@ -27,6 +29,7 @@ pub enum Output { impl From for Stdio { fn from(value: Input) -> Self { match value { + Input::Null => Stdio::null(), Input::Stdin => Stdio::inherit(), Input::Pipe(reader) => reader.into(), Input::File(file) => file.into(), @@ -37,6 +40,7 @@ impl From for Stdio { impl From for Stdio { fn from(value: Output) -> Stdio { match value { + Output::Null => Stdio::null(), Output::Stdout => Stdio::inherit(), Output::Pipe(writer) => writer.into(), Output::File(file) => file.into(), @@ -47,6 +51,7 @@ impl From for Stdio { impl Input { pub fn try_clone(&self) -> io::Result { Ok(match self { + Input::Null => Input::Null, Input::Stdin => Input::Stdin, Input::Pipe(pr) => Input::Pipe(pr.try_clone()?), Input::File(f) => Input::File(f.try_clone()?), @@ -57,6 +62,7 @@ impl Input { impl Output { pub fn try_clone(&self) -> io::Result { Ok(match self { + Output::Null => Output::Null, Output::Stdout => Output::Stdout, Output::Pipe(pw) => Output::Pipe(pw.try_clone()?), Output::File(f) => Output::File(f.try_clone()?), @@ -116,17 +122,18 @@ enum PollStatus { fn check<'a>( canceled: &AtomicBool, cancel: &PipeReader, - fd: BorrowedFd<'a>, + fd: Option>, flags: PollFlags, ) -> PollStatus { if canceled.load(SeqCst) { return PollStatus::Cancel; } - let mut poll_fds = [ - PollFd::new(cancel.as_fd(), PollFlags::POLLIN), - PollFd::new(fd, flags), - ]; + let mut poll_fds = Vec::with_capacity(2); + poll_fds.push(PollFd::new(cancel.as_fd(), PollFlags::POLLIN)); + if let Some(fd) = fd { + poll_fds.push(PollFd::new(fd, flags)); + } if nix::poll::poll(&mut poll_fds, TIMEOUT_MS).is_err() { canceled.store(true, SeqCst); @@ -153,9 +160,10 @@ impl InputReader { fn poll(&mut self) -> PollStatus { let stdin = io::stdin(); let read_fd = match &self.input { - Input::Stdin => stdin.as_fd(), - Input::Pipe(pipe) => pipe.as_fd(), - Input::File(file) => file.as_fd(), + Input::Null => None, + Input::Stdin => Some(stdin.as_fd()), + Input::Pipe(pipe) => Some(pipe.as_fd()), + Input::File(file) => Some(file.as_fd()), }; check(&self.canceled, &self.cancel, read_fd, PollFlags::POLLIN) } @@ -181,6 +189,7 @@ impl Read for InputReader { PollStatus::Wait => continue, } return match &mut self.input { + Input::Null => Ok(0), Input::Stdin => io::stdin().read(buf), Input::Pipe(reader) => reader.read(buf), Input::File(file) => file.read(buf), @@ -210,9 +219,10 @@ impl OutputWriter { fn poll(&mut self) -> PollStatus { let stdout = io::stdout(); let write_fd = match &self.output { - Output::Stdout => stdout.as_fd(), - Output::Pipe(pipe) => pipe.as_fd(), - Output::File(file) => file.as_fd(), + Output::Null => None, + Output::Stdout => Some(stdout.as_fd()), + Output::Pipe(pipe) => Some(pipe.as_fd()), + Output::File(file) => Some(file.as_fd()), }; check(&self.canceled, &self.cancel, write_fd, PollFlags::POLLOUT) } @@ -237,6 +247,7 @@ impl Write for OutputWriter { PollStatus::Wait => continue, } return match &mut self.output { + Output::Null => Ok(buf.len()), Output::Stdout => io::stdout().write(buf), Output::Pipe(writer) => writer.write(buf), Output::File(file) => file.write(buf), @@ -246,6 +257,7 @@ impl Write for OutputWriter { fn flush(&mut self) -> io::Result<()> { match &mut self.output { + Output::Null => Ok(()), Output::Stdout => io::stdout().flush(), Output::Pipe(writer) => writer.flush(), Output::File(file) => file.flush(), -- cgit v1.2.3