[][src]Trait id_arena::ArenaBehavior

pub trait ArenaBehavior {
    type Id: Copy;
    fn new_id(arena_id: u32, index: usize) -> Self::Id;
fn index(_: Self::Id) -> usize;
fn arena_id(_: Self::Id) -> u32; fn new_arena_id() -> u32 { ... } }

A trait representing the implementation behavior of an arena and how identifiers are represented.

When should I implement ArenaBehavior myself?

Usually, you should just use DefaultArenaBehavior, which is simple and correct. However, there are some scenarios where you might want to implement ArenaBehavior yourself:

  • Space optimizations: The default identifier is two words in size, which is larger than is usually necessary. For example, if you know that an arena cannot contain more than 256 items, you could make your own identifier type that stores the index as a u8 and then you can save some space.

  • Trait Coherence: If you need to implement an upstream crate's traits for identifiers, then defining your own identifier type allows you to work with trait coherence rules.

  • Share identifiers across arenas: You can coordinate and share identifiers across different arenas to enable a "struct of arrays" style data representation.

Associated Types

type Id: Copy

The identifier type.

Loading content...

Required methods

fn new_id(arena_id: u32, index: usize) -> Self::Id

Construct a new object identifier from the given index and arena identifier.

Panics

Implementations are allowed to panic if the given index is larger than the underlying storage (e.g. the implementation uses a u8 for storing indices and the given index value is larger than 255).

fn index(_: Self::Id) -> usize

Get the given identifier's index.

fn arena_id(_: Self::Id) -> u32

Get the given identifier's arena id.

Loading content...

Provided methods

fn new_arena_id() -> u32

Construct a new arena identifier.

This is used to disambiguate Ids across different arenas. To make identifiers with the same index from different arenas compare false for equality, return a unique u32 on every invocation. This is the default, provided implementation's behavior.

To make identifiers with the same index from different arenas compare true for equality, return the same u32 on every invocation.

Loading content...

Implementors

impl<T> ArenaBehavior for DefaultArenaBehavior<T>[src]

type Id = Id<T>

fn new_arena_id() -> u32[src]

Loading content...