docs

你正在阅读 v0.1.2 的文档。Nabi 0.1.2 已不再提供支持。 最新版本:v0.1.3 →

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

  • attr runtime attributes: values of a #[runtime_attribute(name)] struct written as #[name(..)] on a type, field or variant, kept in read-only data.
  • field Field, one field of a struct or variant.
  • info TypeInfo, a type's description.

Re-exports

attr::AttrSource

pub use attr::Attr
  • Attr One runtime attribute on a type, field or variant.

field::FieldSource

pub use field::Field
  • Field One field of an Inspect struct, 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}
  • ReflectError An error of TypeInfo::build and the container conversions: what is wrong, in words.
  • TypeInfo A type's description.
  • TypeKind What kind of type a TypeInfo describes.
  • TypeRef A reference to a type by name, with its description when there is one.
  • Variant One variant of an enum.
  • find The description of the type at path ("game::Player"), when the program has one.
  • info T's description; its fields and accessors when T is Inspect.
  • info_of The description of the type value holds, when the program has one (the type, or a type made of it, is asked about somewhere).

Structs

FieldInfoSource

pub struct FieldInfo

One 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 MethodInfo

One 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 Reflect

Type facts every type has; the compiler implements it.

  • fn type_name() -> str§

    Source

    The type's name.

  • fn size() -> usize§

    Source

    The type's size in bytes.

  • fn align() -> usize§

    Source

    The type's alignment in bytes.

  • fn type_name_of_val(self) -> str§

    Source

    The name of self's type.

  • fn size_of_val(self) -> usize§

    Source

    The size of self's type in bytes.

InspectSource

pub trait Inspect: Reflect

Field and method listing, opt-in through #[derive(Inspect)].

  • fn type_info() -> TypeInfo§

    Source

    The type's description.

  • fn field_names() -> List<str>§

    Source

    The field names, in declaration order.

  • fn get_field(self, name: str) -> any | none§

    Source

    The value of field name (of the current variant for an enum), or none when there is no such field.

  • fn set_field(ref self, name: str, value: any) -> bool§

    Source

    Sets field name to value; false when there is no such field or value holds another type. A variant's payload is read-only.

  • fn methods() -> List<MethodInfo>§

    Source

    The inherent methods, by name.

  • fn fields() -> List<FieldInfo>§

    Source

    The fields with their types, in declaration order.

  • fn variant_names() -> List<str>§

    Source

    An enum's variant names, in declaration order; empty for a struct.

  • fn variant_name(self) -> str§

    Source

    The variant self holds ("Circle"); for a struct, its type name.

  • fn field_names_of_val(self) -> List<str>§

    Source

    The fields self holds: a struct's fields, or the fields of the variant an enum value holds ("0", "1" for a tuple variant).

  • fn __inspect_get(owner: any, field: usize) -> any | none§

    Source

    Field field of owner (the derive writes it; TypeInfo calls it).

  • fn __inspect_set(owner: any, field: usize, value: any) -> any | none§

    Source

    A copy of owner with field field set to value.

  • fn __inspect_build(variant: usize, fields: Dict<str, any>) -> (any | none, str)§

    Source

    A value made from named fields, or none and 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.