component_model 0.2.0

A flexible implementation of the Builder pattern supporting nested builders and collection-specific subcomponent_models. Simplify the construction of complex objects.
Documentation

Module :: component_model

experimental rust-status docs.rs Open in Gitpod discord

A flexible component model for Rust supporting generic assignment and type-based field access.

Installation

Add component_model to your Cargo.toml:

cargo add component_model

Minimal Example: Using Assign

use component_model::prelude::Assign;

#[derive(Debug, PartialEq, Default)]
struct Person {
  age: i32,
  name: String,
}

impl<IntoT> Assign<i32, IntoT> for Person
where
  IntoT: Into<i32>,
{
  fn assign(&mut self, component: IntoT) {
    self.age = component.into();
  }
}

impl<IntoT> Assign<String, IntoT> for Person
where
  IntoT: Into<String>,
{
  fn assign(&mut self, component: IntoT) {
    self.name = component.into();
  }
}

fn main() {
  let mut person = Person::default();
  person.assign(42);
  person.assign("Alice");
  assert_eq!(person, Person { age: 42, name: "Alice".to_string() });
}

API Overview

  • Assign: Generic trait for assigning values to struct fields by type.
  • AssignWithType: Trait for assigning values with explicit type annotation.
  • ComponentsAssign: Trait for assigning multiple components at once.

See component_model_types documentation for details.

Where to Go Next