Crate component_model_types

Source
Expand description

§Module :: component_model_types

experimental rust-status docs.rs Open in Gitpod discord

A flexible implementation of the Builder pattern supporting nested builders and collection-specific subcomponent_models. Its compile-time structures and traits that are not generated but reused.

§Example: Using Trait Assign

Demonstrates setting various components (fields) of a struct.

The component_model_types crate provides a generic interface for setting components on an object. This example defines a Person struct and implements the Assign trait for its fields. It shows how to use these implementations to set the fields of a Person instance using different types that can be converted into the required types.

#[ cfg( any( not( feature = "types_component_assign" ), not( feature = "enabled" ) ) ) ]
fn main() {}

#[ cfg( all( feature = "types_component_assign", feature = "enabled" ) ) ]
fn main()
{
  use component_model_types::Assign;

  #[ derive( Default, PartialEq, Debug ) ]
  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();
    }
  }

  let mut got : Person = Default::default();
  got.assign( 13 );
  got.assign( "John" );
  assert_eq!( got, Person { age : 13, name : "John".to_string() } );
  dbg!( got );
  // > Person {
  // >   age: 13,
  // >   name: "John",
  // > }

}

Try out cargo run --example component_model_types_trivial.
See code.

Modules§

dependency
Namespace with dependencies.
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.
AssignWithType
The AssignWithType trait provides a mechanism to set a component on an object, utilizing the type information explicitly. This trait extends the functionality of Assign by allowing implementers to specify the component’s type at the method call site, enhancing expressiveness in code that manipulates object states.
OptionExt
Extension trait to provide a method for setting a component on an Option<Self> if the Option is currently None. If the Option is Some, the method will delegate to the Assign trait’s assign method.