Trait former::ComponentAssign
source · pub trait ComponentAssign<T, IntoT>where
IntoT: Into<T>,{
// Required method
fn assign(&mut self, component: IntoT);
}
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 theset
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 ComponentAssign
to set a name string on a struct :
use former::ComponentAssign;
struct MyStruct
{
name : String,
}
impl< IntoT : Into< String > > ComponentAssign< 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" );