#builder-pattern #fundamental #general-purpose

no-std former_types

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.

12 stable releases

2.12.0 Oct 30, 2024
2.11.0 Oct 30, 2024
2.8.0 Sep 5, 2024
2.7.0 Jul 13, 2024
2.2.0 May 30, 2024

#2509 in Algorithms

Download history 673/week @ 2024-08-17 1801/week @ 2024-08-24 1581/week @ 2024-08-31 1453/week @ 2024-09-07 218/week @ 2024-09-14 143/week @ 2024-09-21 237/week @ 2024-09-28 145/week @ 2024-10-05 235/week @ 2024-10-12 94/week @ 2024-10-19 806/week @ 2024-10-26 348/week @ 2024-11-02 356/week @ 2024-11-09 446/week @ 2024-11-16 159/week @ 2024-11-23 797/week @ 2024-11-30

1,862 downloads per month
Used in 39 crates (5 directly)

MIT license

175KB
2.5K SLoC

Module :: former_types

experimental rust-status docs.rs Open in Gitpod discord

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.

Example: Using Trait Assign

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.

Dependencies

~0–375KB