Expand description
§Former Types - Core Trait Definitions and Type System Integration
This crate provides the foundational trait definitions and type system integration for the Former builder pattern ecosystem. It defines the core abstractions that enable flexible and extensible builder pattern implementations.
§Core Abstractions
§Formation Process Management
The crate defines several key traits that manage the formation process:
FormerDefinition
: Links entities to their formation definitionsFormerDefinitionTypes
: Specifies storage, formed, and context typesFormingEnd
: Handles completion of the formation processFormerMutator
: Enables pre-formation data validation and manipulationFormerBegin
: Initiates subform creation with proper context
§Storage Management
Storage
: Defines the interface for temporary state during formationStoragePreform
: Handles transition from storage to final formed state
§Collection Integration
Specialized support for collection types when the types_former
feature is enabled:
- Automatic trait implementations for standard collections
- Custom collection support through extensible trait system
- Type-safe collection builders with proper generic handling
§Architecture Design
§Type Safety and Generics
The trait system is designed to handle complex generic scenarios:
- Lifetime Parameters: Full support for complex lifetime relationships
- Generic Constraints: Proper constraint propagation through the type system
- Associated Types: Clean separation of concerns through associated types
§Builder Pattern Integration
The traits work together to enable:
- Fluent Interfaces: Method chaining with compile-time validation
- Subform Support: Nested builders with proper context preservation
- Custom Validation: Pre-formation validation and transformation
- Flexible End Conditions: Customizable formation completion logic
§Feature Gates
types_former
: Enables core Former trait definitionsuse_alloc
: Enables allocation-dependent features in no-std environmentsno_std
: Full no-std compatibility when used without std-dependent features
§Integration with Former Ecosystem
This crate serves as the foundation for:
- [
former
]: Main user-facing crate with derive macro - [
former_meta
]: Procedural macro implementation - Collection Tools: Integration with external collection libraries
§Usage Patterns
Most users will not interact with this crate directly, but will instead use
the higher-level [former
] crate. However, this crate is essential for:
- Custom Former implementations
- Integration with external libraries
- Advanced builder pattern scenarios
§Module :: 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.
§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 component_model_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.
Modules§
- definition
- Formation Definition System
- dependency
- Namespace with dependencies
- exposed
- Exposed namespace of the module.
- forming
- Formation Process Management
- orphan
- Parented namespace of the module.
- own
- Own namespace of the module.
- prelude
- Prelude to use essentials:
use my_module::prelude::*
. - storage
- Storage Interface System
Structs§
- BTree
MapDefinition - Represents the formation definition for a hash map-like collection within the former framework.
- BTree
MapDefinition Types - Holds the generic parameters for the
BTreeMapDefinition
. - BTree
SetDefinition - Represents the formation definition for a binary tree set-like collection within the former framework.
- BTree
SetDefinition Types - Holds the generic parameters for the
BTreeSetDefinition
. - Binary
Heap Definition - Represents the formation definition for a binary heap-like collection within the former framework.
- Binary
Heap Definition Types - Holds the generic parameters for the
BinaryHeapDefinition
. - Collection
Former - A builder structure for constructing collections with a fluent and flexible interface.
- Forming
EndClosure - A wrapper around a closure to be used as a
FormingEnd
. - Hash
MapDefinition - Represents the formation definition for a hash map-like collection within the former framework.
- Hash
MapDefinition Types - Holds the generic parameters for the
HashMapDefinition
. - Hash
SetDefinition - Represents the formation definition for a hash set-like collection within the former framework.
- Hash
SetDefinition Types - Holds the generic parameters for the
HashSetDefinition
. - Linked
List Definition - Represents the formation definition for a list-like collection within the former framework.
- Linked
List Definition Types - Holds the generic parameters for the
LinkedListDefinition
. - NoEnd
- A placeholder
FormingEnd
used when no end operation is required or applicable. - Return
Preformed - A
FormingEnd
implementation that directly returns the formed collection as the final product of the forming process. - Return
Storage - A
FormingEnd
implementation that returns the storage itself as the formed entity, disregarding any contextual data. - VecDeque
Definition - Represents the formation definition for a vector deque-like collection within the former framework.
- VecDeque
Definition Types - Holds the generic parameters for the
VecDequeDefinition
. - Vector
Definition - Represents the formation definition for a vector-like collection within the former framework.
- Vector
Definition Types - Holds the generic parameters for the
VectorDefinition
.
Traits§
- BTree
MapExt - Provides an extension method for hash maps to facilitate the use of the builder pattern.
- BTree
SetExt - Provides an extension method for binary tree sets to facilitate the use of the builder pattern.
- Binary
Heap Ext - Provides an extension method for binary heaps to facilitate the use of the builder pattern.
- Collection
- Represents a collection by defining the types of entries and values it handles.
- Collection
Add - Provides functionality to add individual entries to a collection.
- Collection
Assign - Defines the capability to replace all entries in a collection with a new set of entries.
- Collection
ValTo Entry - Provides a mechanism for transforming a value back into a collection-specific entry format.
- Entity
ToDefinition - Maps a type of entity to its corresponding former definition.
- Entity
ToDefinition Types - Provides a mapping between a type of entity and its associated formation type definitions.
- Entity
ToFormer - Maps a type of entity to its corresponding former (builder) implementation.
- Entity
ToStorage - Maps a type of entity to its storage type. This trait defines what storage structure is used to hold the interim state of an entity during its formation.
- Entry
ToVal - Facilitates the conversion of collection entries to their corresponding value representations.
- Former
Begin - A trait for initiating a structured subforming process with contextual and intermediary storage linkage.
- Former
Definition - Expands on
FormerDefinitionTypes
by incorporating an ending mechanism for the formation process. This trait connects the formation types with a specific endpoint, defining how the formation process concludes, including any necessary transformations or validations. - Former
Definition Types - Defines the fundamental components involved in the formation of an entity. This trait specifies the types of storage, the formed entity, and the context used during the formation process.
- Former
Mutator - Provides a mechanism for mutating the context and storage just before the forming process is completed.
- Forming
End - Defines a handler for the end of a subforming process, enabling the return of the original context.
- Hash
MapExt - Provides an extension method for hash maps to facilitate the use of the builder pattern.
- Hash
SetExt - Provides an extension method for
HashSet
to facilitate the use of the builder pattern. - Linked
List Ext - Provides an extension method for lists to facilitate the use of the builder pattern.
- Storage
- Defines the storage interface for entities being constructed using a forming pattern.
- Storage
Preform - Provides a mechanism to finalize the forming process by converting storage into its final formed state.
- ValTo
Entry - Facilitates the conversion of values back into entries for specific collection types.
- VecDeque
Ext - Provides an extension method for vector deques to facilitate the use of the builder pattern.
- VecExt
- Provides an extension method for vectors to facilitate the use of the builder pattern.
Type Aliases§
- BTree
MapFormer - Provides a streamlined builder interface for constructing hash map-like collections.
- BTree
SetFormer - Provides a streamlined builder interface for constructing binary tree set-like collections.
- Binary
Heap Former - Provides a streamlined builder interface for constructing binary heap-like collections.
- Hash
MapFormer - Provides a streamlined builder interface for constructing hash map-like collections.
- Hash
SetFormer - Provides a concise alias for
CollectionFormer
configured specifically forHashSet
-like collections. - Linked
List Former - Provides a streamlined builder interface for constructing list-like collections.
- VecDeque
Former - Provides a streamlined builder interface for constructing vector deque-like collections.
- Vector
Former - Provides a streamlined builder interface for constructing vector-like collections.