Expand description
typex is a lightweight Rust library for inspecting, traversing, comparing
and mutating values at runtime without knowing their concrete types. It
supports no_std environments with alloc.
§Available types
- Reflection traits:
MetaandMetaMut - Owned objects:
Object,ObjectMutandSendObject - Object trait:
ObjectOps - Borrowed views:
ObjectRefandObjectRefMut - Typed map access:
TypedMapAccessandTypedMapAccessMut - Paths:
PathSegment,OwnedPathandTypedPath - Mutations:
PatchOperation,MutationBatchandMutationRollback - Type metadata:
TypeInfo,ValueKindandAccessKind - Structural shapes:
ReflectandReflectMut - Shape access:
StructAccess,TupleAccess,EnumAccess,SequenceAccess,KeyedSequenceAccessandMapAccess, withVariantFieldsfor the fields of an enum variant - Mutable shape access:
StructAccessMut,TupleAccessMut,EnumAccessMut,SequenceAccessMut,KeyedSequenceAccessMut,MapAccessMutandOptionAccessMut, withVariantFieldsMut - Type-keyed maps:
TypeMap
§Derive macros
The Meta and MetaMut derive macros are provided by
typex_derive. Enable the derive feature
to use them as Meta and MetaMut.
§Structural equality
Meta::eq_dyn compares values with matching runtime type metadata. Its
default implementation dispatches on the shape from Meta::reflect:
Reflect::Structcompares field names, then fields by name.Reflect::Tuplecompares lengths, then fields by index.Reflect::Enumcompares active variant names, then their fields by name or index.Reflect::Mapcompares lengths, then looks up each visited key by string. It supportsStringand&'static strkeys.Reflect::SequenceandReflect::KeyedSequencecompare lengths, then items by index.Reflect::Optioncompares presence and inner values.Reflect::Scalarreturnsfalse; scalar implementations provide their own value comparisons.
Built-in scalars, tuples, Result and the derives compare values directly,
avoiding dispatch through the access traits. BTreeMap and HashMap
compare native keys and reflective values. Custom opaque values provide
equality through #[typex(partial_eq)] or a hand-written implementation.
Float equality follows PartialEq, so NaN != NaN.
§Feature flags
std(default): EnablesHashMapsupport and implementations ofstd::error::Errorfor the crate’s error typesderive: Re-exports theMetaandMetaMutderive macros
§Minimum supported Rust version (MSRV)
Rust v1.85+ is required.
§Example
use typex::{Meta, Object, ObjectOps};
#[derive(Clone, Debug, Meta)]
struct Scope {
name: &'static str,
}
#[derive(Debug, Meta)]
struct User {
id: u16,
scopes: Vec<Scope>,
}
let object = Object::new(User {
id: 42,
scopes: vec![Scope { name: "read" }],
});
assert!(object.is::<User>());
assert_eq!(object.type_name(), core::any::type_name::<User>());
assert_eq!(object.field_names(), &["id", "scopes"]);
assert_eq!(object.field("id").unwrap().to_ref::<u16>(), Some(&42));
let scope_name = |object: &Object| {
object
.field("scopes")?
.item(0)?
.field("name")?
.to_ref::<&'static str>()
.copied()
};
assert_eq!(scope_name(&object), Some("read"));
// Use typed paths to traverse fields without an explicit downcast
assert_eq!(object.field_path(User::FIELD_ID.path()), Some(&42));
assert_eq!(
object.field_path(User::FIELD_SCOPES.item(0).then(Scope::FIELD_NAME)),
Some(&"read")
);Structs§
- AnyRef
- Borrowed, type-erased reference to a value that exposes runtime type information and downcasts to the concrete type.
- Mutation
Batch - Ordered list of patch operations that can be applied to a mutable reflective value.
- Mutation
Rollback - Holds inverse operations used by
undoto revert a committedMutationBatch. - Object
- Owned reflective value stored as a boxed
Metatrait object. - Object
Mut - Owned mutable reflective value stored as a boxed
MetaMuttrait object. - Object
Ref - Borrowed reflective view of a
Metavalue. - Object
RefMut - Borrowed mutable reflective view of a
MetaMutvalue. - Owned
Path - Owned path used by staged operations and rollback records.
- Owned
Path Iter - Iterator over the segments of an
OwnedPath. - Rollback
Error - Failure returned by
MutationRollback::undo. - Send
Object - Owned value with reflective access whose concrete value is
SendandSync. - Type
Info - Shallow metadata about a type, containing its qualified Rust name and type ID. Equality and hashing are based only on the type ID.
- TypeMap
- Maps type identities to values of type
T. Useful for registries and dispatch tables. - Typed
Field - A single typed field segment that can be composed into a
TypedPath. - Typed
Path - Typed path from a
Rootvalue to a nestedValue.
Enums§
- Access
Kind - Describes how a reflected value can be accessed.
- Apply
Error - Failure returned when applying a reflective patch. Paths in failure variants borrow names from the patch operation.
- Commit
Error - Failure returned by a staged commit.
- Commit
Operation Error - An operation failure reported by
MutationBatch::commit. - Move
Item Error - Failure returned when a sequential value cannot move an item.
- Owned
Patch Operation - One owned operation waiting for a staged commit.
- Patch
Operation - Operation in a patch, applied with
ObjectRefMut::applyor staged in aMutationBatch. - Patch
Operation Kind - Operation kinds used to identify unsupported patch requests.
- Path
Segment - Borrowed path segment for nested reflective traversal.
- Reflect
- Read-only structural shape of a
Metavalue. - Reflect
Mut - Mutable structural shape of a
MetaMutvalue. - Reflective
Error - Failure returned by reflective path traversal or mutable operations.
- Value
Kind - Classifies the structural shape exposed by a
Metaimplementation. - Variant
Fields - Fields of the active variant of an enum.
- Variant
Fields Mut - Mutable fields of the active variant of an enum.
Traits§
- Enum
Access - Variant access for
Reflect::Enum. - Enum
Access Mut - Mutable variant access for
ReflectMut::Enum. - Field
Path Query - Path accepted by
field_path. Its kind determines the return type. - Field
Path Query Mut - Path accepted by
field_path_mutandObjectRefMut::set_field_path: the mutable counterpart ofFieldPathQuery. - Keyed
Sequence Access - Indexed and keyed item access for
Reflect::KeyedSequence, such as a list of records with unique IDs. - Keyed
Sequence Access Mut - Mutable indexed and keyed item access for
ReflectMut::KeyedSequence. - MapAccess
- Keyed entry access for
Reflect::Map. - MapAccess
Mut - Mutable keyed entry access for
ReflectMut::Map. - Meta
- Read-only reflection for runtime type information, structural navigation and downcasting to a concrete type.
- MetaMut
- Mutable reflection extension of
Meta. - Object
Ops - Conversion and downcasting helpers for owned reflective objects.
- Option
Access Mut - Mutable access for
ReflectMut::Option. - Send
Meta - Thread-safe counterpart of
Metafor boxed values moved across threads. - Sequence
Access - Indexed item access for
Reflect::Sequence, such as aVecorVecDeque. - Sequence
Access Mut - Mutable indexed item access for
ReflectMut::Sequence. - Struct
Access - Field access for
Reflect::Structand for the active variant of aReflect::Enumwith named fields. - Struct
Access Mut - Mutable named field access for
ReflectMut::Structand for enum variants with named fields. - Tuple
Access - Positional field access for
Reflect::Tupleand for tuple enum variants. - Tuple
Access Mut - Mutable positional field access for
ReflectMut::Tupleand for tuple enum variants. - Typed
Map - Associates a map with the type of its values.
- Typed
MapAccess - Typed key lookup for map-like values.
- Typed
MapAccess Mut - Typed mutable key lookup for map-like values.
- Typed
Sequence - Associates a sequential value with the type of each indexed item.
Type Aliases§
- MapEntry
Visitor - Callback for enumerating map entries through reflection.
Derive Macros§
- Meta
- Derives structural
typex::Metafor a struct or enum. - MetaMut
- Derives mutable
typex::MetaMutfor a struct or enum.