pub trait AssignWithType {
    // Required method
    fn assign_with_type<T, IntoT>(&mut self, component: IntoT)
       where IntoT: Into<T>,
             Self: ComponentAssign<T, IntoT>;
}
Expand description

The AssignWithType trait provides a mechanism to set a component on an object, utilizing the type information explicitly. This trait extends the functionality of SetComponent by allowing implementers to specify the component’s type at the method call site, enhancing expressiveness in code that manipulates object states.

§Method Detail

  • assign_with_type::< T, IntoT >( &mut self, component : IntoT )

This method allows an implementer of SetWithType to set a component on self where the component’s type is T, and the input value is of type IntoT, which can be converted into T. This method bridges the gap between dynamic type usage and static type enforcement, providing a flexible yet type-safe interface for modifying object states.

§Type Parameters

  • T : The type of the component to be set on the implementing object. This specifies the exact type expected by the object as its component.
  • IntoT : A type that can be converted into T, providing flexibility in the types of values that can be used to set the component.

§Example

use former::{ ComponentAssign, AssignWithType };

struct UserProfile
{
  username : String,
}

impl< IntoT : Into< String > > ComponentAssign< String, IntoT > for UserProfile
{
  fn assign( &mut self, component : IntoT )
  {
    self.username = component.into();
  }
}

let mut user_profile = UserProfile { username : String::new() };
user_profile.assign_with_type::< String, _ >( "john_doe" );

assert_eq!( user_profile.username, "john_doe" );

Required Methods§

source

fn assign_with_type<T, IntoT>(&mut self, component: IntoT)
where IntoT: Into<T>, Self: ComponentAssign<T, IntoT>,

Function to set value of a component by its type.

Object Safety§

This trait is not object safe.

Implementors§