Trait VirtualCollector

Source
pub trait VirtualCollector<T, Discriminant: DiscriminantTag = ()>: FromTag<T, Discriminant> + TryIntoTag<T, Discriminant> { }
Expand description

A virtual collector for values of type T under a discriminant.

A VirtualCollector represents a type that can:

  • collect a value T into itself (via FromTag)
  • attempt to extract a value T back (via TryIntoTag)

§Virtual Field Context

In the virtual system:

  • values may be interpreted differently depending on their role
  • tagged conversions (FromTag, TryIntoTag) define those interpretations
  • this trait groups types that support bidirectional interaction with T

§Semantics

A VirtualCollector acts as a tagged carrier of T:

  • T -> Self

    • always succeeds (collection)
  • Self -> T

    • may fail depending on structure (extraction)

This is commonly implemented by enums where a specific variant represents T.

§Example Pattern

enum Value {
    Number(u32),
    Text(String),
}

// `Value` can act as a VirtualCollector<u32>

§Discriminant

The Discriminant ensures conversions remain unambiguous, even when T or Self are generic or not fully concrete.

§When to Use

Use this trait when:

  • a type can collect values of T
  • extraction may depend on internal structure
  • tagged semantics are required for disambiguation

§Default Discriminant

  • Discriminant = (): defines a single default interpretation, meaning one collection/extraction behavior is assumed.

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.

Implementors§

Source§

impl<T, Discriminant, U> VirtualCollector<T, Discriminant> for U
where U: FromTag<T, Discriminant> + TryIntoTag<T, Discriminant>, Discriminant: DiscriminantTag,

Blanket implementation for all types supporting bidirectional tagged conversion with T.

Any type that:

automatically implements VirtualCollector.

This allows enums and similar container types to act as collectors without requiring explicit implementations.