Expand description
Virtual struct system mapping discriminants (ZSTs) to behavior via trait-driven schemas.
Traditional Rust data structures require committing upfront to:
- a fixed shape (
Option<T>,Vec<T>,[T; N], struct fields) - a fixed cardinality (single vs multiple)
- a fixed storage layout
This makes evolution difficult:
- structure and storage are tightly coupled
- generic abstractions often hit coherence limits
- external composition (plugins, schemas) is hard
- changing a field (
None -> Some -> Many) requires redesign
This module introduces a system where structure, interpretation, and storage are decoupled, and resolved through types.
§Mental Model: Virtual Structs
Think in terms of a virtual struct, whose fields are not stored directly, but defined through traits and discriminants:
struct <T as Trait>::Struct<K> {
FieldTag: T::Field, // virtual field (identified by discriminant)
ExtTag: K, // virtual extension (identified by discriminant)
}This is not a concrete struct. Instead:
- fields are accessed via trait implementations
- each field is identified by a discriminant (type-level key)
- storage is abstracted or external
§Discriminants (Field Identifiers)
struct FieldTag;
struct ExtTag;Discriminants are zero-sized types used as type-level field keys.
They act as:
- field identifiers in the virtual struct
- selectors for behavior and interpretation
- disambiguators for generic implementations
This ensures:
- multiple fields can coexist without ambiguity
- overlapping generic impls remain coherence-safe
§Trait as Schema
pub trait Trait {
type Struct: VirtualDynField<FieldTag, Some = Self::Field>
+ VirtualDynExtension<ExtTag>;
type Field;
}Structis the container (virtual struct)FieldTagidentifies a logical fieldExtTagidentifies an extension slotFielddefines the logical type of the field
Traits define the schema, not the storage.
§Field Behavior (Cardinality Abstraction)
A virtual field supports:
None- no valueSome- one valueMany- multiple values
This replaces fixed representations like:
Option<T> / Vec<T>with a single abstraction that can evolve without redesign.
§Key Insight
A virtual struct is not a fixed layout, but a composition of discriminant-keyed behaviors, where:
- discriminants -> field identifiers
- traits -> schema (what exists)
- implementations -> storage (how/where it exists)
Structure is resolved by types, not encoded directly.
§Core Primitives
The system is built from:
§Virtual Fields
VirtualDynField- dynamic, vector-like semanticsVirtualStaticField- static, array-like semantics
§Virtual Extensions
VirtualDynExtension/VirtualStaticExtension- externally defined fields via schemas
§Schemas & Bounds
VirtualDynBound/VirtualStaticBound- constraints defined independently of storage
§Concrete Representations
SumDynType- bounded vector semantics (None | Some | Many)SumStaticType- fixed array semantics
§Design Principles
§Discriminant-Keyed Design
All components are keyed by a DiscriminantTag.
This:
- avoids ambiguity in generic implementations
- enables multiple independent fields on the same container
- ensures coherence-safe extensibility
§Tagged Conversions
Instead of From / Into, the system uses:
Conversions are disambiguated by discriminants:
This avoids overlapping implementations in generic contexts.
§Layered Model
The system separates:
-
Type-level layer
- defines structure, schema, and behavior
-
Value-level layer
- represents
None,Some,Many
- represents
This enables abstract structure with concrete representations.
§Dynamic vs Static
§Dynamic (Runtime Flexible)
- vector-like semantics
- runtime bounds (
Get<u32>) - growable/shrinkable collections
§Static (Compile-Time Fixed)
- array-like semantics
- compile-time bounds (
const) - zero-overhead representations
§Helpers
Ergonomic helpers are provided for working with virtual components:
§Summary
This system enables:
- evolving data shapes without redesign
- discriminant-keyed field composition
- external composition via schemas and extensions
- reuse of storage across multiple logical fields
- coherence-safe generic abstractions
It provides a type-level virtualization layer where:
A virtual struct is a mapping from discriminants -> behaviors, resolved through traits and implemented via storage.
Structs§
- ManyTag
- A discriminant representing multiple values in tagged conversions.
- NoneTag
- A discriminant representing the absence of a value in tagged conversions.
- SomeTag
- A discriminant representing a single value in tagged conversions.
Enums§
- SumDyn
Type - A concrete representation of field cardinality: zero, one, or many values.
- SumStatic
Type - A statically shaped representation of field cardinality.
Traits§
- Delegate
Virtual DynBound - Delegates bound resolution to an external
VirtualDynBoundprovider. - Delegate
Virtual DynExtension - Constraint for delegating a virtual extension to an external schema.
- Delegate
Virtual Static Bound - Delegates bound resolution to an external
VirtualStaticBoundprovider. - Delegate
Virtual Static Extension - Constraint for delegating a virtual extension to an external
VirtualStaticExtensionSchema. - Discriminant
Tag - Marker trait for type-level discriminants.
- DynExt
Helpers - Helper methods for accessing and mutating values in a
VirtualDynExtension. - DynField
Helpers - Helper methods for accessing and mutating values in a
VirtualDynField. - FromTag
- Converts a value
TintoSelfunder a given discriminant. - IntoTag
- Converts
selfinto another representation under a given discriminant. - Static
ExtHelpers - Helper methods for accessing and mutating values in a
VirtualStaticExtension. - Static
Field Helpers - Helper methods for accessing and mutating values in a
VirtualStaticField. - TryFrom
Tag - Fallible version of
FromTag. - TryInto
Tag - Fallible version of
IntoTag. - Virtual
Collector - A virtual collector for values of type
Tunder a discriminant. - Virtual
DynBound - Provides the bounds associated with a
VirtualDynField<Discriminant>. - Virtual
DynExtension - Allocation interface for virtual extensions whose type and schema are defined externally.
- Virtual
DynExtension Schema - Defines the schema for a
VirtualDynExtensionidentified by aDiscriminant. - Virtual
DynField - A discriminant-keyed virtual field abstraction with flexible cardinality
(
None,Some, orMany). - Virtual
DynField With Delegated Bounds - Constraint describing a virtual field whose bounds are delegated
to an external
VirtualDynBoundprovider. - Virtual
Error - Defines the error type associated with a virtual component
identified by a
Discriminant. - Virtual
Map - A storage-backed virtual map owned by a container in the virtual structure system.
- VirtualN
Map - A storage-backed virtual n-map owned by a container in the virtual structure system.
- Virtual
Static Bound - Provides the bounds associated with a
VirtualStaticField<Discriminant>. - Virtual
Static Extension - Allocation interface for virtual extensions whose type and schema are defined externally and fully determined at compile time.
- Virtual
Static Extension Schema - Defines the schema for a
VirtualStaticExtensionidentified by aDiscriminant. - Virtual
Static Field - A discriminant-keyed virtual field abstraction with statically determined cardinality.
- Virtual
Static Field With Delegated Bounds - Constraint describing a virtual field whose bounds are delegated
to an external
VirtualStaticBoundprovider.
Functions§
- set_
index 🔒 - Sets the value at a given index in a collection.