libsql_wal/io/
compat.rs

1use std::io;
2
3use bytes::BytesMut;
4use tokio::io::AsyncRead;
5use tokio::io::AsyncReadExt;
6
7use super::FileExt;
8
9/// Copy from src that implements AsyncRead to the detination file, returning how many bytes have
10/// been copied
11pub async fn copy_to_file<R, F>(mut src: R, dst: &F) -> io::Result<usize>
12where
13    F: FileExt,
14    R: AsyncRead + Unpin,
15{
16    let mut dst_offset = 0u64;
17    let mut buffer = BytesMut::with_capacity(4096);
18    loop {
19        let n = src.read_buf(&mut buffer).await?;
20        if n == 0 {
21            return Ok(dst_offset as usize);
22        }
23        let (b, ret) = dst.write_all_at_async(buffer, dst_offset).await;
24        ret?;
25        dst_offset += n as u64;
26        buffer = b;
27        buffer.clear();
28    }
29}