# `file_alloc`
[](https://github.com/share121/file_alloc/commits/main)
[](https://github.com/share121/file_alloc/actions)
[](https://crates.io/crates/file_alloc)
[](https://docs.rs/file_alloc)
[](https://github.com/share121/file_alloc/blob/main/LICENSE)
跨平台、高性能、兼容性好的文件大小分配库
- **跨平台**:支持 Windows、Linux、MacOS
- **高性能**:优先使用 fallocate 高速分配
- **兼容性好**:支持自动回退到全部写 0
- **取消安全**:可用随时中断分配
- **轻量**:只依赖与 `tokio`、`rustix`、`windows-sys`
```rust
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(())
}
```