#builder-pattern #component-model #default #general-purpose #assign

no-std component_model

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

2 unstable releases

0.2.0 May 3, 2025
0.1.0 Apr 20, 2025

#1076 in Development tools

Download history 131/week @ 2025-04-16 18/week @ 2025-04-23 132/week @ 2025-04-30

281 downloads per month

MIT license

98KB
743 lines

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

Dependencies

~1.3–2.1MB
~41K SLoC