#region #mutable #buffer #shared #reference

region_buffer

一个可增长的数组,允许存在多个可变且不重叠的区域

6个版本

0.1.5 2018年12月10日
0.1.4 2018年12月9日

#72 in #region

MIT/Apache

19KB
320

区域缓冲区

Linux build status Documentation Donate using Liberapay

一个可增长的数组,允许从同一个 Vec 获取多个可变且不重叠的区域。

region_buffer = "0.1"

示例

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);

提供了一个 region_buffer 宏,以使初始化更方便。

#[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.")
}

该宏还可以用于指定和初始化大块内存。

#[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);
}

无运行时依赖