Trait VirtualDynField

Source
pub trait VirtualDynField<Discriminant: DiscriminantTag = ()>: Default {
    type None: Delimited;
    type Some: Delimited;
    type Many: Buffer<Self::Some>;
    type Repr: Delimited + Default + FromTag<Self::None, NoneTag> + TryIntoTag<Self::None, NoneTag> + FromTag<Self::Some, SomeTag> + TryIntoTag<Self::Some, SomeTag> + TryFromTag<Self::Many, ManyTag> + IntoTag<Self::Many, ManyTag>;

    // Required methods
    fn access(&self) -> Self::Repr;
    fn mutate(&mut self, v: Self::Repr);
    fn len(&self) -> usize;
    fn min(&self) -> usize;
    fn max(&self) -> usize;
}
Expand description

A discriminant-keyed virtual field abstraction with flexible cardinality (None, Some, or Many).

This trait models a field whose structure is deferred and resolved through types rather than fixed upfront.

§Model

A VirtualDynField behaves like a field in a logical record, without committing to:

  • a concrete container (Option, Vec, etc.)
  • a fixed cardinality
  • or a fixed storage layout

Instead:

  • the implementor defines the backing representation (Repr)
  • the caller selects the shape (None, Some, or Many)

§Representation Semantics

The Many form is expected to have vector-like semantics:

  • dynamically sized (within bounds)
  • growable and shrinkable
  • capacity enforced but not encoded in the type shape

This makes VirtualDynField suitable for:

  • flexible schemas
  • deferred structure
  • abstraction across boundaries

§Discriminant

The Discriminant acts as a type-level key, allowing multiple independent fields to coexist without ambiguity.

§Design

Responsibilities are separated:

  • Implementor (Storage Layer)
    • defines representation (Repr)
  • Caller (Shape Layer)
    • selects cardinality and interpretation

This decouples logical meaning from physical storage.

§When to Use

Use this trait when:

  • structure must remain flexible
  • cardinality is not known upfront
  • storage must be abstract and composable

§Default Discriminant

  • Discriminant = (): uses the unit type as a single field key, meaning only one virtual field exists (no disambiguation needed).

Required Associated Types§

Source

type None: Delimited

Representation of absence.

Source

type Some: Delimited

The logical element type stored in the virtual field.

Source

type Many: Buffer<Self::Some>

A collection of Some.

Must support buffering and indexed access.

Source

type Repr: Delimited + Default + FromTag<Self::None, NoneTag> + TryIntoTag<Self::None, NoneTag> + FromTag<Self::Some, SomeTag> + TryIntoTag<Self::Some, SomeTag> + TryFromTag<Self::Many, ManyTag> + IntoTag<Self::Many, ManyTag>

Opaque storage backing the virtual field.

Encodes the actual data and must support tagged conversions to and from None, Some, and Many.

Required Methods§

Source

fn access(&self) -> Self::Repr

Returns the current representation of the virtual field.

Interaction is performed via tagged conversions.

Source

fn mutate(&mut self, v: Self::Repr)

Replaces the current representation.

The provided value must satisfy representation invariants.

Source

fn len(&self) -> usize

Returns the current number of elements represented.

Interpreted as:

  • None -> 0
  • Some -> 1
  • Many -> n
Source

fn min(&self) -> usize

Returns the minimum number of elements representable.

Source

fn max(&self) -> usize

Returns the maximum number of elements representable.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl VirtualDynField for ()

No-op VirtualDynField implementation for unit.

Represents an allocation with no storage and zero capacity.

Source§

type None = ()

Source§

type Some = ()

Source§

type Many = Vec<()>

Source§

type Repr = ()

Source§

fn access(&self) -> Self::Repr

Source§

fn mutate(&mut self, _: Self::Repr)

Source§

fn len(&self) -> usize

Source§

fn min(&self) -> usize

Source§

fn max(&self) -> usize

Implementors§