Trait former::protected::AssignWithType

source ·
pub trait AssignWithType {
    // Required method
    fn assign_with_type<T, IntoT>(&mut self, component: IntoT)
       where IntoT: Into<T>,
             Self: Assign<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 Assign by allowing implementers to specify the component’s type at the method call site, enhancing expressiveness in code that manipulates 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.

§Examples

Implementing AssignWithType to set a username on a struct:

use former_types::{ Assign, AssignWithType }; // use crate `former` instead of crate `former_types` unless you need to use crate `former_types` directly

struct UserProfile
{
  username : String,
}

impl< IntoT : Into< String > > Assign< 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: Assign<T, IntoT>,

Sets the value of a component by its type.

This method allows an implementer of AssignWithType 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.

§Parameters
  • component: The value to assign to the component.
§Type Parameters
  • T: The type of the component to be set on the implementing object.
  • IntoT: A type that can be converted into T.

Object Safety§

This trait is not object safe.

Implementors§