Struct former::exposed::HashSetSubformer
source · pub struct HashSetSubformer<E, Formed, Context, ContainerEnd>{ /* private fields */ }
Expand description
Facilitates building HashSetLike
containers with a fluent API.
HashSetSubformer
leverages the HashSetLike
trait to enable a concise and expressive way
of populating HashSet
-like containers. It exemplifies the crate’s builder pattern variation for sets.
§Example Usage
Using HashSetSubformer
to populate a HashSet
within a struct:
#[ derive( Debug, PartialEq, former::Former ) ]
pub struct StructWithSet
{
#[ subformer( former::HashSetSubformer ) ]
set : std::collections::HashSet< &'static str >,
}
let instance = StructWithSet::former()
.set()
.insert( "apple" )
.insert( "banana" )
.end()
.form();
assert_eq!(instance, StructWithSet { set : hset![ "apple", "banana" ] });
Implementations§
source§impl<E, Formed, Context, ContainerEnd> HashSetSubformer<E, Formed, Context, ContainerEnd>
impl<E, Formed, Context, ContainerEnd> HashSetSubformer<E, Formed, Context, ContainerEnd>
sourcepub fn begin(
formed: Option<Formed>,
context: Option<Context>,
on_end: ContainerEnd
) -> Self
pub fn begin( formed: Option<Formed>, context: Option<Context>, on_end: ContainerEnd ) -> Self
Begins the building process with an optional context and formed.
This method is typically called internally by the builder but can be used directly to initialize the builder with specific contexts or containers.
§Parameters
context
: An optional context for the building process.formed
: An optional initial formed to populate.on_end
: A handler to be called at the end of the building process.
sourcepub fn end(self) -> Context
pub fn end(self) -> Context
Finalizes the building process and returns the constructed formed or a context.
This method concludes the building process by applying the on_end
handler to transform
the formed or incorporate it into a given context. It’s typically called at the end
of the builder chain to retrieve the final product of the building process.
§Returns
Depending on the on_end
handler’s implementation, this method can return either the
constructed formed or a context that incorporates the formed.
sourcepub fn replace(self, formed: Formed) -> Self
pub fn replace(self, formed: Formed) -> Self
Replaces the current formed with a new one.
This method allows for replacing the entire set being built with a different one. It can be useful in scenarios where a pre-populated set needs to be modified or replaced entirely during the building process.
§Parameters
formed
: The new formed to use for subsequent builder operations.
§Returns
The builder instance with the formed replaced, enabling further chained operations.
source§impl<E, Formed> HashSetSubformer<E, Formed, Formed, ReturnFormed>
impl<E, Formed> HashSetSubformer<E, Formed, Formed, ReturnFormed>
sourcepub fn new() -> Self
pub fn new() -> Self
Initializes a new instance of the builder with default settings.
This method provides a starting point for building a HashSetLike
formed using
a fluent interface. It sets up an empty formed ready to be populated.
§Returns
A new instance of HashSetSubformer
with no elements.
source§impl<E, Formed, Context, ContainerEnd> HashSetSubformer<E, Formed, Context, ContainerEnd>
impl<E, Formed, Context, ContainerEnd> HashSetSubformer<E, Formed, Context, ContainerEnd>
sourcepub fn insert<E2>(self, element: E2) -> Selfwhere
E2: Into<E>,
pub fn insert<E2>(self, element: E2) -> Selfwhere
E2: Into<E>,
Inserts an element into the set, possibly replacing an existing element.
This method ensures that the set contains the given element, and if the element was already present, it might replace it depending on the formed’s behavior.
§Parameters
element
: The element to insert into the set.
§Returns
Some(element)
if the element was replaced.None
if the element was newly inserted without replacing any existing element.