2 unstable releases
0.2.0 | May 3, 2025 |
---|---|
0.1.0 | Apr 20, 2025 |
#1076 in Development tools
281 downloads per month
98KB
743 lines
Module :: component_model
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
- Examples Directory: Explore practical, runnable examples.
- API Documentation (docs.rs): Get detailed information on all public types, traits, and functions.
- Repository (GitHub): View the source code, contribute, or report issues.
Dependencies
~1.3–2.1MB
~41K SLoC