#mmap

mmap_buffer

(Mostly) safe wrapper for a memory-backed buffer

3 releases

0.1.4 Apr 21, 2023
0.1.3 Apr 21, 2023
0.1.2 Apr 1, 2023
0.1.1 Apr 1, 2023
0.1.0 Apr 1, 2023

#30 in #mmap

41 downloads per month

MIT license

12KB
208 lines

mmap-buffer

(Mostly) safe wrapper for a fixed-size file-backed buffer.

Example

use mmap_buffer::BackedBuffer;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    {
        let mut buf = BackedBuffer::<i32>::new(100, "test.data")?;

        // These changes will be reflected in the file
        buf[10] = -10;
        buf[20] = 27;
    }
    
    // Later, we can load the same array
    let mut buf = BackedBuffer::<i32>::load("test.data")?;

    assert_eq!(buf[10], -10);
    assert_eq!(buf[20], 27);

    Ok(())
}

lib.rs:

mmap-backed provides a (mostly) safe buffer which is backed by a file using an mmap system call. These buffers don't support dynamic reallocation, so they are fixed size. Here is an example:

use mmap_buffer::BackedBuffer;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    {
        let mut buf = BackedBuffer::<i32>::new(100, "test.data")?;

        // These changes will be reflected in the file
        buf[10] = -10;
        buf[20] = 27;
    }
    
    // Later, we can load the same array
    let mut buf = BackedBuffer::<i32>::load("test.data")?;

    assert_eq!(buf[10], -10);
    assert_eq!(buf[20], 27);

    Ok(())
}

Dependencies

~280–500KB