file_alloc
跨平台、高性能、兼容性好的文件大小分配库
- 跨平台:支持 Windows、Linux、MacOS
- 高性能:优先使用 fallocate 高速分配
- 兼容性好:支持自动回退到全部写 0
- 取消安全:可用随时中断分配
- 轻量:只依赖与
tokio、rustix、windows-sys
use FileAlloc;
async
file_alloc跨平台、高性能、兼容性好的文件大小分配库
tokio、rustix、windows-sysuse file_alloc::FileAlloc;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let temp_file = tempfile::NamedTempFile::new()?;
let mut file = tokio::fs::File::options()
.read(true)
.write(true)
.open(temp_file.path())
.await?;
let target_size = 5 * 1024 * 1024;
file.allocate(target_size).await?; // 文件预分配
let metadata = file.metadata().await?;
assert_eq!(metadata.len(), target_size);
Ok(())
}