Skip to content

Commit 3e3daaa

Browse files
oech3cakebaker
authored andcommitted
yes: remove cfg & move ownership of buffer to fn exec
1 parent 6b75bae commit 3e3daaa

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

src/uu/yes/src/yes.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2727
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
2828

2929
#[allow(clippy::unwrap_used, reason = "clap provides 'y' by default")]
30-
let mut buffer = args_into_buffer(matches.get_many::<OsString>("STRING").unwrap())?;
31-
#[cfg(any(target_os = "linux", target_os = "android"))]
32-
let aligned_before_growing = PAGE_SIZE.is_multiple_of(buffer.len());
33-
34-
prepare_buffer(&mut buffer);
35-
#[cfg(any(target_os = "linux", target_os = "android"))]
36-
let res = exec(&buffer, aligned_before_growing);
37-
#[cfg(not(any(target_os = "linux", target_os = "android")))]
38-
let res = exec(&buffer);
39-
match res {
30+
let buffer = args_into_buffer(matches.get_many::<OsString>("STRING").unwrap())?;
31+
32+
match exec(buffer) {
4033
Ok(()) => Ok(()),
4134
// On Windows, silently handle broken pipe since there's no SIGPIPE
4235
#[cfg(windows)]
@@ -98,7 +91,7 @@ fn args_into_buffer<'a>(i: impl Iterator<Item = &'a OsString>) -> UResult<Vec<u8
9891

9992
/// Assumes buf holds a single output line forged from the command line arguments, copies it
10093
/// repeatedly until the buffer holds as many copies as it can
101-
fn prepare_buffer(buf: &mut Vec<u8>) {
94+
fn repeat_content_to_capacity(buf: &mut Vec<u8>) {
10295
let line_len = buf.len();
10396
debug_assert!(line_len > 0, "buffer is not empty since we have newline");
10497
let target_size = line_len * (buf.capacity() / line_len); // 0 if line_len is already large enough
@@ -111,16 +104,21 @@ fn prepare_buffer(buf: &mut Vec<u8>) {
111104
}
112105

113106
#[cfg(not(any(target_os = "linux", target_os = "android")))]
114-
pub fn exec(bytes: &[u8]) -> io::Result<()> {
107+
pub fn exec(mut bytes: Vec<u8>) -> io::Result<()> {
108+
repeat_content_to_capacity(&mut bytes);
109+
let bytes = bytes.as_slice();
115110
let mut stdout = io::stdout().lock();
116111
loop {
117112
stdout.write_all(bytes)?;
118113
}
119114
}
120115

121116
#[cfg(any(target_os = "linux", target_os = "android"))]
122-
pub fn exec(bytes: &[u8], aligned: bool) -> io::Result<()> {
117+
pub fn exec(mut bytes: Vec<u8>) -> io::Result<()> {
123118
use uucore::pipes::{pipe, splice, tee};
119+
let aligned = PAGE_SIZE.is_multiple_of(bytes.len());
120+
repeat_content_to_capacity(&mut bytes);
121+
let bytes = bytes.as_slice();
124122
let mut stdout = io::stdout(); // no need to lock with zero-copy
125123
// don't show any error from fast-path and fallback to write for proper message
126124
if let Ok((p_read, mut p_write)) = pipe()
@@ -182,7 +180,7 @@ mod tests {
182180
for (line, final_len) in tests {
183181
let mut v = Vec::with_capacity(BUF_SIZE);
184182
v.extend(std::iter::repeat_n(b'a', line));
185-
prepare_buffer(&mut v);
183+
repeat_content_to_capacity(&mut v);
186184
assert_eq!(v.len(), final_len);
187185
}
188186
}

0 commit comments

Comments
 (0)