Skip to content

Commit d6ac1fa

Browse files
committed
Add a custom stdout for the yes command
1 parent 2ae023a commit d6ac1fa

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/bin/yes.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
1+
#![allow(unused_attributes)]
2+
#![feature(can_vector)]
3+
14
use std::{
25
borrow::Cow,
3-
io::{BufWriter, Write, stdout},
6+
io::{BufWriter, Write},
7+
os::fd::BorrowedFd,
48
};
59

610
use puppyutils::{Result, cli};
11+
use rustix::{
12+
io::{write, writev},
13+
stdio::stdout,
14+
};
15+
16+
#[repr(transparent)]
17+
struct UnbufStdout<'a> {
18+
raw: BorrowedFd<'a>,
19+
}
20+
21+
impl UnbufStdout<'_> {
22+
pub fn new() -> Self {
23+
Self { raw: stdout() }
24+
}
25+
}
26+
27+
impl Write for UnbufStdout<'_> {
28+
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
29+
write(self.raw, buf).map_err(std::io::Error::from)
30+
}
31+
32+
fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
33+
writev(self.raw, bufs).map_err(std::io::Error::from)
34+
}
35+
36+
#[inline]
37+
fn is_write_vectored(&self) -> bool {
38+
true
39+
}
40+
41+
fn flush(&mut self) -> std::io::Result<()> {
42+
Ok(())
43+
}
44+
}
745

846
pub fn main() -> Result {
9-
let mut stdout = stdout();
47+
let mut stdout = UnbufStdout::new();
1048

1149
let mut buffer: Option<String> = None;
1250

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(can_vector)]
2+
13
use std::{
24
borrow::Cow,
35
fmt::{Debug, Display},

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(can_vector)]
2+
13
use std::{borrow::Cow, env::current_exe, os::unix::ffi::OsStrExt, path::PathBuf};
24

35
use puppyutils::{Exit, Result};

0 commit comments

Comments
 (0)