use std::fs::File;
use std::io::Write;
use tempfile::NamedTempFile;
#[cfg(feature = "mmap")]
use zipora::{DataInput, DataOutput, MemoryMappedInput, MemoryMappedOutput};
#[cfg(feature = "mmap")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("πΊοΈ Memory-Mapped I/O Demo for zipora");
println!("==========================================\n");
let temp_file = NamedTempFile::new()?;
let file_path = temp_file.path();
println!("π Created temporary file: {:?}", file_path);
println!();
println!("π€ PART 1: Memory-Mapped Output (Writing)");
println!("------------------------------------------");
{
let mut output = MemoryMappedOutput::create(file_path, 512)?;
println!("β
Created MemoryMappedOutput with 512 bytes initial capacity");
output.write_u32(0x12345678)?;
println!(" Wrote u32: 0x12345678");
output.write_u64(0x9ABCDEF012345678)?;
println!(" Wrote u64: 0x9ABCDEF012345678");
output.write_var_int(300)?;
println!(" Wrote var_int: 300");
output.write_length_prefixed_string("Hello, Memory Mapping!")?;
println!(" Wrote string: \"Hello, Memory Mapping!\"");
let large_data = vec![0xAB; 1000];
output.write_slice(&large_data)?;
println!(" Wrote 1000 bytes of data (testing automatic growth)");
println!(" Current position: {} bytes", output.position());
println!(" Current capacity: {} bytes", output.capacity());
println!(" Remaining space: {} bytes", output.remaining());
output.flush()?;
println!("β
Flushed data to disk");
output.truncate()?;
println!(
"β
Truncated file to actual data size: {} bytes",
output.capacity()
);
}
println!();
println!("π₯ PART 2: Memory-Mapped Input (Reading)");
println!("----------------------------------------");
{
let file = File::open(file_path)?;
let mut input = MemoryMappedInput::new(file)?;
println!("β
Created MemoryMappedInput");
println!(" File size: {} bytes", input.len());
println!(" Initial position: {}", input.position());
println!(" Available bytes: {}", input.remaining());
let value1 = input.read_u32()?;
println!(
" Read u32: 0x{:08X} ({})",
value1,
if value1 == 0x12345678 {
"β
correct"
} else {
"β incorrect"
}
);
let value2 = input.read_u64()?;
println!(
" Read u64: 0x{:016X} ({})",
value2,
if value2 == 0x9ABCDEF012345678 {
"β
correct"
} else {
"β incorrect"
}
);
let var_int = input.read_var_int()?;
println!(
" Read var_int: {} ({})",
var_int,
if var_int == 300 {
"β
correct"
} else {
"β incorrect"
}
);
let text = input.read_length_prefixed_string()?;
println!(
" Read string: \"{}\" ({})",
text,
if text == "Hello, Memory Mapping!" {
"β
correct"
} else {
"β incorrect"
}
);
let slice = input.read_slice(10)?;
println!(" Read 10 bytes (zero-copy): {:02X?}", slice);
println!(" Position after reads: {}", input.position());
println!(" Remaining bytes: {}", input.remaining());
}
println!();
println!("π§ PART 3: Advanced Operations");
println!("------------------------------");
{
let file = File::open(file_path)?;
let mut input = MemoryMappedInput::new(file)?;
input.seek(4)?; println!("β
Seeked to position 4");
let value = input.read_u64()?;
println!(" Read u64 after seek: 0x{:016X}", value);
input.seek(0)?;
let peeked = input.peek_slice(4)?;
println!(" Peeked first 4 bytes: {:02X?}", peeked);
println!(
" Position after peek: {} (should still be 0)",
input.position()
);
input.skip(12)?; println!(" Skipped 12 bytes, new position: {}", input.position());
let var_int = input.read_var_int()?;
println!(" Read var_int after skip: {}", var_int);
}
println!();
println!("π PART 4: Performance Benefits");
println!("-------------------------------");
println!("Memory-mapped I/O advantages:");
println!("β’ Zero-copy operations - no intermediate buffers");
println!("β’ Operating system handles caching and paging");
println!("β’ Efficient random access patterns");
println!("β’ Automatic file growth for writes");
println!("β’ Cross-platform compatibility");
println!("β’ Memory safety through Rust's type system");
println!();
println!("π― Use cases:");
println!("β’ Large file processing");
println!("β’ Database storage engines");
println!("β’ Index file management");
println!("β’ Log file processing");
println!("β’ Scientific data analysis");
println!();
println!("β
Memory-mapping demonstration completed successfully!");
Ok(())
}
#[cfg(not(feature = "mmap"))]
fn main() {
println!("β οΈ Memory mapping feature is not enabled!");
println!("To run this example, enable the 'mmap' feature:");
println!("cargo run --example memory_mapping_demo --features mmap");
}