AutomaticallyHandled

Trait AutomaticallyHandled 

Source
pub trait AutomaticallyHandled: Sized {
    type HandleCoreType: HandleCore;

    // Required method
    fn serial(&self) -> usize;

    // Provided method
    fn handle(&self) -> Handle<Self> { ... }
}
Expand description

A data type for which handles can be automatically generated to identify its instances.

To do so, we need to implement the AutomaticallyHandled::serial method, that generates a unique serial-number with each instance of the type. This serial will be used to construct the identifying handle of the instance.

This usually serves for generating handles to instances of very simple data types, most commonly enums. Such data types can be so easily enumerated, that there’s no need to manage an auxiliary collection (e.g. HandledVec, HandledHashMap) to associate enumerated handles with their known instances.

§Example

enum MyLexemeType { Identifier, IntegerLiteral, If, While }
impl AutomaticallyHandled for MyLexemeType {
    type HandleCoreType = u8;
    fn serial(&self) -> usize { *self as usize }
}
let identifier_lexeme_type = MyLexemeType::Identifier.handle();

Required Associated Types§

Source

type HandleCoreType: HandleCore

The internal representation of this type’s handles (see HandleCore for more detail).

Required Methods§

Source

fn serial(&self) -> usize

Generate a serial-number that identifies this instance of the type.

Provided Methods§

Source

fn handle(&self) -> Handle<Self>

Get a handle to this instance of the type.

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.

Implementations on Foreign Types§

Source§

impl AutomaticallyHandled for u8

Implementors§