Skip to content

Commit 83b09a4

Browse files
oech3cakebaker
authored andcommitted
pipes.rs: simplify trait and match
1 parent 547a490 commit 83b09a4

1 file changed

Lines changed: 11 additions & 20 deletions

File tree

src/uucore/src/lib/features/pipes.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
#[cfg(any(target_os = "linux", target_os = "android"))]
99
use rustix::pipe::{SpliceFlags, fcntl_setpipe_size};
1010
#[cfg(any(target_os = "linux", target_os = "android"))]
11-
use std::fs::File;
12-
#[cfg(any(target_os = "linux", target_os = "android"))]
1311
use std::{
14-
io::{Read, Write},
12+
fs::File,
13+
io::{PipeReader, PipeWriter, Read, Write},
1514
os::fd::AsFd,
1615
sync::OnceLock,
1716
};
@@ -27,19 +26,17 @@ const KERNEL_DEFAULT_PIPE_SIZE: usize = 64 * 1024;
2726
/// used for resolving the limitation for splice: one of a input or output should be pipe
2827
#[inline]
2928
#[cfg(any(target_os = "linux", target_os = "android"))]
30-
pub fn pipe<const SIZE_REQUIRED: bool>(
31-
s: usize,
32-
) -> std::io::Result<(std::io::PipeReader, std::io::PipeWriter)> {
33-
let (read, write) = std::io::pipe()?;
29+
pub fn pipe<const SIZE_REQUIRED: bool>(s: usize) -> std::io::Result<(PipeReader, PipeWriter)> {
30+
let pair = std::io::pipe()?;
3431
// guard unnecessary syscall
3532
if s > KERNEL_DEFAULT_PIPE_SIZE {
36-
let r = fcntl_setpipe_size(&read, s);
33+
let r = fcntl_setpipe_size(&pair.0, s);
3734
if SIZE_REQUIRED {
3835
r?;
3936
}
4037
}
4138

42-
Ok((read, write))
39+
Ok(pair)
4340
}
4441

4542
/// Less noisy wrapper around [`rustix::pipe::splice`].
@@ -87,11 +84,7 @@ pub fn might_fuse(source: &impl AsFd) -> bool {
8784
/// fails if one of in/output should be pipe
8885
#[inline]
8986
#[cfg(any(target_os = "linux", target_os = "android"))]
90-
pub fn splice_unbounded<R, S>(source: &R, dest: &mut S) -> std::io::Result<bool>
91-
where
92-
R: Read + AsFd,
93-
S: AsFd,
94-
{
87+
pub fn splice_unbounded(source: &impl AsFd, dest: &mut impl AsFd) -> std::io::Result<bool> {
9588
// improve throughput
9689
// todo: avoid fcntl overhead for small input, but don't fcntl inside of the loop
9790
// no need to increase pipe size of input fd since
@@ -118,8 +111,7 @@ where
118111
R: Read + AsFd,
119112
S: AsFd,
120113
{
121-
static PIPE_CACHE: OnceLock<Option<(std::io::PipeReader, std::io::PipeWriter)>> =
122-
OnceLock::new();
114+
static PIPE_CACHE: OnceLock<Option<(PipeReader, PipeWriter)>> = OnceLock::new();
123115
let Some((pipe_rd, pipe_wr)) = PIPE_CACHE
124116
.get_or_init(|| pipe::<false>(MAX_ROOTLESS_PIPE_SIZE).ok())
125117
.as_ref()
@@ -163,8 +155,7 @@ pub fn send_n_bytes(
163155
mut target: impl Write + AsFd,
164156
n: u64,
165157
) -> std::io::Result<u64> {
166-
static PIPE_CACHE: OnceLock<Option<(std::io::PipeReader, std::io::PipeWriter)>> =
167-
OnceLock::new();
158+
static PIPE_CACHE: OnceLock<Option<(PipeReader, PipeWriter)>> = OnceLock::new();
168159
let pipe_size = MAX_ROOTLESS_PIPE_SIZE.min(n as usize);
169160
let mut n = n;
170161
let mut bytes_written: u64 = 0;
@@ -187,7 +178,7 @@ pub fn send_n_bytes(
187178
loop {
188179
match splice(&input, &target, n as usize) {
189180
Ok(0) => break might_fuse(&input),
190-
Ok(s @ 1..) => {
181+
Ok(s) => {
191182
n -= s as u64;
192183
bytes_written += s as u64;
193184
}
@@ -202,7 +193,7 @@ pub fn send_n_bytes(
202193
loop {
203194
match splice(&input, &broker_w, n as usize) {
204195
Ok(0) => break might_fuse(&input),
205-
Ok(s @ 1..) => {
196+
Ok(s) => {
206197
if splice_exact(&broker_r, &target, s).is_ok() {
207198
n -= s as u64;
208199
bytes_written += s as u64;

0 commit comments

Comments
 (0)