core::reflect
이 페이지의 내용
API 참조는 영어로만 있습니다. 표준 라이브러리 소스의 영어 문서 주석에서 만듭니다.
Reflect / Inspect.
Reflect is implemented by the compiler for every type (no impl in
source; a manual one is an error): T::type_name(), T::size(),
T::align() are the type's answer, constants; x.type_name_of_val() /
x.size_of_val() are the value's: they look at the variant an any or
sum value holds at run time. A type's own method of the same name wins.
Inspect is opt-in through #[derive(Inspect)], written below as a
meta block: the compiler describes the type in read-only data
(TypeInfo: fields with offsets, variants, type arguments, runtime
attributes), and the derive adds the code that reads and writes its
fields through any. Private fields are listed too: deriving it is
the type's consent. Reading a description allocates nothing.
Modules
attrruntime attributes: values of a#[runtime_attribute(name)]struct written as#[name(..)]on a type, field or variant, kept in read-only data.fieldField, one field of a struct or variant.infoTypeInfo, a type's description.
Re-exports
attr::AttrSource
pub use attr::AttrAttrOne runtime attribute on a type, field or variant.
field::FieldSource
pub use field::FieldFieldOne field of anInspectstruct, or of one variant of an enum.
info::{ReflectError, TypeInfo, TypeKind, TypeRef, Variant, find, info, info_of}Source
pub use info::{ReflectError, TypeInfo, TypeKind, TypeRef, Variant, find, info, info_of}ReflectErrorAn error ofTypeInfo::buildand the container conversions: what is wrong, in words.TypeInfoA type's description.TypeKindWhat kind of type aTypeInfodescribes.TypeRefA reference to a type by name, with its description when there is one.VariantOne variant of an enum.findThe description of the type atpath("game::Player"), when the program has one.infoT's description; its fields and accessors whenTisInspect.info_ofThe description of the typevalueholds, when the program has one (the type, or a type made of it, is asked about somewhere).
Structs
FieldInfoSource
pub struct FieldInfoOne field of an Inspect type. type_name is the instantiated type
(f64 for v: T in Box<f64>); name is "0", "1", .. for a
tuple struct; is_pub is true only for a plain pub field.
MethodInfoSource
pub struct MethodInfoOne inherent method of an Inspect type. It has name / arity /
is_static and arg_types / return_type.
arity and arg_types leave out self.
Traits
ReflectSource
pub trait ReflectType facts every type has; the compiler implements it.
The type's name.
The type's size in bytes.
The type's alignment in bytes.
The name of
self's type.The size of
self's type in bytes.
InspectSource
pub trait Inspect: ReflectField and method listing, opt-in through #[derive(Inspect)].
The type's description.
The field names, in declaration order.
The value of field
name(of the current variant for an enum), ornonewhen there is no such field.Sets field
nametovalue;falsewhen there is no such field orvalueholds another type. A variant's payload is read-only.
Sourcefn methods(§) -> List<MethodInfo> The inherent methods, by name.
The fields with their types, in declaration order.
An enum's variant names, in declaration order; empty for a struct.
The variant
selfholds ("Circle"); for a struct, its type name.The fields
selfholds: a struct's fields, or the fields of the variant an enum value holds ("0","1"for a tuple variant).Field
fieldofowner(the derive writes it;TypeInfocalls it).A copy of
ownerwith fieldfieldset tovalue.A value made from named fields, or
noneand what is wrong.
Functions
castSource
pub fn cast<T>(value: any) -> T | none value as a T: the value when it holds a T; an integer of another
integer type when it fits in T (set_field("id", 42) on a u64
field); else none.