aboutsummaryrefslogtreecommitdiffstats
path: root/src/rw.rs
diff options
context:
space:
mode:
authorJonas Maier <>2026-05-03 09:15:04 +0200
committerJonas Maier <>2026-05-03 09:15:04 +0200
commitd5953e52f0df8ca6727e71fa07f147467a7369c1 (patch)
treecffbb58207d2aa81fc6d765b005b1225914fb268 /src/rw.rs
parenta9bf864158e67353f57047cdc4d6b0e325d73eae (diff)
downloadpish-d5953e52f0df8ca6727e71fa07f147467a7369c1.tar.gz
complete keybind overhaul, not working, afraid to loose it
Diffstat (limited to 'src/rw.rs')
-rw-r--r--src/rw.rs34
1 files changed, 23 insertions, 11 deletions
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<Input> 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<Input> for Stdio {
impl From<Output> 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<Output> for Stdio {
impl Input {
pub fn try_clone(&self) -> io::Result<Self> {
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<Self> {
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<BorrowedFd<'a>>,
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(),