id_arena

Trait ArenaBehavior

Source
pub trait ArenaBehavior {
    type Id: Copy;

    // Required methods
    fn new_id(arena_id: u32, index: usize) -> Self::Id;
    fn index(_: Self::Id) -> usize;
    fn arena_id(_: Self::Id) -> u32;

    // Provided method
    fn new_arena_id() -> u32 { ... }
}
Expand description

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.

Required Associated Types§

Source

type Id: Copy

The identifier type.

Required Methods§

Source

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

Source

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

Get the given identifier’s index.

Source

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

Get the given identifier’s arena id.

Provided Methods§

Source

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§