Create a memory-mapped temporary file
(data: &[u8], config: &MemoryMappedConfig)
| 83 | |
| 84 | /// Create a memory-mapped temporary file |
| 85 | fn create_temp_mapped(data: &[u8], config: &MemoryMappedConfig) -> io::Result<Self> { |
| 86 | let mut temp_file = if let Some(ref dir) = config.temp_dir { |
| 87 | NamedTempFile::new_in(dir)? |
| 88 | } else { |
| 89 | NamedTempFile::new()? |
| 90 | }; |
| 91 | |
| 92 | // Write data to temp file |
| 93 | temp_file.write_all(data)?; |
| 94 | temp_file.flush()?; |
| 95 | |
| 96 | // Memory map the file |
| 97 | let file = temp_file.reopen()?; |
| 98 | // Safety: This is safe because: |
| 99 | // 1. The file is freshly created and written with valid data |
| 100 | // 2. memmap2::Mmap::map() handles the OS-level memory mapping safely |
| 101 | // 3. The file descriptor remains valid for the lifetime of the mapping |
| 102 | let mmap = unsafe { Mmap::map(&file)? }; |
| 103 | |
| 104 | debug!("Created memory-mapped value: {} bytes", data.len()); |
| 105 | |
| 106 | Ok(MappedValue::Mapped { |
| 107 | mmap, |
| 108 | offset: 0, |
| 109 | length: data.len(), |
| 110 | _temp_file: Some(temp_file), |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | /// Get a slice view of the data |
| 115 | pub fn as_slice(&self) -> &[u8] { |