Struct former::HashMapSubformer
source · pub struct HashMapSubformer<K, E, Container, Context, End>{ /* private fields */ }Expand description
A builder for constructing hash map-like structures with a fluent interface.
HashMapSubformer leverages the HashMapLike trait to enable a flexible and customizable
way to build hash map-like structures. It supports the chaining of insert operations and
allows for the definition of custom end actions to finalize the building process.
§Type Parameters
K: Key type, must implementEqandHash.E: Element (value) type.Container: The hash map-like container being built.Context: Type of the optional context used during the building process.End: End-of-forming action to be executed upon completion.
§Examples
#[ derive( Debug, PartialEq, former::Former ) ]
pub struct StructWithMap
{
#[ subformer( former::HashMapSubformer ) ]
map : std::collections::HashMap< &'static str, &'static str >,
}
let struct1 = StructWithMap::former()
.map()
.insert( "a", "b" )
.insert( "c", "d" )
.end()
.form()
;
assert_eq!( struct1, StructWithMap { map : hmap!{ "a" => "b", "c" => "d" } } );Implementations§
source§impl<K, E, Container, Context, End> HashMapSubformer<K, E, Container, Context, End>
impl<K, E, Container, Context, End> HashMapSubformer<K, E, Container, Context, End>
sourcepub fn new(
) -> HashMapSubformer<K, E, Container, Container, impl ToSuperFormer<Container, Container>>
pub fn new( ) -> HashMapSubformer<K, E, Container, Container, impl ToSuperFormer<Container, Container>>
Create a new instance without context or on end processing. It just returns continaer on end of forming.
sourcepub fn begin(
context: Option<Context>,
container: Option<Container>,
on_end: End
) -> Self
pub fn begin( context: Option<Context>, container: Option<Container>, on_end: End ) -> Self
Make a new HashMapSubformer. It should be called by a context generated for your structure. The context is returned after completion of forming by function `on_end``.
source§impl<K, E, Container, Context, End> HashMapSubformer<K, E, Container, Context, End>
impl<K, E, Container, Context, End> HashMapSubformer<K, E, Container, Context, End>
sourcepub fn insert<K2, E2>(self, k: K2, e: E2) -> Self
pub fn insert<K2, E2>(self, k: K2, e: E2) -> Self
Inserts a key-value pair into the container. If the container doesn’t exist, it is created.
§Parameters
k: The key for the value to be inserted. Will be converted into the container’s key type.e: The value to be inserted. Will be converted into the container’s value type.
§Returns
Returns self for chaining further insertions or operations.