ranged_mmap/lib.rs
1//! High-performance lock-free concurrent file writing library
2//!
3//! 高性能无锁并发文件写入库
4//!
5//! This library provides high-performance memory-mapped file implementations optimized for
6//! concurrent random writes. It offers both type-safe and unsafe versions to balance safety
7//! and performance needs.
8//!
9//! 本库提供针对并发随机写入优化的高性能内存映射文件实现。
10//! 提供类型安全和 unsafe 版本以平衡安全性和性能需求。
11//!
12//! # Features
13//!
14//! - **Zero-copy writes**: Data is written directly to mapped memory
15//! - **Lock-free concurrency**: No locks needed for writes to different regions
16//! - **Type-safe API**: [`MmapFile`] prevents overlapping writes at compile-time
17//! - **High performance**: Avoids frequent system calls
18//! - **Runtime agnostic**: Works with any async runtime or without one
19//!
20//! # 特性
21//!
22//! - **零拷贝写入**:数据直接写入映射内存
23//! - **无锁并发**:不同区域的写入无需加锁
24//! - **类型安全 API**:[`MmapFile`] 在编译期防止重叠写入
25//! - **高性能**:避免频繁的系统调用
26//! - **运行时无关**:可用于任何异步运行时或无运行时环境
27//!
28//! # Quick Start
29//!
30//! ## Type-Safe Version (Recommended)
31//!
32//! Use [`MmapFile`] with [`RangeAllocator`] for compile-time safety:
33//!
34//! ## 类型安全版本(推荐)
35//!
36//! 使用 [`MmapFile`] 和 [`RangeAllocator`] 获得编译期安全:
37//!
38//! ```
39//! use ranged_mmap::{MmapFile, Result, allocator::ALIGNMENT};
40//! # use tempfile::tempdir;
41//! # fn main() -> Result<()> {
42//! # let dir = tempdir()?;
43//! # let path = dir.path().join("output.bin");
44//! # use std::num::NonZeroU64;
45//!
46//! // Create file and allocator (file size should be 4K aligned)
47//! // 创建文件和分配器(文件大小应为4K对齐)
48//! let (file, mut allocator) = MmapFile::create_default(&path, NonZeroU64::new(ALIGNMENT * 2).unwrap())?;
49//!
50//! // Allocate ranges (allocations are 4K aligned)
51//! // 分配范围(分配是4K对齐的)
52//! let range1 = allocator.allocate(NonZeroU64::new(ALIGNMENT).unwrap()).unwrap();
53//! let range2 = allocator.allocate(NonZeroU64::new(ALIGNMENT).unwrap()).unwrap();
54//!
55//! // Concurrent writes (compile-time safe!)
56//! // 并发写入(编译期安全!)
57//! std::thread::scope(|s| {
58//! let f1 = file.clone();
59//! let f2 = file.clone();
60//! s.spawn(move || f1.write_range(range1, &vec![1u8; ALIGNMENT as usize]));
61//! s.spawn(move || f2.write_range(range2, &vec![2u8; ALIGNMENT as usize]));
62//! });
63//!
64//! unsafe { file.sync_all()?; }
65//! # Ok(())
66//! # }
67//! ```
68//!
69//! ## Unsafe Version (Maximum Performance)
70//!
71//! Use [`MmapFileInner`] when you can guarantee safety:
72//!
73//! ## Unsafe 版本(最大性能)
74//!
75//! 当你能保证安全时使用 [`MmapFileInner`]:
76//!
77//! ```
78//! use ranged_mmap::{MmapFileInner, Result};
79//! # use tempfile::tempdir;
80//! # fn main() -> Result<()> {
81//! # let dir = tempdir()?;
82//! # let path = dir.path().join("download.bin");
83//! # use std::num::NonZeroU64;
84//!
85//! let file = MmapFileInner::create(&path, NonZeroU64::new(1024).unwrap())?;
86//!
87//! // ⚠️ You must ensure non-overlapping writes
88//! // ⚠️ 你必须确保写入不重叠
89//! let file1 = file.clone();
90//! let file2 = file.clone();
91//!
92//! std::thread::scope(|s| {
93//! // Safety: Non-overlapping regions
94//! // Safety: 不重叠的区域
95//! s.spawn(|| unsafe { file1.write_at(0, &[1; 512]) });
96//! s.spawn(|| unsafe { file2.write_at(512, &[2; 512]) });
97//! });
98//!
99//! unsafe { file.flush()?; }
100//! # Ok(())
101//! # }
102//! ```
103//!
104//! # Main Types
105//!
106//! - [`MmapFile`]: Type-safe memory-mapped file
107//! - [`MmapFileInner`]: Unsafe high-performance memory-mapped file
108//! - [`RangeAllocator`]: Allocates non-overlapping file ranges
109//! - [`AllocatedRange`]: Represents an allocated file range
110//! - [`WriteReceipt`]: Proof that a range has been written
111//! - [`SplitUpResult`]: Result of splitting with 4K upper alignment
112//! - [`SplitDownResult`]: Result of splitting with 4K lower alignment
113//!
114//! # 主要类型
115//!
116//! - [`MmapFile`][]: 类型安全的内存映射文件
117//! - [`MmapFileInner`]: Unsafe 高性能内存映射文件
118//! - [`RangeAllocator`][]: 分配不重叠的文件范围
119//! - [`AllocatedRange`][]: 表示已分配的文件范围
120//! - [`WriteReceipt`][]: 证明范围已被写入的凭据
121//! - [`SplitUpResult`][]: 4K上对齐拆分的结果
122//! - [`SplitDownResult`][]: 4K下对齐拆分的结果
123
124mod file;
125
126pub use file::*;