Expand description
§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.
Modules§
- dependency
- Namespace with dependencies.
- derive
- exposed
- Exposed namespace of the module.
- orphan
- Parented namespace of the module.
- own
- Own namespace of the module.
- prelude
- Prelude to use essentials:
use my_module::prelude::*.
Traits§
- Assign
- Provides a generic interface for setting a component of a certain type on an object.
- Assign
With Type - The
AssignWithTypetrait provides a mechanism to set a component on an object, utilizing the type information explicitly. This trait extends the functionality ofAssignby allowing implementers to specify the component’s type at the method call site, enhancing expressiveness in code that manipulates object states. - Option
Ext - Extension trait to provide a method for setting a component on an
Option<Self>if theOptionis currentlyNone. If theOptionisSome, the method will delegate to theAssigntrait’sassignmethod.
Derive Macros§
- Assign
- Derives the
Assigntrait for struct fields, allowing each field to be set with a value that can be converted into the field’s type. - Component
From - Macro to implement
Fromfor each component (field) of a structure. This macro simplifies the creation ofFromtrait implementations for struct fields, enabling easy conversion from a struct reference to its field types. - Components
Assign - Derives the
ComponentsAssigntrait for a struct, enablingcomponents_assignwhich set all fields at once. - From
Components - A procedural macro to automatically derive the
From<T>trait implementation for a struct, enabling instances of one type to be converted from instances of another type.