]> git.proxmox.com Git - pxar.git/blobdiff - src/util.rs
make ReadAt trait more correct
[pxar.git] / src / util.rs
index 79a8785b96c97536fc25cf5ca65995bc23e7eb8d..7389cb2110feab489e521f21085bde16db33bc6d 100644 (file)
@@ -5,6 +5,22 @@ use std::io;
 use std::pin::Pin;
 use std::task::{Context, Poll};
 
+pub const MAX_READ_BUF_LEN: usize = 4 * 1024 * 1024;
+
+pub fn scale_read_buffer(buffer: &mut Vec<u8>, target: usize) {
+    let target = target.min(MAX_READ_BUF_LEN);
+
+    if buffer.len() >= target {
+        buffer.truncate(target);
+        return;
+    }
+
+    buffer.reserve(target - buffer.len());
+    unsafe {
+        buffer.set_len(target);
+    }
+}
+
 // from /usr/include/linux/magic.h
 // and from casync util.h
 #[rustfmt::skip]