use std::fs::File;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::io::IoSlice;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::os::fd::AsFd;
#[cfg(any(target_os = "linux", target_os = "android"))]
use nix::fcntl::SpliceFFlags;
pub use nix::{Error, Result};
pub fn pipe() -> Result<(File, File)> {
let (read, write) = nix::unistd::pipe()?;
Ok((File::from(read), File::from(write)))
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn splice(source: &impl AsFd, target: &impl AsFd, len: usize) -> Result<usize> {
nix::fcntl::splice(source, None, target, None, len, SpliceFFlags::empty())
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn splice_exact(source: &impl AsFd, target: &impl AsFd, len: usize) -> Result<()> {
let mut left = len;
while left != 0 {
let written = splice(source, target, left)?;
assert_ne!(written, 0, "unexpected end of data");
left -= written;
}
Ok(())
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn vmsplice(target: &impl AsFd, bytes: &[u8]) -> Result<usize> {
nix::fcntl::vmsplice(target, &[IoSlice::new(bytes)], SpliceFFlags::empty())
}