[][src]Crate compact_arena

A crate with a few single-typed arenas that work exclusively with indexes. The indexes are sized with the arena. This can reduce memory footprint when changing pointers to indices e.g. on 64-bit systems.

The arenas use a variant of "branded indices": The indices contain a type tag that binds them to their respective arena so you cannot mix up two arenas by accident. Unlike the indexing crate, this implements the type tags as actual unique types. This has the downside of not being able to Sync or Send arenas or indices, but on the other hand, we can store indices within objects that we put into the arena, which is a boon to things like graph data structures.

A word of warning: The soundness of this isn't proven, and it may well be possible to use the macros provided in this crate to create undefined behavior. For simple use cases, you should be pretty safe.

Use the in_arena and similar methods to run code within the scope of an arena:

Examples

in_arena!(arena, {
    let idx = arena.add(1usize);
    assert_eq!(1, arena[idx]);
});

You can use in_sized_arena to change the initial size of the backing memory:

in_arena!(arena / 1, {
    ..
});

You can nest calls to work with multiple arenas:

in_arena!(a / 1, {
    in_arena!(b / 1, {
        ..
    })
});

The compiler gives you a type error if you mix up arenas:

This example deliberately fails to compile
in_arena!(a / 1, {
    in_arena!(b / 1, {
        let i = a.add(1usize);
        let _ = b[i];
    })
});

Macros

in_arena

Run code using an arena. The indirection through the macro is required to safely bind the indices to the arena. The macro takes an identifier that will be bound to the &mut Arena<_, _> and an expression that will be executed within a block where the arena is instantiated. The arena will be dropped afterwards.

in_nano_arena

Run code using a nano arena. The indirection through the macro is required to bind the indices to the arena.

in_tiny_arena

Run code using a tiny arena. The indirection through this macro is required to bind the indices to the arena.

Structs

Idx

An index into the arena. You will not directly use this type, but one of the aliases this crate provides (Idx32, Idx16 or Idx8).

NanoArena

A "nano" arena containing 255 elements. This variant only works with types implementing Default.

SmallArena

A "Small" arena based on a resizable slice (i.e. a Vec) that can be indexed with 32-bit Idx32s. This can help reduce memory overhead when using many pointer-heavy objects on 64-bit systems.

TinyArena

A "tiny" arena containing <64K elements. This variant only works with types implementing Default.

Type Definitions

Idx8

The index type for a nano arena is 8 bits large. You will usually get the index from the arena and use it by indexing, e.g. arena[index].

Idx16

The index type for a tiny arena is 16 bits large. You will usually get the index from the arena and use it by indexing, e.g. arena[index].

Idx32

The index type for a small arena is 32 bits large. You will usually get the index from the arena and use it by indexing, e.g. arena[index].