Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/uucore/src/lib/features/buf_copy/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ use std::{
os::fd::AsFd,
};

/// A readable file descriptor.
pub trait FdReadable: Read + AsFd {}

impl FdReadable for std::fs::File {}
impl FdReadable for std::io::PipeReader {}

/// A writable file descriptor.
pub trait FdWritable: Write + AsFd {}

impl FdWritable for std::fs::File {}
impl FdWritable for std::io::PipeWriter {}

/// 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.
Expand All @@ -24,10 +36,10 @@ use std::{
/// # 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<()>
pub fn copy_stream<R, W>(src: &mut R, dest: &mut W) -> UResult<()>
where
R: Read + AsFd,
S: Write + AsFd,
R: FdReadable,
W: FdWritable,
{
// If we're on Linux or Android, try to use the splice() system call
// for faster writing. If it works, we're done.
Expand All @@ -38,12 +50,5 @@ where

// If the splice() call failed, fall back on slower writing.
std::io::copy(src, dest)?;

// If the splice() call failed and there has been some data written to
// stdout via while loop above AND there will be second splice() call
// that will succeed, data pushed through splice will be output before
// the data buffered in stdout.lock. Therefore additional explicit flush
// is required here.
dest.flush()?;
Ok(())
}
Loading