Skip to main content

EnumRef

Trait EnumRef 

Source
pub trait EnumRef: ObjectRefCore {
    // Provided methods
    fn get(name: &str) -> Result<Self> { ... }
    fn name(&self) -> &str { ... }
    fn attr_map<V: AnyCompatible>(
        name: &str,
    ) -> Result<Option<EnumAttrMap<Self, V>>> { ... }
}
Expand description

A reference to an enum value: one named singleton of an enum type, registered once and shared with every language.

get looks a value up by name. Each name has one value, so equality is identity: compare values with same_as. EnumDef registers values and their attributes, and attr_map reads an attribute.

The stub generator implements it for the enum types it binds. A hand-written reference implements it when its object’s parent is EnumObj.

§Examples

use tvm_ffi::derive::{Object, ObjectRef};
use tvm_ffi::reflection::EnumDef;
use tvm_ffi::{EnumRef, ObjectArc, ObjectRefCore};
use tvm_ffi::enum_ref::EnumObj;

#[repr(C)]
#[derive(Object)]
#[object(type_key = "my_ext.Color", register, final, immutable)]
pub struct ColorObj {
    base_: EnumObj,
}

#[derive(Clone, ObjectRef)]
pub struct Color {
    data: ObjectArc<ColorObj>,
}

impl EnumRef for Color {}

EnumDef::<Color>::new("red")?;

let red = Color::get("red")?;
assert_eq!(red.name(), "red");
assert!(red.same_as(&Color::get("red")?));

Provided Methods§

Source

fn get(name: &str) -> Result<Self>

Look up the value registered as name.

§Arguments
  • name - The name of the value.
§Returns

The value, the same object on every call.

§Errors

Returns a ValueError if the enum type has no value named name.

Source

fn name(&self) -> &str

The name of the value, unique within its enum type.

§Panics

Panics if the object of Self does not have EnumObj as an ancestor, which only an incorrect impl EnumRef allows.

Source

fn attr_map<V: AnyCompatible>( name: &str, ) -> Result<Option<EnumAttrMap<Self, V>>>

The attribute name of the values of the enum type, or None when no value has it.

The map holds the registry’s column, so it also sees the attributes set after it was read. See also EnumDef::set_attr.

§Arguments
  • name - The name of the attribute.
§Returns

The attribute of every value that has it, read as V.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§