diff --git a/module/core/former/src/axiomatic2.rs b/module/core/former/src/axiomatic2.rs index 1415ab3ab0..16f43060ef 100644 --- a/module/core/former/src/axiomatic2.rs +++ b/module/core/former/src/axiomatic2.rs @@ -1,198 +1,207 @@ -// //! .... -// -// /// xxx2 -// pub trait StoragePerform -// { -// type Formed; -// fn preform( self ) -> Self::Formed; -// } -// -// /// xxx2 -// pub trait FormerDescriptor -// { -// type Storage : StoragePerform< Formed = Self::Formed >; -// type Formed; -// // type Former : FormerBegin2< Former : FormerDescriptor, 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 FormingEnd2< Former : FormerDescriptor, 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. -// // #[ allow( dead_code ) ] -// fn call( &self, storage : Former::Storage, context : core::option::Option< Context > ) -> Former::Formed; -// } -// -// impl< Former : FormerDescriptor, Context, F > FormingEnd2< Former, Context > for F +//! .... + +/// xxx2 +pub trait StoragePerform +{ + type Formed; + fn preform( self ) -> Self::Formed; +} + +/// xxx2 +pub trait FormerDescriptor +{ + type Storage : StoragePerform< Formed = Self::Formed >; + type Formed; + // type Former; +} + +pub trait FormerDefinition +{ + type Storage : StoragePerform< Formed = Self::Formed >; + type Formed; + type Context; + type FormerDescriptor : FormerDescriptor< Storage = Self::Storage, Formed = Self::Formed >; + type End : FormingEnd2< Self::FormerDescriptor, 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 FormingEnd2< Former : FormerDescriptor, 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. + // #[ allow( dead_code ) ] + fn call( &self, storage : Former::Storage, context : core::option::Option< Context > ) -> Former::Formed; +} + +impl< Former : FormerDescriptor, Context, F > FormingEnd2< Former, Context > for F +where + F : Fn( Former::Storage, core::option::Option< Context > ) -> Former::Formed, +{ + #[ inline( always ) ] + fn call( &self, storage : Former::Storage, context : core::option::Option< Context > ) -> Former::Formed + { + self( storage, context ) + } +} + +/// A `FormingEnd2` 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 ReturnStorage2; + +impl< Former : FormerDescriptor > FormingEnd2< Former, () > +for ReturnStorage2 // where -// F : Fn( Former::Storage, core::option::Option< Context > ) -> Former::Formed, -// { -// #[ inline( always ) ] -// fn call( &self, storage : Former::Storage, context : core::option::Option< Context > ) -> Former::Formed -// { -// self( storage, context ) -// } -// } -// -// /// A `FormingEnd2` 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 ReturnStorage2; -// -// impl< Former : FormerDescriptor > FormingEnd2< Former, () > -// for ReturnStorage2 -// // where -// // Storage : StoragePreform<>, -// { -// #[ inline( always ) ] -// fn call( &self, storage : Former::Storage, _context : core::option::Option< () > ) -> Former::Formed -// { -// storage.preform() -// } -// } -// -// /// A wrapper around a closure to be used as a `FormingEnd2`. -// /// -// /// This struct allows for dynamic dispatch of a closure that matches the -// /// `FormingEnd2` trait's `call` method signature. It is useful for cases where -// /// a closure needs to be stored or passed around as an object implementing -// /// `FormingEnd2`. -// /// -// /// # 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 FormingEndWrapper2< Former : FormerDescriptor, Context > -// { -// closure : Box< dyn Fn( Former::Storage, Option< Context > ) -> Former::Formed >, -// _marker : std::marker::PhantomData< Former::Storage >, -// } -// -// #[ cfg( not( feature = "no_std" ) ) ] -// impl< Former : FormerDescriptor, Context > FormingEndWrapper2< Former, Context > -// { -// /// Constructs a new `FormingEndWrapper2` 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 `FormingEnd2` trait implementation. -// /// -// /// # Returns -// /// -// /// Returns an instance of `FormingEndWrapper2` encapsulating the provided closure. -// pub fn new( closure : impl Fn( Former::Storage, Option< Context > ) -> Former::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< Former : FormerDescriptor, Context > fmt::Debug for FormingEndWrapper2< Former, Context > -// { -// fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result -// { -// f.debug_struct( "FormingEndWrapper2" ) -// .field( "closure", &format_args!{ "- closure -" } ) -// .field( "_marker", &self._marker ) -// .finish() -// } -// } -// -// #[ cfg( not( feature = "no_std" ) ) ] -// impl< Former : FormerDescriptor, Context > FormingEnd2< Former, Context > -// for FormingEndWrapper2< Former, Context > -// { -// fn call( &self, storage : Former::Storage, context : Option< Context > ) -> Former::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 `FormerBegin2` 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. -// -// // xxx2 : change sequence -// pub trait FormerBegin2< Former : FormerDescriptor, 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 `FormingEnd2` trait, defining the precise mechanics of -// /// how the subformer concludes its operation. -// type End : FormingEnd2< Former, 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< Former::Storage >, -// context : core::option::Option< Context >, -// on_end : Self::End, -// ) -> Self; -// -// } + // Storage : StoragePreform<>, +{ + #[ inline( always ) ] + fn call( &self, storage : Former::Storage, _context : core::option::Option< () > ) -> Former::Formed + { + storage.preform() + } +} + +/// A wrapper around a closure to be used as a `FormingEnd2`. +/// +/// This struct allows for dynamic dispatch of a closure that matches the +/// `FormingEnd2` trait's `call` method signature. It is useful for cases where +/// a closure needs to be stored or passed around as an object implementing +/// `FormingEnd2`. +/// +/// # 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 FormingEndWrapper2< Former : FormerDescriptor, Context > +{ + closure : Box< dyn Fn( Former::Storage, Option< Context > ) -> Former::Formed >, + _marker : std::marker::PhantomData< Former::Storage >, +} + +#[ cfg( not( feature = "no_std" ) ) ] +impl< Former : FormerDescriptor, Context > FormingEndWrapper2< Former, Context > +{ + /// Constructs a new `FormingEndWrapper2` 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 `FormingEnd2` trait implementation. + /// + /// # Returns + /// + /// Returns an instance of `FormingEndWrapper2` encapsulating the provided closure. + pub fn new( closure : impl Fn( Former::Storage, Option< Context > ) -> Former::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< Former : FormerDescriptor, Context > fmt::Debug for FormingEndWrapper2< Former, Context > +{ + fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result + { + f.debug_struct( "FormingEndWrapper2" ) + .field( "closure", &format_args!{ "- closure -" } ) + .field( "_marker", &self._marker ) + .finish() + } +} + +#[ cfg( not( feature = "no_std" ) ) ] +impl< Former : FormerDescriptor, Context > FormingEnd2< Former, Context > +for FormingEndWrapper2< Former, Context > +{ + fn call( &self, storage : Former::Storage, context : Option< Context > ) -> Former::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 `FormerBegin2` 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. + +// xxx2 : change sequence +pub trait FormerBegin2< Former : FormerDescriptor, 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 `FormingEnd2` trait, defining the precise mechanics of + /// how the subformer concludes its operation. + type End : FormingEnd2< Former, 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< Former::Storage >, + context : core::option::Option< Context >, + on_end : Self::End, + ) -> Self; + +} diff --git a/module/core/former/src/lib.rs b/module/core/former/src/lib.rs index 5908b42b5b..c3fdf25c49 100644 --- a/module/core/former/src/lib.rs +++ b/module/core/former/src/lib.rs @@ -37,8 +37,9 @@ mod hash_set; #[ cfg( any( feature = "derive_component_from", feature = "derive_component_assign" ) ) ] mod component; -mod axiomatic2; -mod vector2; +// mod axiomatic2; +// mod vector2; +// mod vector3; /// Namespace with dependencies. #[ cfg( feature = "enabled" ) ] diff --git a/module/core/former/src/vector2.rs b/module/core/former/src/vector2.rs index da374b4a2e..9c7e17beb5 100644 --- a/module/core/former/src/vector2.rs +++ b/module/core/former/src/vector2.rs @@ -1,218 +1,195 @@ -// use super::*; -// use axiomatic2::*; -// -// #[ 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 VectorLike2< E > -// { -// /// Appends an element to the back of a formed. -// fn push( &mut self, element : E ); -// } -// -// impl< E > VectorLike2< E > for Vec< E > -// { -// fn push( &mut self, element : E ) -// { -// Vec::push( self, element ); -// } -// } -// -// impl< E > StoragePerform for Vec< E > -// { -// type Formed = Self; -// fn preform( self ) -> Self::Formed -// { -// self -// } -// } -// -// pub struct VectorSubformerDescriptor< E > -// { -// _phantom : core::marker::PhantomData< E >, -// } -// -// impl< E > VectorSubformerDescriptor< E > -// { -// fn new() -> Self -// { -// Self { _phantom : core::marker::PhantomData } -// } -// } -// -// impl< E > FormerDescriptor -// // where -// // End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, -// for VectorSubformerDescriptor< E > -// { -// type Storage = Vec< E >; -// type Formed = Vec< E >; -// // type Former = VectorSubformer2< E, Context, End >; -// } -// -// /// A builder for constructing `VectorLike2` containers, facilitating a fluent and flexible interface. -// /// -// /// `VectorSubformer2` leverages the `VectorLike2` 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::Former ) ] -// /// 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 VectorSubformer2< E, Context, End > -// where -// End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, -// { -// formed : core::option::Option< < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed >, -// context : core::option::Option< Context >, -// on_end : core::option::Option< End >, -// } -// -// impl< E, Context, End > VectorSubformer2< E, Context, End > +use super::*; +use axiomatic2::*; + +#[ 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 VectorLike2< E > +{ + /// Appends an element to the back of a formed. + fn push( &mut self, element : E ); +} + +impl< E > VectorLike2< E > for Vec< E > +{ + fn push( &mut self, element : E ) + { + Vec::push( self, element ); + } +} + +impl< E > StoragePerform for Vec< E > +{ + type Formed = Self; + fn preform( self ) -> Self::Formed + { + self + } +} +pub struct VectorSubformerDescriptor< E > +{ + _phantom : core::marker::PhantomData< E >, +} + +impl< E > VectorSubformerDescriptor< E > +{ + fn new() -> Self + { + Self { _phantom : core::marker::PhantomData } + } +} + +impl< E > FormerDescriptor +for VectorSubformerDescriptor< E > +{ + type Storage = Vec< E >; + type Formed = Vec< E >; + // type Former = VectorSubformer2< E, Context, End >; +} + +/// A builder for constructing `VectorLike2` containers, facilitating a fluent and flexible interface. +/// +/// `VectorSubformer2` leverages the `VectorLike2` 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 VectorSubformer2< E, Context, End > +where + End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, +{ + formed : core::option::Option< < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed >, + context : core::option::Option< Context >, + on_end : core::option::Option< End >, +} + +impl< E, Context, End > VectorSubformer2< E, Context, End > +where + End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, +{ + + /// Form current former into target structure. + #[ inline( always ) ] + pub fn form( mut self ) -> < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed + { + let formed = if self.formed.is_some() + { + self.formed.take().unwrap() + } + else + { + let val = Default::default(); + val + }; + formed + } + + /// Begins the building process, optionally initializing with a context and formed. + #[ inline( always ) ] + pub fn begin + ( + formed : core::option::Option< < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed >, + context : core::option::Option< Context >, + on_end : End + ) -> Self + { + Self + { + context, + formed, + on_end : Some( on_end ), + } + } + + /// Finalizes the building process, returning the formed or a context incorporating it. + #[ inline( always ) ] + pub fn end( mut self ) -> < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed + { + 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 : < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed ) -> Self + { + self.formed = Some( vector ); + self + } + +} + +impl< E > VectorSubformer2< E, (), ReturnStorage2 > +where +{ + + /// Initializes a new `VectorSubformer2` instance, starting with an empty formed. + /// This function serves as the entry point for the builder pattern. + /// + /// # Returns + /// A new instance of `VectorSubformer2` with an empty internal formed. + /// + #[ inline( always ) ] + pub fn new() -> Self + { + Self::begin + ( + None, + None, + ReturnStorage2, + ) + } + +} + +impl< E, Context, End > VectorSubformer2< E, Context, End > +where + End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, +{ + + /// 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 >, + { + 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< Former, Context, End > FormerBegin< Formed, Formed, Context > +// for VectorSubformer2< Former, Context, End > // where // End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, +// // Formed : VectorLike2< E > + Default, +// Former : FormerDescriptor, // { +// type End = End; // -// /// Form current former into target structure. -// #[ inline( always ) ] -// pub fn form( mut self ) -> < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed -// { -// let formed = if self.formed.is_some() -// { -// self.formed.take().unwrap() -// } -// else -// { -// let val = Default::default(); -// val -// }; -// formed -// } -// -// /// Begins the building process, optionally initializing with a context and formed. // #[ inline( always ) ] -// pub fn begin +// fn _begin // ( -// formed : core::option::Option< < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed >, +// formed : core::option::Option< Formed >, // context : core::option::Option< Context >, -// on_end : End +// on_end : End, // ) -> Self // { -// Self -// { -// context, -// formed, -// on_end : Some( on_end ), -// } -// } -// -// /// Finalizes the building process, returning the formed or a context incorporating it. -// #[ inline( always ) ] -// pub fn end( mut self ) -> < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed -// { -// 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 : < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed ) -> Self -// { -// self.formed = Some( vector ); -// self -// } -// -// } -// -// impl< E > VectorSubformer2< E, (), ReturnStorage2 > -// where -// { -// -// /// Initializes a new `VectorSubformer2` instance, starting with an empty formed. -// /// This function serves as the entry point for the builder pattern. -// /// -// /// # Returns -// /// A new instance of `VectorSubformer2` with an empty internal formed. -// /// -// #[ inline( always ) ] -// pub fn new() -> Self -// { -// Self::begin -// ( -// None, -// None, -// ReturnStorage2, -// ) -// } -// -// } -// -// impl< E, Context, End > VectorSubformer2< E, Context, End > -// where -// End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, -// { -// -// /// 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 >, -// { -// 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 +// Self::begin( formed, context, on_end ) // } // // } -// -// // -// -// // impl< Former, Context, End > FormerBegin< Formed, Formed, Context > -// // for VectorSubformer2< Former, Context, End > -// // where -// // End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, -// // // Formed : VectorLike2< E > + Default, -// // Former : FormerDescriptor, -// // { -// // 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 ) -// // } -// // -// // } diff --git a/module/core/former/src/vector3.rs b/module/core/former/src/vector3.rs new file mode 100644 index 0000000000..4df4958ce3 --- /dev/null +++ b/module/core/former/src/vector3.rs @@ -0,0 +1,196 @@ +use super::*; +use axiomatic2::*; + +#[ 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 VectorLike2< E > +{ + /// Appends an element to the back of a formed. + fn push( &mut self, element : E ); +} + +impl< E > VectorLike2< E > for Vec< E > +{ + fn push( &mut self, element : E ) + { + Vec::push( self, element ); + } +} + +impl< E > StoragePerform for Vec< E > +{ + type Formed = Self; + fn preform( self ) -> Self::Formed + { + self + } +} +pub struct VectorSubformerDescriptor< E > +{ + _phantom : core::marker::PhantomData< E >, +} + +impl< E > VectorSubformerDescriptor< E > +{ + fn new() -> Self + { + Self { _phantom : core::marker::PhantomData } + } +} + +impl< E > FormerDescriptor +for VectorSubformerDescriptor< E > +{ + type Storage = Vec< E >; + type Formed = Vec< E >; + // type Former = VectorSubformer2< E, Context, End >; +} + +/// A builder for constructing `VectorLike2` containers, facilitating a fluent and flexible interface. +/// +/// `VectorSubformer2` leverages the `VectorLike2` 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 VectorSubformer2< E, Context, End > +where + End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, +{ + formed : core::option::Option< < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed >, + context : core::option::Option< Context >, + on_end : core::option::Option< End >, +} + +impl< E, Context, End > VectorSubformer2< E, Context, End > +where + End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, +{ + + /// Form current former into target structure. + #[ inline( always ) ] + pub fn form( mut self ) -> < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed + { + let formed = if self.formed.is_some() + { + self.formed.take().unwrap() + } + else + { + let val = Default::default(); + val + }; + formed + } + + /// Begins the building process, optionally initializing with a context and formed. + #[ inline( always ) ] + pub fn begin + ( + formed : core::option::Option< < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed >, + context : core::option::Option< Context >, + on_end : End + ) -> Self + { + Self + { + context, + formed, + on_end : Some( on_end ), + } + } + + /// Finalizes the building process, returning the formed or a context incorporating it. + #[ inline( always ) ] + pub fn end( mut self ) -> < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed + { + 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 : < VectorSubformerDescriptor< E > as axiomatic2::FormerDescriptor >::Formed ) -> Self + { + self.formed = Some( vector ); + self + } + +} + +impl< E > VectorSubformer2< E, (), ReturnStorage2 > +where +{ + + /// Initializes a new `VectorSubformer2` instance, starting with an empty formed. + /// This function serves as the entry point for the builder pattern. + /// + /// # Returns + /// A new instance of `VectorSubformer2` with an empty internal formed. + /// + #[ inline( always ) ] + pub fn new() -> Self + { + Self::begin + ( + None, + None, + ReturnStorage2, + ) + } + +} + +impl< E, Context, End > VectorSubformer2< E, Context, End > +where + End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, +{ + + /// 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 >, + { + 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< Former, Context, End > FormerBegin< Formed, Formed, Context > +// for VectorSubformer2< Former, Context, End > +// where +// End : FormingEnd2< VectorSubformerDescriptor< E >, Context >, +// // Formed : VectorLike2< E > + Default, +// Former : FormerDescriptor, +// { +// 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 ) +// } +// +// }