use std::os::fd::{AsRawFd, BorrowedFd};
use crate::{io::AsInnerRawHandle, op::SpliceOp};
pub async fn splice<'a, 'b>(
from: &'a impl AsRawFd,
to: &'b impl AsInnerRawHandle<'b>,
len: usize,
) -> Result<usize, std::io::Error> {
let from_handle = unsafe { BorrowedFd::borrow_raw(from.as_raw_fd()) };
let to_handle = to.as_inner_raw_handle();
let mut op = SpliceOp::new(from_handle, to_handle, len);
let result = std::future::poll_fn(move |cx| to_handle.poll_op(cx, &mut op)).await;
result
}
pub async fn splice_exact<'a, 'b>(
from: &'a impl AsRawFd,
to: &'b impl AsInnerRawHandle<'b>,
len: u64,
) -> Result<u64, std::io::Error> {
let mut total = 0;
while total < len {
let n = splice(from, to, (len - total).min(usize::MAX as u64) as usize).await?;
if n == 0 {
break;
}
total += n as u64;
}
Ok(total)
}