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>
impl<E: EnumRef> EnumDef<E>
Sourcepub fn new(name: &str) -> Result<Self>where
E: PlainObjectRef,
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.
Sourcepub fn set_attr(
self,
name: &str,
value: impl Into<Any> + Send + Sync,
) -> Result<Self>
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 + Syncsince 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.
Sourcepub unsafe fn set_native_fn_attr<Args, R>(
self,
name: &str,
f: NativeFunction<Args, R>,
) -> Result<Self>
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.