Skip to main content

EnumDef

Struct EnumDef 

Source
pub struct EnumDef<E: EnumRef> { /* private fields */ }
Expand description

Registers a value of the enum type E, then its attributes.

new registers the value at once, and set_attr sets each attribute on it, which EnumRef::attr_map reads. The value’s object is an EnumObj, so E’s object type adds no fields to its parent EnumObj.

As in C++, values and attributes register during static initialization, such as in a static_init_block!, before other threads read them: registrations from Rust lock each other out, but readers take no lock.

§Examples

use tvm_ffi::derive::{Object, ObjectRef};
use tvm_ffi::reflection::EnumDef;
use tvm_ffi::{EnumRef, ObjectArc};
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 {}

let red = EnumDef::<Color>::new("red")?.set_attr("my_ext.hex", 0xff0000_i64)?.value();

let hex = Color::attr_map::<i64>("my_ext.hex")?.unwrap();
assert_eq!(hex.get(&red)?, Some(0xff0000));

Implementations§

Source§

impl<E: EnumRef> EnumDef<E>

Source

pub fn new(name: &str) -> Result<Self>
where E: PlainObjectRef,

Register the value name of E.

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

The builder of the value’s attributes.

§Errors

Returns a ValueError if name is empty or E already has a value named name, and a TypeError if E is not an enum type, is an integer enum (ffi.IntEnum), whose values name their own indices, or registers an object size other than that of EnumObj: an enum type whose objects have fields of their own.

E accepts every object of its type, as a non-generic #[derive(ObjectRef)] reference does.

Source

pub fn set_attr( self, name: &str, value: impl Into<Any> + Send + Sync, ) -> Result<Self>

Set the attribute name of the value.

See also EnumRef::attr_map.

§Arguments
  • name - The name of the attribute.
  • value - The value of the attribute, Send + Sync since every thread reads it.
§Returns

The builder, for the next attribute.

§Errors

Returns a TypeError if value is an opaque pointer: set a native hook with set_native_fn_attr.

Source

pub unsafe fn set_native_fn_attr<Args, R>( self, name: &str, f: NativeFunction<Args, R>, ) -> Result<Self>

Set the native hook f as the attribute name of the value.

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

The builder, for the next attribute.

§Safety

Every caller of the attribute name calls its hooks with exactly Args and reads R: a native caller calls the function pointer unchecked.

Source

pub fn value(&self) -> E

The registered value.

Auto Trait Implementations§

§

impl<E> Freeze for EnumDef<E>
where E: Freeze,

§

impl<E> RefUnwindSafe for EnumDef<E>
where E: RefUnwindSafe,

§

impl<E> Send for EnumDef<E>
where E: Send,

§

impl<E> Sync for EnumDef<E>
where E: Sync,

§

impl<E> Unpin for EnumDef<E>
where E: Unpin,

§

impl<E> UnsafeUnpin for EnumDef<E>
where E: UnsafeUnpin,

§

impl<E> UnwindSafe for EnumDef<E>
where E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeSubsumes<T> for T
where T: ?Sized,