pub trait Assign<T, IntoT>where
IntoT: Into<T>,{
// Required method
fn assign(&mut self, component: IntoT);
// Provided method
fn impute(self, component: IntoT) -> Self
where Self: Sized { ... }
}
Expand description
Provides a generic interface for setting a component of a certain type on an object.
This trait abstracts the action of setting or replacing a component, where a component
can be any part or attribute of an object, such as a field value. It is designed to be
generic over the type of the component being set (T
) and the type that can be converted
into the component (IntoT
). This design allows for flexible implementations that can
accept various types that can then be converted into the required component type.
§Type Parameters
T
: The type of the component to be set on the implementing object. This type represents the final form of the component as it should be stored or represented in the object.IntoT
: The type that can be converted intoT
. This allows theassign
method to accept different types that are capable of being transformed into the required component typeT
, providing greater flexibility in setting the component.
§Examples
Implementing Assign
to set a name string on a struct:
use component_model_types::Assign; // use crate `component_model` instead of crate `component_model_types` unless you need to use crate `component_model_types` directly
struct MyStruct {
name: String,
}
impl< IntoT : Into< String > > Assign< String, IntoT > for MyStruct
{
fn assign( &mut self, component : IntoT )
{
self.name = component.into();
}
}
let mut obj = MyStruct { name : String::new() };
obj.assign( "New Name" );
assert_eq!( obj.name, "New Name" );