buf_copy: remove thin wrapper & useless conversion

This commit is contained in:
oech3
2026-05-27 10:07:44 +02:00
committed by Daniel Hofstetter
parent fa6d68d6c2
commit 8de3238dca
3 changed files with 16 additions and 66 deletions
+16 -6
View File
@@ -10,17 +10,27 @@
//! does not handle copying special files (e.g pipes, character/block devices).
#[cfg(any(target_os = "linux", target_os = "android"))]
pub mod linux;
use std::os::fd::AsFd;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub use linux::*;
pub fn copy_stream(
src: &mut (impl std::io::Read + AsFd),
dest: &mut impl AsFd,
) -> std::io::Result<()> {
// try to splice() system call for throughput
if crate::pipes::splice_unbounded_auto(src, dest)?.is_err() {
// fall back on writing "without buffering", or order of output would be wrong
// unrelated for cp /dev/stdin since cp does not have multiple input? <https://github.com/uutils/coreutils/issues/5186>
// RawWriter also removes io::copy's specialization slower than our splice
std::io::copy(src, &mut crate::io::RawWriter(dest))?;
}
Ok(())
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub mod other;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub use other::copy_stream;
pub use std::io::copy as copy_stream;
#[cfg(test)]
#[cfg(any(target_os = "linux", target_os = "android"))] // copy_stream is a thin wrapper for io::copy. nothing to test...
#[cfg(any(target_os = "linux", target_os = "android"))] // copy_stream is io::copy on other platforms. nothing to test.
mod tests {
use super::*;
use std::fs::File;
@@ -1,33 +0,0 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//! Buffer-based copying implementation for Linux and Android.
use std::os::fd::AsFd;
/// Copy data from `Read` implementor `source` into a `Write` implementor
/// `dest`. This works by reading a chunk of data from `source` and writing the
/// data to `dest` in a loop.
///
/// This function uses the Linux-specific `splice` call when possible which does
/// not use any intermediate user-space buffer. It falls backs to
/// `std::io::copy` when the call fails and is still recoverable.
///
/// # Arguments
/// * `source` - `Read` implementor to copy data from.
/// * `dest` - `Write` implementor to copy data to.
pub fn copy_stream(
src: &mut (impl std::io::Read + AsFd),
dest: &mut impl AsFd,
) -> crate::error::UResult<()> {
// If we're on Linux or Android, try to use the splice() system call
// for faster writing. If it works, we're done.
if crate::pipes::splice_unbounded_auto(src, dest)?.is_err() {
// If the splice() call failed, fall back on writing "without buffering", or order of output would be wrong
// unrelated for cp /dev/stdin since cp does not have multiple input? <https://github.com/uutils/coreutils/issues/5186>
// RawWriter also removes io::copy's specialization
std::io::copy(src, &mut crate::io::RawWriter(dest))?;
}
Ok(())
}
@@ -1,27 +0,0 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//!
//! Buffer-based copying implementation for other platforms.
use std::io::{Read, Write};
use crate::error::UResult;
/// Copy data from `Read` implementor `source` into a `Write` implementor
/// `dest`. This works by reading a chunk of data from `source` and writing the
/// data to `dest` in a loop, using std::io::copy. This is implemented for
/// non-Linux platforms.
///
/// # Arguments
/// * `source` - `Read` implementor to copy data from.
/// * `dest` - `Write` implementor to copy data to.
pub fn copy_stream<R, S>(src: &mut R, dest: &mut S) -> UResult<()>
where
R: Read,
S: Write,
{
std::io::copy(src, dest)?;
Ok(())
}