()
| 18 | } |
| 19 | |
| 20 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 21 | println!("--- Parcode Memory Buffer Example ---"); |
| 22 | |
| 23 | // 1. Create some data |
| 24 | let user = UserProfile { |
| 25 | id: 12345, |
| 26 | username: "generic_writer_fan".to_string(), |
| 27 | tags: vec!["rust".into(), "parcode".into(), "memory".into()], |
| 28 | scores: vec![100, 200, 300, 400, 500], |
| 29 | }; |
| 30 | |
| 31 | println!("Original data: {:?}", user); |
| 32 | |
| 33 | // 2. Prepare a memory buffer (Vec<u8>) |
| 34 | // Parcode will write into this vector. |
| 35 | // Note: The vector will grow as needed. |
| 36 | let mut buffer: Vec<u8> = Vec::new(); |
| 37 | |
| 38 | // 3. Serialize to the buffer |
| 39 | // We use `write` which accepts any W: Write + Send |
| 40 | // Vec<u8> implements Write and Send. |
| 41 | println!("Serializing to memory buffer..."); |
| 42 | Parcode::builder() |
| 43 | .compression(true) // Optional: enable compression |
| 44 | .write(&mut buffer, &user)?; |
| 45 | |
| 46 | println!( |
| 47 | "Serialization complete. Buffer size: {} bytes", |
| 48 | buffer.len() |
| 49 | ); |
| 50 | |
| 51 | // 4. Verify the buffer content (Forensics) |
| 52 | // The last 26 bytes should be the Global Header. |
| 53 | // Let's print the first few bytes (Magic) and the last few. |
| 54 | if buffer.len() > 30 { |
| 55 | println!( |
| 56 | "Magic bytes: {:02X?}", |
| 57 | &buffer.get(0..8).expect("Failed to get magic bytes") |
| 58 | ); |
| 59 | println!( |
| 60 | "Tail bytes: {:02X?}", |
| 61 | &buffer |
| 62 | .get(buffer.len() - 26..) |
| 63 | .expect("Failed to get tail bytes") |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | // 5. (Optional) Write buffer to disk to verify with standard reader |
| 68 | // In a real app, you might send this buffer over network, store in DB, etc. |
| 69 | let path = "memory_dump.par"; |
| 70 | std::fs::write(path, &buffer)?; |
| 71 | println!("Dumped buffer to '{}' for verification.", path); |
| 72 | |
| 73 | // 6. Read it back using standard Parcode::read |
| 74 | // Note: Currently Parcode::read expects a file path because it uses memory mapping. |
| 75 | // Reading from a memory buffer directly (without file) would require a `ParcodeReader::from_bytes` |
| 76 | // which is a separate feature (Reader refactor). |
| 77 | // For now, we verify by reading the dumped file. |
nothing calls this directly
no test coverage detected