Crate structural_typing

Crate structural_typing 

Source
Expand description

§Structural Typing for Rust

Track which struct fields are present/absent at the type level.

§Quick Example

use structural_typing::{structural, presence::Present, select};

#[structural]
struct User { name: String, email: String }

// Build incrementally - type tracks which fields are set
let user = User::empty().name("Alice".to_owned());

// Methods can require specific fields
impl<F: user::Fields<name = Present>> User<F> {
    fn greet(&self) -> String {
        format!("Hello, {}!", self.name)
    }
}

user.greet(); // ✓ Compiles - name is Present
// User::empty().greet(); // ✗ Compile error - name is Absent

// Use select! to create field sets
type NameAndEmail = select!(user: name, email);

§Core Concepts

  • Field States: Present (has value), Optional (has Option<T>), Absent (no value)
  • Builder API: .field(value) infers presence from type (T → Present, Option<T> → Optional, PhantomData<T> → Absent)
  • Type Selection: select!(module: field1, ?field2) macro or type aliases like with::name::Present<with::email::Optional>
  • Bounded Impls: Methods requiring specific fields via trait bounds

See the examples directory for comprehensive usage including merge, split, serde integration, and more.

Modules§

access
Runtime field access trait for checking presence and getting values. Runtime field access for structural types.
presence
Type-level presence markers and traits for field state tracking. Type-level markers for field presence states.
split
Traits for splitting structs into selected fields and remainder. Split structs into selected fields and remainder.

Macros§

select
Construct a FieldSet by selecting fields from a module.

Attribute Macros§

structural
Generates type-safe structural typing support for a struct.