Crates.io | former_types |
lib.rs | former_types |
version | 2.6.0 |
source | src |
created_at | 2024-05-21 12:52:35.021276 |
updated_at | 2024-07-13 17:36:43.06443 |
description | A flexible implementation of the Builder pattern supporting nested builders and collection-specific subformers. Its compile-time structures and traits that are not generated but reused. |
homepage | https://github.com/Wandalen/wTools/tree/master/module/core/former |
repository | https://github.com/Wandalen/wTools/tree/master/module/core/former |
max_upload_size | |
id | 1246822 |
size | 141,404 |
A flexible implementation of the Builder pattern supporting nested builders and collection-specific subformers. Its compile-time structures and traits that are not generated but reused.
Demonstrates setting various components (fields) of a struct.
The former_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_former" ), not( feature = "enabled" ) ) ) ]
fn main() {}
#[ cfg( all( feature = "types_former", feature = "enabled" ) ) ]
fn main()
{
use former_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 former_types_trivial
.
See code.