From bfb321595a2acb3ae7499be423e0723e2fd2f437 Mon Sep 17 00:00:00 2001 From: wandalen Date: Sun, 24 Mar 2024 00:00:23 +0200 Subject: [PATCH] former : experimenting --- module/core/former/src/axiomatic.rs | 40 ++-- module/core/former/src/hash_map.rs | 177 ---------------- module/core/former/src/hash_set.rs | 196 ------------------ module/core/former/src/vector.rs | 158 +------------- .../inc/former_tests/container_former_vec.rs | 44 ++-- 5 files changed, 52 insertions(+), 563 deletions(-) diff --git a/module/core/former/src/axiomatic.rs b/module/core/former/src/axiomatic.rs index 241383491c..4066ae3661 100644 --- a/module/core/former/src/axiomatic.rs +++ b/module/core/former/src/axiomatic.rs @@ -65,25 +65,25 @@ where } } -/// 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 > FormingEnd< Definition > -for ReturnFormed -where - Definition::Storage : StoragePerform< Formed = Definition::Formed >, - Definition : FormerDefinition< Context = () >, -{ - #[ inline( always ) ] - fn call( &self, storage : Definition::Storage, _context : core::option::Option< () > ) -> Definition::Formed - { - storage.preform() - } -} +// /// 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 > FormingEnd< Definition > +// for ReturnFormed +// where +// Definition::Storage : StoragePerform< Formed = Definition::Formed >, +// Definition : FormerDefinition< Context = () >, +// { +// #[ inline( always ) ] +// fn call( &self, storage : Definition::Storage, _context : core::option::Option< () > ) -> Definition::Formed +// { +// storage.preform() +// } +// } /// xxx #[ derive( Debug, Default ) ] @@ -92,7 +92,7 @@ pub struct ReturnStorage; impl< Definition, T > FormingEnd< Definition > for ReturnStorage where - Definition : FormerDefinition< Context = (), Storage = T, Formed = T >, + Definition : FormerDefinition< Context = (), Storage = T, Formed = T, End = Self >, { #[ inline( always ) ] fn call( &self, storage : Definition::Storage, _context : core::option::Option< () > ) -> Definition::Formed diff --git a/module/core/former/src/hash_map.rs b/module/core/former/src/hash_map.rs index b2ea774f13..e3134e65ee 100644 --- a/module/core/former/src/hash_map.rs +++ b/module/core/former/src/hash_map.rs @@ -141,180 +141,3 @@ where /// ``` pub type HashMapSubformer< K, E, Context, End > = ContainerSubformer::< ( K, E ), HashMapDefinition< K, E, Context, End > >; - -// #[ 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 4be2c7bbf5..1f9c63836e 100644 --- a/module/core/former/src/hash_set.rs +++ b/module/core/former/src/hash_set.rs @@ -123,199 +123,3 @@ where /// ``` pub type HashSetSubformer< K, Context, End > = ContainerSubformer::< K, HashSetDefinition< K, Context, End > >; - -// #[ 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/vector.rs b/module/core/former/src/vector.rs index d35824cb55..cc3ba6dbac 100644 --- a/module/core/former/src/vector.rs +++ b/module/core/former/src/vector.rs @@ -45,7 +45,7 @@ for Vec< E > // #[ derive( Debug ) ] -pub struct VectorDefinition< E, Context, End > +pub struct VectorDefinition< E, Context = (), End = ReturnStorage > where End : FormingEnd< Self > { @@ -79,159 +79,3 @@ where /// of vector-like containers in a builder pattern style, promoting readability and ease of use. pub type VectorSubformer< E, Context, End > = ContainerSubformer::< E, VectorDefinition< E, Context, End > >; - -// #[ 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/container_former_vec.rs b/module/core/former/tests/inc/former_tests/container_former_vec.rs index 09fce459a0..68e27f6ca8 100644 --- a/module/core/former/tests/inc/former_tests/container_former_vec.rs +++ b/module/core/former/tests/inc/former_tests/container_former_vec.rs @@ -4,10 +4,41 @@ use super::*; #[ allow( unused_imports ) ] use collection_tools::Vec; +// impl< Definition, T > FormingEnd< Definition > +// for ReturnStorage +// where +// Definition : FormerDefinition< Context = (), Storage = T, Formed = T, End = Self >, + +pub fn f1< Definition : former::FormerDefinition >( x : Definition ) +{ +} + #[ test ] fn push() { + // f1( the_module::ReturnStorage ); + + // + + // let got : Vec< String > = the_module + // ::ContainerSubformer + // ::< String, former::VectorDefinition< String, (), the_module::ReturnStorage > > + // ::new() + // .push( "a" ) + // .push( "b" ) + // .form(); + // let exp = vec! + // [ + // "a".to_string(), + // "b".to_string(), + // ]; + // a_id!( got, exp ); + + // Definition : FormerDefinition< Context = (), Storage = T, Formed = T, End = Self >, + + // + // let got : Vec< String > = the_module::ContainerSubformer:: // < // String, @@ -22,19 +53,6 @@ fn push() // "b".to_string(), // ]; // a_id!( got, exp ); - - // - -// let got : Vec< String > = the_module::ContainerSubformer::< String, former::VectorDefinition< String > >::new() -// .push( "a" ) -// .push( "b" ) -// .form(); -// let exp = vec! -// [ -// "a".to_string(), -// "b".to_string(), -// ]; -// a_id!( got, exp ); // // // //