Skip to main content

Crate file_alloc

Crate file_alloc 

Source
Expand description

§file_alloc

GitHub last commit Test Latest version Documentation License

跨平台、高性能、兼容性好的文件大小分配库

  • 跨平台:支持 Windows、Linux、MacOS
  • 高性能:优先使用 fallocate 高速分配
  • 兼容性好:支持自动回退到全部写 0
  • 取消安全:可用随时中断分配
  • 轻量:只依赖与 tokiorustixwindows-sys
use file_alloc::{init_fast_alloc, FileAlloc};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    init_fast_alloc(); // 必须在打开文件前调用,否则无法享受快分配

    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(())
}

Traits§

FileAlloc

Functions§

init_fast_alloc
Unix 下不需要提权,提供统一接口并直接返回 true