#region #mutable #buffer #shared #reference

region_buffer

A growable array allowing for multiple mutable non overlapping regions

6 releases

0.1.5 Dec 10, 2018
0.1.4 Dec 9, 2018

#70 in #region

MIT/Apache

19KB
320 lines

Region Buffer

Linux build status Documentation Donate using Liberapay

A growable array allowing for multiple mutable non overlapping regions from the same Vec.

region_buffer = "0.1"

Examples

use region_buffer::RegionBuffer;
let mut buffer = RegionBuffer::new();

buffer.push(1);
buffer.push(2);

assert_eq!(buffer.len(), 2);

let mut a = buffer.get_mut(0);
let mut b = buffer.get_mut(1);

assert_eq!(*a, 1);
assert_eq!(*b, 2);

*a = *b;
*b = 3;

assert_eq!(*a, 2);
assert_eq!(*b, 3);

There is a region_buffer macro provided to make initialisation more convenient.

#[macro_use]
extern crate region_buffer;

fn main() {
    let strings = region_buffer!["Hello", "World", "!"];

    let mut greeting =  strings.get_mut(0);
    let mut noun =  strings.get_mut(1);
    let mut punctuation =  strings.get_mut(2);

    *greeting = "Hallo";
    *noun = "Peter";
    *punctuation = ".";

    let string = format!("{} {}{}", greeting, noun, punctuation);
    assert_eq!(string, "Hallo Peter.")
}

The macro can also be used to specify and initialise large regions of memory.

#[macro_use]
extern crate region_buffer;

type Slice<'a> = region_buffer::Slice<'a, u8>;

fn main() {
    let memory = region_buffer![0; 0xFFFF];

    let rom =  memory.region(0, 0x800);
    let gpu = memory.region(0x800, 0x1600);
    let sound = memory.region(0x1600, 0x2400);
    let ram = memory.region(0x2400, 0xFFFF);

    let console = Console::new(rom, gpu, sound, ram);
}

No runtime deps