From 1daded7041f6e3595019f4693ca947f6dbbbc74a Mon Sep 17 00:00:00 2001 From: wandalen Date: Sat, 23 Mar 2024 19:52:34 +0200 Subject: [PATCH] former : experimenting --- module/core/former/src/axiomatic.rs | 183 +++++---- module/core/former/src/axiomatic3.rs | 211 ---------- module/core/former/src/container.rs | 162 ++++++++ module/core/former/src/hash_map.rs | 354 ++++++++-------- module/core/former/src/hash_set.rs | 386 +++++++++--------- module/core/former/src/lib.rs | 2 - module/core/former/src/vector.rs | 344 ++++++++-------- module/core/former/src/vector3.rs | 221 ---------- ...hashmap.rs => container_former_hashmap.rs} | 0 ...hashset.rs => container_former_hashset.rs} | 0 ...bformer_vec.rs => container_former_vec.rs} | 31 +- module/core/former/tests/inc/mod.rs | 18 +- 12 files changed, 843 insertions(+), 1069 deletions(-) delete mode 100644 module/core/former/src/axiomatic3.rs delete mode 100644 module/core/former/src/vector3.rs rename module/core/former/tests/inc/former_tests/{subformer_hashmap.rs => container_former_hashmap.rs} (100%) rename module/core/former/tests/inc/former_tests/{subformer_hashset.rs => container_former_hashset.rs} (100%) rename module/core/former/tests/inc/former_tests/{subformer_vec.rs => container_former_vec.rs} (52%) diff --git a/module/core/former/src/axiomatic.rs b/module/core/former/src/axiomatic.rs index 161a0f3ea8..1587a215ab 100644 --- a/module/core/former/src/axiomatic.rs +++ b/module/core/former/src/axiomatic.rs @@ -1,14 +1,42 @@ //! .... +pub trait Storage : ::core::default::Default +{ + type Definition : FormerDefinition< Storage = Self >; +} + +/// xxx +pub trait StoragePerform : Storage +{ + fn preform( self ) -> < < Self as Storage >::Definition as FormerDefinition >::Formed; +} + +/// xxx +pub trait FormerDefinition +{ + // type Storage : StoragePerform< Definition = Self >; + type Storage : Storage< Definition = Self >; + type Formed; +} + +// pub trait FormerDefinition +// { +// type Storage : StoragePerform< Formed = Self::Formed >; +// type Formed; +// type Context; +// type FormerDefinition : FormerDefinition< Storage = Self::Storage, Formed = Self::Formed >; +// type End : FormingEnd< Self::FormerDefinition, Self::Context >; +// } + /// Defines a handler for the end of a subforming process, enabling the return of the original context. /// /// This trait is designed to be flexible, allowing for various end-of-forming behaviors in builder patterns. /// Implementors can define how to transform or pass through the context during the forming process's completion. /// /// # Parameters -/// - `Formed`: The type of the container being processed. +/// - `Storage`: The type of the container being processed. /// - `Context`: The type of the context that might be altered or returned upon completion. -pub trait FormingEnd< Formed, Context > +pub trait FormingEnd< Definition : FormerDefinition, Context > { /// Called at the end of the subforming process to return the modified or original context. /// @@ -18,21 +46,41 @@ pub trait FormingEnd< Formed, Context > /// /// # Returns /// Returns the transformed or original context based on the implementation. - #[ allow( dead_code ) ] - fn call( &self, storage : Formed, context : core::option::Option< Context > ) -> Context; + fn call( &self, storage : Definition::Storage, context : core::option::Option< Context > ) -> Definition::Formed; } -impl< Storage, Context, F > FormingEnd< Storage, Context > for F +impl< Definition : FormerDefinition, Context, F > FormingEnd< Definition, Context > for F where - F : Fn( Storage, core::option::Option< Context > ) -> Context, + F : Fn( Definition::Storage, core::option::Option< Context > ) -> Definition::Formed, { #[ inline( always ) ] - fn call( &self, storage : Storage, context : core::option::Option< Context > ) -> Context + fn call( &self, storage : Definition::Storage, context : core::option::Option< Context > ) -> Definition::Formed { self( storage, context ) } } +/// A `FormingEnd` implementation that returns the formed container itself instead of the context. +/// +/// This struct is useful when the forming process should result in the formed container being returned directly, +/// bypassing any additional context processing. It simplifies scenarios where the formed container is the final result. +#[ derive( Debug, Default ) ] +pub struct ReturnFormed; + +impl< Definition : FormerDefinition > FormingEnd< Definition, () > +for ReturnFormed +where + Definition::Storage : StoragePerform< Definition = Definition >, + // xxx : rename Former -> Definition + // xxx : rename Definition -> Definition +{ + #[ inline( always ) ] + fn call( &self, storage : Definition::Storage, _context : core::option::Option< () > ) -> Definition::Formed + { + storage.preform() + } +} + /// A wrapper around a closure to be used as a `FormingEnd`. /// /// This struct allows for dynamic dispatch of a closure that matches the @@ -47,14 +95,14 @@ where /// * `Context` - The type of the context that may be altered or returned by the closure. /// This allows for flexible manipulation of context based on the container. #[ cfg( not( feature = "no_std" ) ) ] -pub struct FormingEndWrapper< Storage, Context > +pub struct FormingEndWrapper< Definition : FormerDefinition, Context > { - closure : Box< dyn Fn( Storage, Option< Context > ) -> Context >, - _marker : std::marker::PhantomData< Storage >, + closure : Box< dyn Fn( Definition::Storage, Option< Context > ) -> Definition::Formed >, + _marker : std::marker::PhantomData< Definition::Storage >, } #[ cfg( not( feature = "no_std" ) ) ] -impl< Storage, Context > FormingEndWrapper< Storage, Context > +impl< Definition : FormerDefinition, Context > FormingEndWrapper< Definition, Context > { /// Constructs a new `FormingEndWrapper` with the provided closure. /// @@ -67,7 +115,7 @@ impl< Storage, Context > FormingEndWrapper< Storage, Context > /// # Returns /// /// Returns an instance of `FormingEndWrapper` encapsulating the provided closure. - pub fn new( closure : impl Fn( Storage, Option< Context > ) -> Context + 'static ) -> Self + pub fn new( closure : impl Fn( Definition::Storage, Option< Context > ) -> Definition::Formed + 'static ) -> Self { Self { @@ -80,7 +128,7 @@ impl< Storage, Context > FormingEndWrapper< Storage, Context > #[ cfg( not( feature = "no_std" ) ) ] use std::fmt; #[ cfg( not( feature = "no_std" ) ) ] -impl< Storage, Context > fmt::Debug for FormingEndWrapper< Storage, Context > +impl< Definition : FormerDefinition, Context > fmt::Debug for FormingEndWrapper< Definition, Context > { fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result { @@ -92,99 +140,70 @@ impl< Storage, Context > fmt::Debug for FormingEndWrapper< Storage, Context > } #[ cfg( not( feature = "no_std" ) ) ] -impl< Storage, Context > FormingEnd< Storage, Context > -for FormingEndWrapper< Storage, Context > +impl< Definition : FormerDefinition, Context > FormingEnd< Definition, Context > +for FormingEndWrapper< Definition, Context > { - fn call( &self, storage : Storage, context : Option< Context > ) -> Context + fn call( &self, storage : Definition::Storage, context : Option< Context > ) -> Definition::Formed { ( self.closure )( storage, context ) } } -// /// A `FormingEnd` implementation that returns the original context without any modifications. -// /// -// /// This struct is used when no end-of-forming processing is needed, and the original context is to be returned as-is. -// #[ derive( Debug, Default ) ] -// pub struct NoEnd; // -// impl< Formed, Context > FormingEnd< Formed, Context > -// for NoEnd -// { -// #[ inline( always ) ] -// fn call( &self, _formed : Formed, context : core::option::Option< Context > ) -> Context -// { -// context.unwrap() -// } -// } -/// A `FormingEnd` implementation that returns the formed container itself instead of the context. +/// A trait for initiating a structured subforming process with contextual and intermediary storage linkage. /// -/// This struct is useful when the forming process should result in the formed container being returned directly, -/// bypassing any additional context processing. It simplifies scenarios where the formed container is the final result. -#[ derive( Debug, Default ) ] -pub struct ReturnFormed; - -impl< Storage > FormingEnd< Storage, Storage > -for ReturnFormed -{ - #[ inline( always ) ] - fn call( &self, storage : Storage, _context : core::option::Option< Storage > ) -> Storage - { - storage - } -} - -// - -/// A trait defining the initialization process for a subformer with contextual linkage. -/// -/// This trait is designed for types that need to initiate a subforming process, -/// passing themselves as the context and specifying a closure or handler (`on_end`) to be -/// called upon completion. It facilitates the construction of builder pattern chains -/// that maintain stateful context through each step of the process. +/// This trait facilitates the creation of a subformer that carries through a builder pattern chain, +/// utilizing intermediary storage for accumulating state or data before finally transforming it into +/// a `Formed` structure. It is designed for scenarios where a multi-step construction or transformation +/// process benefits from maintaining both transient state (`Storage`) and contextual information (`Context`), +/// before concluding with the generation of a final product (`Formed`). /// /// # Type Parameters /// -/// * `Formed` - Represents the type that is being constructed or transformed by the subformer. -/// * `Context` - Denotes the contextual information or the environment in which `Formed` is being formed. -/// This could be a reference to a parent builder, configuration settings, or any other -/// relevant state. +/// * `Storage` - Represents a mutable intermediary storage structure used throughout the subforming process +/// to accumulate data, state, or partial computations. This storage is internal to the +/// subformer and is eventually converted into the final `Formed` structure by the subformer, +/// not directly by implementations of this trait. +/// +/// * `Formed` - Denotes the final type that results from the subforming process. This is the intended outcome +/// of the builder chain, constructed or transformed from the `Storage` with consideration of +/// the provided `Context`. +/// +/// * `Context` - Specifies the contextual backdrop against which the subforming process unfolds. This could +/// encompass references to parent builders, configuration data, or any state influencing how +/// `Storage` transitions into `Formed`. /// -/// # Associated Types +/// # Functions /// -/// * `End` - Specifies the trait bound for the closure or handler that gets called at the completion -/// of the subforming process. This type must implement the `FormingEnd` -/// trait, which defines how the final transformation or construction of `Formed` is handled, -/// potentially using the provided `Context`. +/// * `_begin` - This function launches the subforming process, marking the start of a construction or transformation +/// sequence defined by the implementing type. It establishes the foundational `Storage` and `Context`, +/// alongside specifying an `on_end` completion handler that dictates the final conversion into `Formed`. /// +/// The `FormerBegin` trait, by decoupling `Storage` from `Formed` and introducing a contextual layer, enables +/// sophisticated and flexible construction patterns conducive to complex data transformations or object creation +/// sequences within builder patterns. -pub trait FormerBegin< Storage, Formed, Context > +pub trait FormerBegin< Definition : FormerDefinition, Context > { - /// * `End` - Specifies the trait bound for the closure or handler that gets called at the completion - /// of the subforming process. This type must implement the `FormingEnd` - /// trait, which defines how the final transformation or construction of `Formed` is handled, - /// potentially using the provided `Context`. - type End : FormingEnd< Formed, Context >; + /// * `End` - A trait bound marking the closure or handler invoked upon completing the subforming process. Implementers + /// of this trait (`End`) are tasked with applying the final transformations that transition `Storage` + /// into `Formed`, optionally utilizing `Context` to guide this transformation. It is crucial that this + /// associated type satisfies the `FormingEnd` trait, defining the precise mechanics of + /// how the subformer concludes its operation. + type End : FormingEnd< Definition, Context >; - /// Initializes the subforming process by setting the context and specifying an `on_end` completion handler. - /// - /// This function is the entry point for initiating a subforming sequence, allowing the caller - /// to establish initial contextual information and define how the process concludes. + /// Launches the subforming process with an initial storage and context, setting up an `on_end` completion handler. /// /// # Parameters /// - /// * `context` - An optional parameter providing initial context for the subforming process. This - /// might include configuration data, references to parent structures, or any state - /// relevant to the formation of `Formed`. - /// - /// * `on_end` - A closure or handler of type `Self::End` that is invoked at the completion of - /// the subforming process. This handler is responsible for applying any final transformations - /// to `Formed` and potentially utilizing `Context` to influence the outcome. - /// + /// * `storage` - An optional initial state for the intermediary storage structure. + /// * `context` - An optional initial setting providing contextual information for the subforming process. + /// * `on_end` - A completion handler responsible for transforming the accumulated `Storage` into the final `Formed` structure. fn _begin ( - storage : core::option::Option< Storage >, + storage : core::option::Option< Definition::Storage >, context : core::option::Option< Context >, on_end : Self::End, ) -> Self; diff --git a/module/core/former/src/axiomatic3.rs b/module/core/former/src/axiomatic3.rs deleted file mode 100644 index 1587a215ab..0000000000 --- a/module/core/former/src/axiomatic3.rs +++ /dev/null @@ -1,211 +0,0 @@ -//! .... - -pub trait Storage : ::core::default::Default -{ - type Definition : FormerDefinition< Storage = Self >; -} - -/// xxx -pub trait StoragePerform : Storage -{ - fn preform( self ) -> < < Self as Storage >::Definition as FormerDefinition >::Formed; -} - -/// xxx -pub trait FormerDefinition -{ - // type Storage : StoragePerform< Definition = Self >; - type Storage : Storage< Definition = Self >; - type Formed; -} - -// pub trait FormerDefinition -// { -// type Storage : StoragePerform< Formed = Self::Formed >; -// type Formed; -// type Context; -// type FormerDefinition : FormerDefinition< Storage = Self::Storage, Formed = Self::Formed >; -// type End : FormingEnd< Self::FormerDefinition, Self::Context >; -// } - -/// Defines a handler for the end of a subforming process, enabling the return of the original context. -/// -/// This trait is designed to be flexible, allowing for various end-of-forming behaviors in builder patterns. -/// Implementors can define how to transform or pass through the context during the forming process's completion. -/// -/// # Parameters -/// - `Storage`: The type of the container being processed. -/// - `Context`: The type of the context that might be altered or returned upon completion. -pub trait FormingEnd< Definition : FormerDefinition, Context > -{ - /// Called at the end of the subforming process to return the modified or original context. - /// - /// # Parameters - /// - `container`: The container being processed. - /// - `context`: Optional context to be transformed or returned. - /// - /// # Returns - /// Returns the transformed or original context based on the implementation. - fn call( &self, storage : Definition::Storage, context : core::option::Option< Context > ) -> Definition::Formed; -} - -impl< Definition : FormerDefinition, Context, F > FormingEnd< Definition, Context > for F -where - F : Fn( Definition::Storage, core::option::Option< Context > ) -> Definition::Formed, -{ - #[ inline( always ) ] - fn call( &self, storage : Definition::Storage, context : core::option::Option< Context > ) -> Definition::Formed - { - self( storage, context ) - } -} - -/// A `FormingEnd` implementation that returns the formed container itself instead of the context. -/// -/// This struct is useful when the forming process should result in the formed container being returned directly, -/// bypassing any additional context processing. It simplifies scenarios where the formed container is the final result. -#[ derive( Debug, Default ) ] -pub struct ReturnFormed; - -impl< Definition : FormerDefinition > FormingEnd< Definition, () > -for ReturnFormed -where - Definition::Storage : StoragePerform< Definition = Definition >, - // xxx : rename Former -> Definition - // xxx : rename Definition -> Definition -{ - #[ inline( always ) ] - fn call( &self, storage : Definition::Storage, _context : core::option::Option< () > ) -> Definition::Formed - { - storage.preform() - } -} - -/// A wrapper around a closure to be used as a `FormingEnd`. -/// -/// This struct allows for dynamic dispatch of a closure that matches the -/// `FormingEnd` trait's `call` method signature. It is useful for cases where -/// a closure needs to be stored or passed around as an object implementing -/// `FormingEnd`. -/// -/// # Type Parameters -/// -/// * `Storage` - The type of the container being processed. This type is passed to the closure -/// when it's called. -/// * `Context` - The type of the context that may be altered or returned by the closure. -/// This allows for flexible manipulation of context based on the container. -#[ cfg( not( feature = "no_std" ) ) ] -pub struct FormingEndWrapper< Definition : FormerDefinition, Context > -{ - closure : Box< dyn Fn( Definition::Storage, Option< Context > ) -> Definition::Formed >, - _marker : std::marker::PhantomData< Definition::Storage >, -} - -#[ cfg( not( feature = "no_std" ) ) ] -impl< Definition : FormerDefinition, Context > FormingEndWrapper< Definition, Context > -{ - /// Constructs a new `FormingEndWrapper` with the provided closure. - /// - /// # Parameters - /// - /// * `closure` - A closure that matches the expected signature for transforming a container - /// and context into a new context. This closure is stored and called by the - /// `call` method of the `FormingEnd` trait implementation. - /// - /// # Returns - /// - /// Returns an instance of `FormingEndWrapper` encapsulating the provided closure. - pub fn new( closure : impl Fn( Definition::Storage, Option< Context > ) -> Definition::Formed + 'static ) -> Self - { - Self - { - closure : Box::new( closure ), - _marker : std::marker::PhantomData - } - } -} - -#[ cfg( not( feature = "no_std" ) ) ] -use std::fmt; -#[ cfg( not( feature = "no_std" ) ) ] -impl< Definition : FormerDefinition, Context > fmt::Debug for FormingEndWrapper< Definition, Context > -{ - fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result - { - f.debug_struct( "FormingEndWrapper" ) - .field( "closure", &format_args!{ "- closure -" } ) - .field( "_marker", &self._marker ) - .finish() - } -} - -#[ cfg( not( feature = "no_std" ) ) ] -impl< Definition : FormerDefinition, Context > FormingEnd< Definition, Context > -for FormingEndWrapper< Definition, Context > -{ - fn call( &self, storage : Definition::Storage, context : Option< Context > ) -> Definition::Formed - { - ( self.closure )( storage, context ) - } -} - -// - -/// A trait for initiating a structured subforming process with contextual and intermediary storage linkage. -/// -/// This trait facilitates the creation of a subformer that carries through a builder pattern chain, -/// utilizing intermediary storage for accumulating state or data before finally transforming it into -/// a `Formed` structure. It is designed for scenarios where a multi-step construction or transformation -/// process benefits from maintaining both transient state (`Storage`) and contextual information (`Context`), -/// before concluding with the generation of a final product (`Formed`). -/// -/// # Type Parameters -/// -/// * `Storage` - Represents a mutable intermediary storage structure used throughout the subforming process -/// to accumulate data, state, or partial computations. This storage is internal to the -/// subformer and is eventually converted into the final `Formed` structure by the subformer, -/// not directly by implementations of this trait. -/// -/// * `Formed` - Denotes the final type that results from the subforming process. This is the intended outcome -/// of the builder chain, constructed or transformed from the `Storage` with consideration of -/// the provided `Context`. -/// -/// * `Context` - Specifies the contextual backdrop against which the subforming process unfolds. This could -/// encompass references to parent builders, configuration data, or any state influencing how -/// `Storage` transitions into `Formed`. -/// -/// # Functions -/// -/// * `_begin` - This function launches the subforming process, marking the start of a construction or transformation -/// sequence defined by the implementing type. It establishes the foundational `Storage` and `Context`, -/// alongside specifying an `on_end` completion handler that dictates the final conversion into `Formed`. -/// -/// The `FormerBegin` trait, by decoupling `Storage` from `Formed` and introducing a contextual layer, enables -/// sophisticated and flexible construction patterns conducive to complex data transformations or object creation -/// sequences within builder patterns. - -pub trait FormerBegin< Definition : FormerDefinition, Context > -{ - - /// * `End` - A trait bound marking the closure or handler invoked upon completing the subforming process. Implementers - /// of this trait (`End`) are tasked with applying the final transformations that transition `Storage` - /// into `Formed`, optionally utilizing `Context` to guide this transformation. It is crucial that this - /// associated type satisfies the `FormingEnd` trait, defining the precise mechanics of - /// how the subformer concludes its operation. - type End : FormingEnd< Definition, Context >; - - /// Launches the subforming process with an initial storage and context, setting up an `on_end` completion handler. - /// - /// # Parameters - /// - /// * `storage` - An optional initial state for the intermediary storage structure. - /// * `context` - An optional initial setting providing contextual information for the subforming process. - /// * `on_end` - A completion handler responsible for transforming the accumulated `Storage` into the final `Formed` structure. - fn _begin - ( - storage : core::option::Option< Definition::Storage >, - context : core::option::Option< Context >, - on_end : Self::End, - ) -> Self; - -} diff --git a/module/core/former/src/container.rs b/module/core/former/src/container.rs index cfdbfdb4d1..e68732dfa6 100644 --- a/module/core/former/src/container.rs +++ b/module/core/former/src/container.rs @@ -1,5 +1,7 @@ //! Interface for containers. +use crate::*; + /// A trait defining the capability to add elements to a container. /// /// This trait should be implemented by container types that require a generic interface @@ -188,3 +190,163 @@ where self.len() - initial_len } } + +// = + + +/// A builder for constructing containers, facilitating a fluent and flexible interface. +#[ derive( Debug, Default ) ] +pub struct ContainerSubformer< E, Definition, Context = (), End = ReturnFormed > +where + End : FormingEnd< Definition, Context >, + Definition : FormerDefinition, + Definition::Storage : ContainerAdd< Element = E >, +{ + storage : core::option::Option< Definition::Storage >, + context : core::option::Option< Context >, + on_end : core::option::Option< End >, +} + +impl< E, Definition, Context, End > ContainerSubformer< E, Definition, Context, End > +where + End : FormingEnd< Definition, Context >, + Definition : FormerDefinition, + Definition::Storage : ContainerAdd< Element = E >, +{ + + /// Form current former into target structure. + #[ inline( always ) ] + pub fn storage( mut self ) -> Definition::Storage + { + let storage = if self.storage.is_some() + { + self.storage.take().unwrap() + } + else + { + let val = Default::default(); + val + }; + storage + } + + /// Begins the building process, optionally initializing with a context and storage. + #[ inline( always ) ] + pub fn begin + ( + storage : core::option::Option< Definition::Storage >, + context : core::option::Option< Context >, + on_end : End + ) -> Self + { + Self + { + storage, + context, + on_end : Some( on_end ), + } + } + + /// Finalizes the building process, returning the formed or a context incorporating it. + #[ inline( always ) ] + pub fn end( mut self ) -> Definition::Formed + { + let on_end = self.on_end.take().unwrap(); + let context = self.context.take(); + let storage = self.storage(); + on_end.call( storage, context ) + } + + /// Finalizes the building process, returning the formed or a context incorporating it. + #[ inline( always ) ] + pub fn form( self ) -> Definition::Formed + { + self.end() + } + + /// Replaces the current storage with a provided one, allowing for a reset or redirection of the building process. + #[ inline( always ) ] + pub fn replace( mut self, vector : Definition::Storage ) -> Self + { + self.storage = Some( vector ); + self + } + +} + +impl< E, Definition > ContainerSubformer< E, Definition, (), ReturnFormed > +where + Definition : FormerDefinition, + Definition::Storage : ContainerAdd< Element = E >, + Definition::Storage : StoragePerform< Definition = Definition >, +{ + + /// Initializes a new `ContainerSubformer` instance, starting with an empty formed. + /// This function serves as the entry point for the builder pattern. + /// + /// # Returns + /// A new instance of `ContainerSubformer` with an empty internal formed. + /// + #[ inline( always ) ] + pub fn new() -> Self + { + Self::begin + ( + None, + None, + ReturnFormed, + ) + } + +} + +impl< E, Definition, Context, End > ContainerSubformer< E, Definition, Context, End > +where + End : FormingEnd< Definition, Context >, + Definition : FormerDefinition, + Definition::Storage : ContainerAdd< Element = E >, +{ + + /// Appends an element to the end of the storage, expanding the internal collection. + #[ inline( always ) ] + pub fn push< IntoElement >( mut self, element : IntoElement ) -> Self + where IntoElement : core::convert::Into< E >, + { + if self.storage.is_none() + { + self.storage = core::option::Option::Some( Default::default() ); + } + if let core::option::Option::Some( ref mut storage ) = self.storage + { + ContainerAdd::add( storage, element.into() ); + // storage.push( element.into() ); + } + self + } + +} + +// + +impl< E, Definition, Context, End > FormerBegin< Definition, Context > +for ContainerSubformer< E, Definition, Context, End > +where + End : FormingEnd< Definition, Context >, + Definition : FormerDefinition, + Definition::Storage : ContainerAdd< Element = E >, +{ + type End = End; + + #[ inline( always ) ] + fn _begin + ( + storage : core::option::Option< Definition::Storage >, + context : core::option::Option< Context >, + on_end : End, + ) + -> Self + { + Self::begin( storage, context, on_end ) + } + +} diff --git a/module/core/former/src/hash_map.rs b/module/core/former/src/hash_map.rs index d4f0d278d3..6448031d7f 100644 --- a/module/core/former/src/hash_map.rs +++ b/module/core/former/src/hash_map.rs @@ -80,10 +80,6 @@ where K : ::core::cmp::Eq + ::core::hash::Hash, { type Definition = HashMapDefinition< K, E >; - // fn preform( self ) -> < < Self as Storage >::Definition as FormerDefinition >::Formed - // { - // self - // } } impl< K, E > StoragePerform @@ -135,179 +131,181 @@ where /// # } /// ``` -#[ derive( Debug, Default ) ] -pub struct HashMapSubformer< K, E, Definition, Context, End > -where - K : ::core::cmp::Eq + ::core::hash::Hash, - // Formed : HashMapLike< K, E > + ::core::default::Default, - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = ( K, E ) >, -{ - storage : ::core::option::Option< Definition::Storage >, - context : ::core::option::Option< Context >, - on_end : ::core::option::Option< End >, - _e_phantom : ::core::marker::PhantomData< E >, - _k_phantom : ::core::marker::PhantomData< K >, -} - -impl< K, E, Definition, Context, End > -HashMapSubformer< K, E, Definition, Context, End > -where - K : ::core::cmp::Eq + ::core::hash::Hash, - // Formed : HashMapLike< K, E > + ::core::default::Default, - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = ( K, E ) >, -{ - - /// Form current former into target structure. - #[ inline( always ) ] - pub fn storage( mut self ) -> Definition::Storage - { - // xxx - let storage = if self.storage.is_some() - { - self.storage.take().unwrap() - } - else - { - let val = Default::default(); - val - }; - storage - // storage.preform() - } - // xxx - - - /// 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``. - #[ inline( always ) ] - pub fn begin - ( - storage : ::core::option::Option< Definition::Storage >, - context : ::core::option::Option< Context >, - on_end : End, - ) - -> Self - { - Self - { - storage, - context, - on_end : Some( on_end ), - _e_phantom : ::core::marker::PhantomData, - _k_phantom : ::core::marker::PhantomData, - } - } - - /// Return context of your struct moving formed there. Should be called after forming process. - #[ inline( always ) ] - pub fn end( mut self ) -> Definition::Formed - { - let on_end = self.on_end.take().unwrap(); - let context = self.context.take(); - let storage = self.storage(); - on_end.call( storage, context ) - } - - /// Return context of your struct moving formed there. Should be called after forming process. - #[ inline( always ) ] - pub fn form( self ) -> Definition::Formed - { - self.end() - } - - /// Set the whole storage instead of setting each element individually. - #[ inline( always ) ] - pub fn replace( mut self, storage : Definition::Storage ) -> Self - { - self.storage = Some( storage ); - self - } - -} - -impl< K, E, Definition > -HashMapSubformer< K, E, Definition, (), crate::ReturnFormed > -where - K : ::core::cmp::Eq + ::core::hash::Hash, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = ( K, E ) >, - Definition::Storage : StoragePerform< Definition = Definition >, -{ - - /// Create a new instance without context or on end processing. It just returns continaer on end of forming. - #[ inline( always ) ] - pub fn new() -> Self - { - HashMapSubformer::begin - ( - None, - None, - crate::ReturnFormed, - ) - } - -} - -impl< K, E, Definition, Context, End > -HashMapSubformer< K, E, Definition, Context, End > -where - K : ::core::cmp::Eq + ::core::hash::Hash, - // Formed : HashMapLike< K, E > + ::core::default::Default, - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = ( K, E ) >, -{ - - /// Inserts a key-value pair into the formed. If the formed doesn't exist, it is created. - /// - /// # Parameters - /// - `k`: The key for the value to be inserted. Will be converted into the formed's key type. - /// - `e`: The value to be inserted. Will be converted into the formed's value type. - /// - /// # Returns - /// Returns `self` for chaining further insertions or operations. - /// - #[ inline( always ) ] - pub fn insert< K2, E2 >( mut self, k : K2, e : E2 ) -> Self - where - K2 : ::core::convert::Into< K >, - E2 : ::core::convert::Into< E >, - // Definition::Storage : ContainerAdd< Element = ( K, E ) >, - { - if self.storage.is_none() - { - self.storage = ::core::option::Option::Some( Default::default() ); - } - if let ::core::option::Option::Some( ref mut storage ) = self.storage - { - ContainerAdd::add( storage, ( k.into(), e.into() ) ); - // storage.insert( k.into(), e.into() ); - } - self - } - - /// Alias for insert. - /// - /// # Parameters - /// - `k`: The key for the value to be inserted. Will be converted into the formed's key type. - /// - `e`: The value to be inserted. Will be converted into the formed's value type. - /// - /// # Returns - /// Returns `self` for chaining further insertions or operations. - /// - #[ inline( always ) ] - pub fn push< K2, E2 >( self, k : K2, e : E2 ) -> Self - where - K2 : ::core::convert::Into< K >, - E2 : ::core::convert::Into< E >, - { - self.insert( k, e ) - } - -} +pub type HashMapSubformer< K, E > = ContainerSubformer::< ( K, E ), HashMapDefinition< K, E > >; + +// #[ derive( Debug, Default ) ] +// pub struct HashMapSubformer< K, E, Definition, Context, End > +// where +// K : ::core::cmp::Eq + ::core::hash::Hash, +// // Formed : HashMapLike< K, E > + ::core::default::Default, +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = ( K, E ) >, +// { +// storage : ::core::option::Option< Definition::Storage >, +// context : ::core::option::Option< Context >, +// on_end : ::core::option::Option< End >, +// _e_phantom : ::core::marker::PhantomData< E >, +// _k_phantom : ::core::marker::PhantomData< K >, +// } +// +// impl< K, E, Definition, Context, End > +// HashMapSubformer< K, E, Definition, Context, End > +// where +// K : ::core::cmp::Eq + ::core::hash::Hash, +// // Formed : HashMapLike< K, E > + ::core::default::Default, +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = ( K, E ) >, +// { +// +// /// Form current former into target structure. +// #[ inline( always ) ] +// pub fn storage( mut self ) -> Definition::Storage +// { +// // xxx +// let storage = if self.storage.is_some() +// { +// self.storage.take().unwrap() +// } +// else +// { +// let val = Default::default(); +// val +// }; +// storage +// // storage.preform() +// } +// // xxx +// +// +// /// 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``. +// #[ inline( always ) ] +// pub fn begin +// ( +// storage : ::core::option::Option< Definition::Storage >, +// context : ::core::option::Option< Context >, +// on_end : End, +// ) +// -> Self +// { +// Self +// { +// storage, +// context, +// on_end : Some( on_end ), +// _e_phantom : ::core::marker::PhantomData, +// _k_phantom : ::core::marker::PhantomData, +// } +// } +// +// /// Return context of your struct moving formed there. Should be called after forming process. +// #[ inline( always ) ] +// pub fn end( mut self ) -> Definition::Formed +// { +// let on_end = self.on_end.take().unwrap(); +// let context = self.context.take(); +// let storage = self.storage(); +// on_end.call( storage, context ) +// } +// +// /// Return context of your struct moving formed there. Should be called after forming process. +// #[ inline( always ) ] +// pub fn form( self ) -> Definition::Formed +// { +// self.end() +// } +// +// /// Set the whole storage instead of setting each element individually. +// #[ inline( always ) ] +// pub fn replace( mut self, storage : Definition::Storage ) -> Self +// { +// self.storage = Some( storage ); +// self +// } +// +// } +// +// impl< K, E, Definition > +// HashMapSubformer< K, E, Definition, (), crate::ReturnFormed > +// where +// K : ::core::cmp::Eq + ::core::hash::Hash, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = ( K, E ) >, +// Definition::Storage : StoragePerform< Definition = Definition >, +// { +// +// /// Create a new instance without context or on end processing. It just returns continaer on end of forming. +// #[ inline( always ) ] +// pub fn new() -> Self +// { +// HashMapSubformer::begin +// ( +// None, +// None, +// crate::ReturnFormed, +// ) +// } +// +// } +// +// impl< K, E, Definition, Context, End > +// HashMapSubformer< K, E, Definition, Context, End > +// where +// K : ::core::cmp::Eq + ::core::hash::Hash, +// // Formed : HashMapLike< K, E > + ::core::default::Default, +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = ( K, E ) >, +// { +// +// /// Inserts a key-value pair into the formed. If the formed doesn't exist, it is created. +// /// +// /// # Parameters +// /// - `k`: The key for the value to be inserted. Will be converted into the formed's key type. +// /// - `e`: The value to be inserted. Will be converted into the formed's value type. +// /// +// /// # Returns +// /// Returns `self` for chaining further insertions or operations. +// /// +// #[ inline( always ) ] +// pub fn insert< K2, E2 >( mut self, k : K2, e : E2 ) -> Self +// where +// K2 : ::core::convert::Into< K >, +// E2 : ::core::convert::Into< E >, +// // Definition::Storage : ContainerAdd< Element = ( K, E ) >, +// { +// if self.storage.is_none() +// { +// self.storage = ::core::option::Option::Some( Default::default() ); +// } +// if let ::core::option::Option::Some( ref mut storage ) = self.storage +// { +// ContainerAdd::add( storage, ( k.into(), e.into() ) ); +// // storage.insert( k.into(), e.into() ); +// } +// self +// } +// +// /// Alias for insert. +// /// +// /// # Parameters +// /// - `k`: The key for the value to be inserted. Will be converted into the formed's key type. +// /// - `e`: The value to be inserted. Will be converted into the formed's value type. +// /// +// /// # Returns +// /// Returns `self` for chaining further insertions or operations. +// /// +// #[ inline( always ) ] +// pub fn push< K2, E2 >( self, k : K2, e : E2 ) -> Self +// where +// K2 : ::core::convert::Into< K >, +// E2 : ::core::convert::Into< E >, +// { +// self.insert( k, e ) +// } +// +// } // diff --git a/module/core/former/src/hash_set.rs b/module/core/former/src/hash_set.rs index 0959953156..8c4cdb62f3 100644 --- a/module/core/former/src/hash_set.rs +++ b/module/core/former/src/hash_set.rs @@ -117,198 +117,200 @@ where /// # } /// ``` -#[ derive( Debug, Default ) ] -pub struct HashSetSubformer< K, Definition, Context, End > -where - K : core::cmp::Eq + core::hash::Hash, - // Formed : HashSetLike< K > + core::default::Default, - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = K >, -{ - storage : core::option::Option< Definition::Storage >, - context : core::option::Option< Context >, - on_end : core::option::Option< End >, - _e_phantom : core::marker::PhantomData< K >, -} - -impl< K, Definition, Context, End > -HashSetSubformer< K, Definition, Context, End > -where - K : core::cmp::Eq + core::hash::Hash, - // Formed : HashSetLike< K > + core::default::Default, - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = K >, -{ - - /// Form current former into target structure. - #[ inline( always ) ] - pub fn storage( mut self ) -> Definition::Storage - { - let storage = if self.storage.is_some() - { - self.storage.take().unwrap() - } - else - { - let val = Default::default(); - val - }; - storage - } - // xxx - - /// Begins the building process with an optional context and storage. - /// - /// 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. - /// - `storage`: An optional initial storage to populate. - /// - `on_end`: A handler to be called at the end of the building process. - /// - #[ inline( always ) ] - pub fn begin - ( - storage : core::option::Option< Definition::Storage >, - context : core::option::Option< Context >, - on_end : End, - ) -> Self - { - Self - { - storage, - context : context, - on_end : Some( on_end ), - _e_phantom : core::marker::PhantomData, - } - } - - /// 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. - /// - #[ inline( always ) ] - pub fn form( self ) -> Definition::Formed - { - self.end() - } - - /// 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. - /// - #[ inline( always ) ] - pub fn end( mut self ) -> Definition::Formed - { - let on_end = self.on_end.take().unwrap(); - let context = self.context.take(); - let storage = self.storage(); - on_end.call( storage, context ) - } - - /// Replaces the current storage 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 - /// - `storage`: The new storage to use for subsequent builder operations. - /// - /// # Returns - /// The builder instance with the storage replaced, enabling further chained operations. - /// - #[ inline( always ) ] - pub fn replace( mut self, storage : Definition::Storage ) -> Self - { - self.storage = Some( storage ); - self - } - -} - -impl< K, Definition > -HashSetSubformer< K, Definition, (), crate::ReturnFormed > -where - K : core::cmp::Eq + core::hash::Hash, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = K >, - Definition::Storage : StoragePerform< Definition = Definition >, -{ - - /// Initializes a new instance of the builder with default settings. - /// - /// This method provides a starting point for forming a `HashSetLike` using - /// a fluent interface. - /// - /// # Returns - /// A new instance of `HashSetSubformer` with no elements. - /// - #[ inline( always ) ] - pub fn new() -> Self - { - HashSetSubformer::begin - ( - None, - None, - crate::ReturnFormed, - ) - } - -} - -impl< K, Definition, Context, End > -HashSetSubformer< K, Definition, Context, End > -where - K : core::cmp::Eq + core::hash::Hash, - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = K >, -{ - - /// 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 storage'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. - /// - #[ inline( always ) ] - pub fn insert< E2 >( mut self, element : E2 ) -> Self - where - E2 : core::convert::Into< K >, - { - if self.storage.is_none() - { - self.storage = core::option::Option::Some( Default::default() ); - } - if let core::option::Option::Some( ref mut storage ) = self.storage - { - ContainerAdd::add( storage, element.into() ); - } - self - } +pub type VectorSubformer< K > = ContainerSubformer::< K, HashSetDefinition< K > >; -} +// #[ derive( Debug, Default ) ] +// pub struct HashSetSubformer< K, Definition, Context, End > +// where +// K : core::cmp::Eq + core::hash::Hash, +// // Formed : HashSetLike< K > + core::default::Default, +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = K >, +// { +// storage : core::option::Option< Definition::Storage >, +// context : core::option::Option< Context >, +// on_end : core::option::Option< End >, +// _e_phantom : core::marker::PhantomData< K >, +// } +// +// impl< K, Definition, Context, End > +// HashSetSubformer< K, Definition, Context, End > +// where +// K : core::cmp::Eq + core::hash::Hash, +// // Formed : HashSetLike< K > + core::default::Default, +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = K >, +// { +// +// /// Form current former into target structure. +// #[ inline( always ) ] +// pub fn storage( mut self ) -> Definition::Storage +// { +// let storage = if self.storage.is_some() +// { +// self.storage.take().unwrap() +// } +// else +// { +// let val = Default::default(); +// val +// }; +// storage +// } +// // xxx +// +// /// Begins the building process with an optional context and storage. +// /// +// /// 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. +// /// - `storage`: An optional initial storage to populate. +// /// - `on_end`: A handler to be called at the end of the building process. +// /// +// #[ inline( always ) ] +// pub fn begin +// ( +// storage : core::option::Option< Definition::Storage >, +// context : core::option::Option< Context >, +// on_end : End, +// ) -> Self +// { +// Self +// { +// storage, +// context : context, +// on_end : Some( on_end ), +// _e_phantom : core::marker::PhantomData, +// } +// } +// +// /// 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. +// /// +// #[ inline( always ) ] +// pub fn form( self ) -> Definition::Formed +// { +// self.end() +// } +// +// /// 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. +// /// +// #[ inline( always ) ] +// pub fn end( mut self ) -> Definition::Formed +// { +// let on_end = self.on_end.take().unwrap(); +// let context = self.context.take(); +// let storage = self.storage(); +// on_end.call( storage, context ) +// } +// +// /// Replaces the current storage 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 +// /// - `storage`: The new storage to use for subsequent builder operations. +// /// +// /// # Returns +// /// The builder instance with the storage replaced, enabling further chained operations. +// /// +// #[ inline( always ) ] +// pub fn replace( mut self, storage : Definition::Storage ) -> Self +// { +// self.storage = Some( storage ); +// self +// } +// +// } +// +// impl< K, Definition > +// HashSetSubformer< K, Definition, (), crate::ReturnFormed > +// where +// K : core::cmp::Eq + core::hash::Hash, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = K >, +// Definition::Storage : StoragePerform< Definition = Definition >, +// { +// +// /// Initializes a new instance of the builder with default settings. +// /// +// /// This method provides a starting point for forming a `HashSetLike` using +// /// a fluent interface. +// /// +// /// # Returns +// /// A new instance of `HashSetSubformer` with no elements. +// /// +// #[ inline( always ) ] +// pub fn new() -> Self +// { +// HashSetSubformer::begin +// ( +// None, +// None, +// crate::ReturnFormed, +// ) +// } +// +// } +// +// impl< K, Definition, Context, End > +// HashSetSubformer< K, Definition, Context, End > +// where +// K : core::cmp::Eq + core::hash::Hash, +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = K >, +// { +// +// /// 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 storage'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. +// /// +// #[ inline( always ) ] +// pub fn insert< E2 >( mut self, element : E2 ) -> Self +// where +// E2 : core::convert::Into< K >, +// { +// if self.storage.is_none() +// { +// self.storage = core::option::Option::Some( Default::default() ); +// } +// if let core::option::Option::Some( ref mut storage ) = self.storage +// { +// ContainerAdd::add( storage, element.into() ); +// } +// self +// } +// +// } // \ No newline at end of file diff --git a/module/core/former/src/lib.rs b/module/core/former/src/lib.rs index 978805cca1..4834d7a293 100644 --- a/module/core/former/src/lib.rs +++ b/module/core/former/src/lib.rs @@ -9,7 +9,6 @@ /// Axiomatic things. #[ cfg( feature = "enabled" ) ] #[ cfg( feature = "derive_former" ) ] -#[ path = "axiomatic3.rs" ] mod axiomatic; /// Interface for containers. @@ -21,7 +20,6 @@ mod container; #[ cfg( feature = "enabled" ) ] #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] #[ cfg( feature = "derive_former" ) ] -#[ path = "vector3.rs" ] mod vector; /// Former of a hash map. #[ cfg( feature = "enabled" ) ] diff --git a/module/core/former/src/vector.rs b/module/core/former/src/vector.rs index 46426d8733..c7e2099e0a 100644 --- a/module/core/former/src/vector.rs +++ b/module/core/former/src/vector.rs @@ -1,4 +1,5 @@ use super::*; +use axiomatic::*; #[ allow( unused ) ] use collection_tools::Vec; @@ -10,7 +11,7 @@ use collection_tools::Vec; /// pub trait VectorLike< E > { - /// Appends an element to the back of a formed. + /// Appends an element to the back of a storage. fn push( &mut self, element : E ); } @@ -22,186 +23,201 @@ impl< E > VectorLike< E > for Vec< E > } } -/// A builder for constructing `VectorLike` containers, facilitating a fluent and flexible interface. -/// -/// `VectorSubformer` leverages the `VectorLike` trait to enable the construction and manipulation -/// of vector-like containers in a builder pattern style, promoting readability and ease of use. -/// -/// # Example -/// ```rust -/// #[ derive( Debug, PartialEq, former::Former ) ] -/// pub struct StructWithVec -/// { -/// #[ subformer( former::VectorSubformer ) ] -/// vec : Vec< &'static str >, -/// } -/// -/// let instance = StructWithVec::former() -/// .vec() -/// .push( "apple" ) -/// .push( "banana" ) -/// .end() -/// .form(); -/// -/// assert_eq!( instance, StructWithVec { vec: vec![ "apple", "banana" ] } ); -///``` -/// -#[ derive( Debug, Default ) ] -pub struct VectorSubformer< E, Formed, Context, ContainerEnd > -where - Formed : VectorLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, +#[ derive( Debug ) ] +pub struct VectorDefinition< E > { - formed : core::option::Option< Formed >, - context : core::option::Option< Context >, - on_end : core::option::Option< ContainerEnd >, _phantom : core::marker::PhantomData< E >, } -impl< E, Formed, Context, ContainerEnd > VectorSubformer< E, Formed, Context, ContainerEnd > -where - Formed : VectorLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, +impl< E > VectorDefinition< E > { - - /// Form current former into target structure. - #[ inline( always ) ] - pub fn form( mut self ) -> Formed - { - let formed = if self.formed.is_some() - { - self.formed.take().unwrap() - } - else - { - let val = Default::default(); - val - }; - formed - } - - // /// Initializes a new `VectorSubformer` instance, starting with an empty formed. - // /// This function serves as the entry point for the builder pattern. - // /// - // /// # Returns - // /// A new instance of `VectorSubformer` with an empty internal formed. - // /// - // #[ inline( always ) ] - // pub fn new() -> VectorSubformer< E, Formed, Formed, impl FormingEnd< Formed, Formed > > - // { - // VectorSubformer::begin - // ( - // None, - // None, - // crate::ReturnFormed, - // ) - // } - - /// Begins the building process, optionally initializing with a context and formed. - #[ inline( always ) ] - pub fn begin - ( - formed : core::option::Option< Formed >, - context : core::option::Option< Context >, - on_end : ContainerEnd - ) -> Self - { - Self - { - context, - formed, - on_end : Some( on_end ), - _phantom : core::marker::PhantomData, - } - } - - /// Finalizes the building process, returning the formed or a context incorporating it. - #[ inline( always ) ] - pub fn end( mut self ) -> Context - { - let on_end = self.on_end.take().unwrap(); - let context = self.context.take(); - let formed = self.form(); - on_end.call( formed, context ) - } - - /// Replaces the current formed with a provided one, allowing for a reset or redirection of the building process. - #[ inline( always ) ] - pub fn replace( mut self, vector : Formed ) -> Self + pub fn new() -> Self { - self.formed = Some( vector ); - self + Self { _phantom : core::marker::PhantomData } } - } -impl< E, Formed > VectorSubformer< E, Formed, Formed, crate::ReturnFormed > -where - Formed : VectorLike< E > + core::default::Default, +impl< E > FormerDefinition +for VectorDefinition< E > { - - /// Initializes a new `VectorSubformer` instance, starting with an empty formed. - /// This function serves as the entry point for the builder pattern. - /// - /// # Returns - /// A new instance of `VectorSubformer` with an empty internal formed. - /// - #[ inline( always ) ] - pub fn new() -> Self - { - Self::begin - ( - None, - None, - crate::ReturnFormed, - ) - } - + type Storage = Vec< E >; + type Formed = Vec< E >; } -impl< E, Formed, Context, ContainerEnd > VectorSubformer< E, Formed, Context, ContainerEnd > -where - Formed : VectorLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, +impl< E > Storage +for Vec< E > { + type Definition = VectorDefinition< E >; +} - /// Appends an element to the end of the formed, expanding the internal collection. - #[ inline( always ) ] - pub fn push< E2 >( mut self, element : E2 ) -> Self - where E2 : core::convert::Into< E >, +impl< E > StoragePerform +for Vec< E > +{ + fn preform( self ) -> < < Self as Storage >::Definition as FormerDefinition >::Formed { - if self.formed.is_none() - { - self.formed = core::option::Option::Some( Default::default() ); - } - if let core::option::Option::Some( ref mut formed ) = self.formed - { - formed.push( element.into() ); - } self } - } -// - -impl< E, Formed, Context, End > FormerBegin< Formed, Formed, Context > -for VectorSubformer< E, Formed, Context, End > -where - End : FormingEnd< Formed, Context >, - Formed : VectorLike< E > + Default, -{ - type End = End; - - #[ inline( always ) ] - fn _begin - ( - formed : core::option::Option< Formed >, - context : core::option::Option< Context >, - on_end : End, - ) -> Self - { - Self::begin( formed, context, on_end ) - } +/// A builder for constructing `VectorLike` containers, facilitating a fluent and flexible interface. +/// +/// `VectorSubformer` leverages the `VectorLike` trait to enable the construction and manipulation +/// of vector-like containers in a builder pattern style, promoting readability and ease of use. -} +pub type VectorSubformer< E > = ContainerSubformer::< E, VectorDefinition< E > >; + +// #[ derive( Debug, Default ) ] +// pub struct VectorSubformer< E, Definition, Context, End > +// where +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = E >, +// { +// storage : core::option::Option< Definition::Storage >, +// context : core::option::Option< Context >, +// on_end : core::option::Option< End >, +// } +// +// impl< E, Definition, Context, End > VectorSubformer< E, Definition, Context, End > +// where +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = E >, +// { +// +// /// Form current former into target structure. +// #[ inline( always ) ] +// pub fn storage( mut self ) -> Definition::Storage +// { +// let storage = if self.storage.is_some() +// { +// self.storage.take().unwrap() +// } +// else +// { +// let val = Default::default(); +// val +// }; +// storage +// } +// +// /// Begins the building process, optionally initializing with a context and storage. +// #[ inline( always ) ] +// pub fn begin +// ( +// storage : core::option::Option< Definition::Storage >, +// context : core::option::Option< Context >, +// on_end : End +// ) -> Self +// { +// Self +// { +// storage, +// context, +// on_end : Some( on_end ), +// } +// } +// +// /// Finalizes the building process, returning the formed or a context incorporating it. +// #[ inline( always ) ] +// pub fn form( self ) -> Definition::Formed +// { +// self.end() +// } +// +// /// Finalizes the building process, returning the formed or a context incorporating it. +// #[ inline( always ) ] +// pub fn end( mut self ) -> Definition::Formed +// { +// let on_end = self.on_end.take().unwrap(); +// let context = self.context.take(); +// let storage = self.storage(); +// on_end.call( storage, context ) +// } +// +// /// Replaces the current storage with a provided one, allowing for a reset or redirection of the building process. +// #[ inline( always ) ] +// pub fn replace( mut self, vector : Definition::Storage ) -> Self +// { +// self.storage = Some( vector ); +// self +// } +// +// } +// +// impl< E, Definition > VectorSubformer< E, Definition, (), ReturnFormed > +// where +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = E >, +// Definition::Storage : StoragePerform< Definition = Definition >, +// { +// +// /// Initializes a new `VectorSubformer` instance, starting with an empty formed. +// /// This function serves as the entry point for the builder pattern. +// /// +// /// # Returns +// /// A new instance of `VectorSubformer` with an empty internal formed. +// /// +// #[ inline( always ) ] +// pub fn new() -> Self +// { +// Self::begin +// ( +// None, +// None, +// ReturnFormed, +// ) +// } +// +// } +// +// impl< E, Definition, Context, End > VectorSubformer< E, Definition, Context, End > +// where +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = E >, +// { +// +// /// Appends an element to the end of the storage, expanding the internal collection. +// #[ inline( always ) ] +// pub fn push< IntoElement >( mut self, element : IntoElement ) -> Self +// where IntoElement : core::convert::Into< E >, +// { +// if self.storage.is_none() +// { +// self.storage = core::option::Option::Some( Default::default() ); +// } +// if let core::option::Option::Some( ref mut storage ) = self.storage +// { +// ContainerAdd::add( storage, element.into() ); +// // storage.push( element.into() ); +// } +// self +// } +// +// } +// +// // +// +// impl< E, Definition, Context, End > FormerBegin< Definition, Context > +// for VectorSubformer< E, Definition, Context, End > +// where +// End : FormingEnd< Definition, Context >, +// Definition : FormerDefinition, +// Definition::Storage : ContainerAdd< Element = E >, +// { +// type End = End; +// +// #[ inline( always ) ] +// fn _begin +// ( +// storage : core::option::Option< Definition::Storage >, +// context : core::option::Option< Context >, +// on_end : End, +// ) +// -> Self +// { +// Self::begin( storage, context, on_end ) +// } +// +// } diff --git a/module/core/former/src/vector3.rs b/module/core/former/src/vector3.rs deleted file mode 100644 index 51730a9ee9..0000000000 --- a/module/core/former/src/vector3.rs +++ /dev/null @@ -1,221 +0,0 @@ -use super::*; -use axiomatic::*; - -#[ allow( unused ) ] -use collection_tools::Vec; - -/// Trait for containers that behave like a vector, providing an interface for element addition. -/// -/// This trait enables the use of custom or standard vector-like containers within the builder pattern, -/// allowing for a unified and flexible approach to constructing collections. -/// -pub trait VectorLike< E > -{ - /// Appends an element to the back of a storage. - fn push( &mut self, element : E ); -} - -impl< E > VectorLike< E > for Vec< E > -{ - fn push( &mut self, element : E ) - { - Vec::push( self, element ); - } -} - -#[ derive( Debug ) ] -pub struct VectorDefinition< E > -{ - _phantom : core::marker::PhantomData< E >, -} - -impl< E > VectorDefinition< E > -{ - pub fn new() -> Self - { - Self { _phantom : core::marker::PhantomData } - } -} - -impl< E > FormerDefinition -for VectorDefinition< E > -{ - type Storage = Vec< E >; - type Formed = Vec< E >; -} - -impl< E > Storage -for Vec< E > -{ - type Definition = VectorDefinition< E >; -} - -impl< E > StoragePerform -for Vec< E > -{ - // type Definition = VectorDefinition< E >; - fn preform( self ) -> < < Self as Storage >::Definition as FormerDefinition >::Formed - { - self - } -} - -/// A builder for constructing `VectorLike` containers, facilitating a fluent and flexible interface. -/// -/// `VectorSubformer` leverages the `VectorLike` trait to enable the construction and manipulation -/// of vector-like containers in a builder pattern style, promoting readability and ease of use. -#[ derive( Debug, Default ) ] -pub struct VectorSubformer< E, Definition, Context, End > -where - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = E >, -{ - storage : core::option::Option< Definition::Storage >, - context : core::option::Option< Context >, - on_end : core::option::Option< End >, -} - -impl< E, Definition, Context, End > VectorSubformer< E, Definition, Context, End > -where - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = E >, -{ - - /// Form current former into target structure. - #[ inline( always ) ] - pub fn storage( mut self ) -> Definition::Storage - { - let storage = if self.storage.is_some() - { - self.storage.take().unwrap() - } - else - { - let val = Default::default(); - val - }; - storage - } - - /// Begins the building process, optionally initializing with a context and storage. - #[ inline( always ) ] - pub fn begin - ( - storage : core::option::Option< Definition::Storage >, - context : core::option::Option< Context >, - on_end : End - ) -> Self - { - Self - { - storage, - context, - on_end : Some( on_end ), - } - } - - /// Finalizes the building process, returning the formed or a context incorporating it. - #[ inline( always ) ] - pub fn form( self ) -> Definition::Formed - { - self.end() - } - - /// Finalizes the building process, returning the formed or a context incorporating it. - #[ inline( always ) ] - pub fn end( mut self ) -> Definition::Formed - { - let on_end = self.on_end.take().unwrap(); - let context = self.context.take(); - let storage = self.storage(); - on_end.call( storage, context ) - } - - /// Replaces the current storage with a provided one, allowing for a reset or redirection of the building process. - #[ inline( always ) ] - pub fn replace( mut self, vector : Definition::Storage ) -> Self - { - self.storage = Some( vector ); - self - } - -} - -impl< E, Definition > VectorSubformer< E, Definition, (), ReturnFormed > -where - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = E >, - Definition::Storage : StoragePerform< Definition = Definition >, -{ - - /// Initializes a new `VectorSubformer` instance, starting with an empty formed. - /// This function serves as the entry point for the builder pattern. - /// - /// # Returns - /// A new instance of `VectorSubformer` with an empty internal formed. - /// - #[ inline( always ) ] - pub fn new() -> Self - { - Self::begin - ( - None, - None, - ReturnFormed, - ) - } - -} - -impl< E, Definition, Context, End > VectorSubformer< E, Definition, Context, End > -where - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = E >, -{ - - /// Appends an element to the end of the storage, expanding the internal collection. - #[ inline( always ) ] - pub fn push< IntoElement >( mut self, element : IntoElement ) -> Self - where IntoElement : core::convert::Into< E >, - { - if self.storage.is_none() - { - self.storage = core::option::Option::Some( Default::default() ); - } - if let core::option::Option::Some( ref mut storage ) = self.storage - { - ContainerAdd::add( storage, element.into() ); - // storage.push( element.into() ); - } - self - } - -} - -// - -impl< E, Definition, Context, End > FormerBegin< Definition, Context > -for VectorSubformer< E, Definition, Context, End > -where - End : FormingEnd< Definition, Context >, - Definition : FormerDefinition, - Definition::Storage : ContainerAdd< Element = E >, -{ - type End = End; - - #[ inline( always ) ] - fn _begin - ( - storage : core::option::Option< Definition::Storage >, - context : core::option::Option< Context >, - on_end : End, - ) - -> Self - { - Self::begin( storage, context, on_end ) - } - -} diff --git a/module/core/former/tests/inc/former_tests/subformer_hashmap.rs b/module/core/former/tests/inc/former_tests/container_former_hashmap.rs similarity index 100% rename from module/core/former/tests/inc/former_tests/subformer_hashmap.rs rename to module/core/former/tests/inc/former_tests/container_former_hashmap.rs diff --git a/module/core/former/tests/inc/former_tests/subformer_hashset.rs b/module/core/former/tests/inc/former_tests/container_former_hashset.rs similarity index 100% rename from module/core/former/tests/inc/former_tests/subformer_hashset.rs rename to module/core/former/tests/inc/former_tests/container_former_hashset.rs diff --git a/module/core/former/tests/inc/former_tests/subformer_vec.rs b/module/core/former/tests/inc/former_tests/container_former_vec.rs similarity index 52% rename from module/core/former/tests/inc/former_tests/subformer_vec.rs rename to module/core/former/tests/inc/former_tests/container_former_vec.rs index 7a7244fece..3eccc1475f 100644 --- a/module/core/former/tests/inc/former_tests/subformer_vec.rs +++ b/module/core/former/tests/inc/former_tests/container_former_vec.rs @@ -8,9 +8,7 @@ use collection_tools::Vec; fn push() { - // - - let got : Vec< String > = the_module::VectorSubformer::new() + let got : Vec< String > = the_module::ContainerSubformer::< String, former::VectorDefinition< String > >::new() .push( "a" ) .push( "b" ) .form(); @@ -21,15 +19,11 @@ fn push() ]; a_id!( got, exp ); -} - -#[ test ] -fn replace() -{ + // let got : Vec< String > = the_module::VectorSubformer::new() - .push( "x" ) - .replace( vec![ "a".to_string(), "b".to_string() ] ) + .push( "a" ) + .push( "b" ) .form(); let exp = vec! [ @@ -39,3 +33,20 @@ fn replace() a_id!( got, exp ); } + +#[ test ] +fn replace() +{ + + // let got : Vec< String > = the_module::VectorSubformer::new() + // .push( "x" ) + // .replace( vec![ "a".to_string(), "b".to_string() ] ) + // .form(); + // let exp = vec! + // [ + // "a".to_string(), + // "b".to_string(), + // ]; + // a_id!( got, exp ); + +} diff --git a/module/core/former/tests/inc/mod.rs b/module/core/former/tests/inc/mod.rs index 21d986470d..d418d95951 100644 --- a/module/core/former/tests/inc/mod.rs +++ b/module/core/former/tests/inc/mod.rs @@ -7,8 +7,15 @@ mod former_tests #[ allow( unused_imports ) ] use super::*; - mod a_primitives_manual; - mod a_primitives; + #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] + mod container_former_vec; + // #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] + // mod container_former_hashset; + // #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] + // mod container_former_hashmap; + + // mod a_primitives_manual; + // mod a_primitives; // mod a_primitives_expanded; // mod a_containers_without_runtime_manual; // mod a_containers_without_runtime; @@ -48,13 +55,6 @@ mod former_tests // mod subformer_basic_manual; // #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] // mod subformer_basic; -// #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -// mod subformer_vec; -// #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -// mod subformer_hashset; -// #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -// mod subformer_hashmap; -// // // #[ cfg( any( not( feature = "no_std" ) ) ) ] // mod subformer_shortcut;