extern crate libmmap; extern crate libc; extern crate tempdir; use libmmap::{MemoryMap, MemoryMapOption}; #[test] fn memory_map_rw() { let mut chunk = match MemoryMap::new(16, &[MemoryMapOption::Readable, MemoryMapOption::Writable]) { Ok(chunk) => chunk, Err(msg) => panic!("Allocation failed: {:?}", msg), }; assert!(chunk.len() >= 16); chunk.set_data(0xBE as *mut u8); assert!(chunk.data() == 0xBE as *mut u8); } #[test] fn memory_map_file() { use std::fs; use std::io::{Seek, SeekFrom, Write}; #[cfg(unix)] fn get_fd(file: &fs::File) -> libc::c_int { use std::os::unix::io::AsRawFd; file.as_raw_fd() } #[cfg(windows)] fn get_fd(file: &fs::File) -> libc::HANDLE { use std::os::windows::io::AsRawHandle; file.as_raw_handle() } let tmpdir = tempdir::TempDir::new("").expect("Couldn't create tempdir"); let mut path = tmpdir.path().to_path_buf(); path.push("mmap_file.tmp"); let size = MemoryMap::granularity() * 2; let mut file = fs::OpenOptions::new() .create(true) .read(true) .write(true) .open(&path) .expect("Couldn't create temd file"); file.seek(SeekFrom::Start(size as u64)).unwrap(); file.write(b"\0").unwrap(); let fd = get_fd(&file); let mut chunk = MemoryMap::new(size / 2, &[MemoryMapOption::Readable, MemoryMapOption::Writable, MemoryMapOption::Fd(fd), MemoryMapOption::Offset(size / 2)]) .unwrap(); assert!(chunk.len() > 0); chunk.set_data(0xbe as *mut u8); chunk.set_data(0xbe as *mut u8); assert!(chunk.data() == 0xbe as *mut u8); fs::remove_file(&path).expect("Couldn't remove file"); }