From aa64c2d1cffbbaa02103389cf1268c3e09c6784a Mon Sep 17 00:00:00 2001 From: wandalen Date: Sat, 23 Mar 2024 22:53:48 +0200 Subject: [PATCH] former : experimenting --- module/core/former/src/axiomatic.rs | 20 +++++- module/core/former/src/container.rs | 33 ++++----- module/core/former/src/hash_map.rs | 20 +++--- module/core/former/src/hash_set.rs | 20 +++--- module/core/former/src/vector.rs | 19 +++-- .../inc/former_tests/container_former_vec.rs | 70 ++++++++++--------- 6 files changed, 109 insertions(+), 73 deletions(-) diff --git a/module/core/former/src/axiomatic.rs b/module/core/former/src/axiomatic.rs index 084ce87db4..241383491c 100644 --- a/module/core/former/src/axiomatic.rs +++ b/module/core/former/src/axiomatic.rs @@ -14,12 +14,13 @@ pub trait StoragePerform : Storage } /// xxx -pub trait FormerDefinition +pub trait FormerDefinition : Sized { // type Storage : Storage< Definition = Self >; type Storage : Storage< Formed = Self::Formed >; type Formed; type Context; + type End : FormingEnd< Self >; } // pub trait FormerDefinition @@ -74,7 +75,6 @@ pub struct ReturnFormed; impl< Definition > FormingEnd< Definition > for ReturnFormed where - // Definition::Storage : StoragePerform< Definition = Definition >, Definition::Storage : StoragePerform< Formed = Definition::Formed >, Definition : FormerDefinition< Context = () >, { @@ -85,6 +85,22 @@ where } } +/// xxx +#[ derive( Debug, Default ) ] +pub struct ReturnStorage; + +impl< Definition, T > FormingEnd< Definition > +for ReturnStorage +where + Definition : FormerDefinition< Context = (), Storage = T, Formed = T >, +{ + #[ inline( always ) ] + fn call( &self, storage : Definition::Storage, _context : core::option::Option< () > ) -> Definition::Formed + { + storage + } +} + /// A wrapper around a closure to be used as a `FormingEnd`. /// /// This struct allows for dynamic dispatch of a closure that matches the diff --git a/module/core/former/src/container.rs b/module/core/former/src/container.rs index a14882d04b..a7156befe3 100644 --- a/module/core/former/src/container.rs +++ b/module/core/former/src/container.rs @@ -195,20 +195,20 @@ where /// A builder for constructing containers, facilitating a fluent and flexible interface. #[ derive( Debug, Default ) ] -pub struct ContainerSubformer< E, Definition, End = ReturnFormed > +pub struct ContainerSubformer< E, Definition > where - End : FormingEnd< Definition >, + // End : FormingEnd< Definition >, Definition : FormerDefinition, Definition::Storage : ContainerAdd< Element = E >, { storage : core::option::Option< Definition::Storage >, context : core::option::Option< Definition::Context >, - on_end : core::option::Option< End >, + on_end : core::option::Option< Definition::End >, } -impl< E, Definition, End > ContainerSubformer< E, Definition, End > +impl< E, Definition > ContainerSubformer< E, Definition > where - End : FormingEnd< Definition >, + // End : FormingEnd< Definition >, Definition : FormerDefinition, Definition::Storage : ContainerAdd< Element = E >, { @@ -235,7 +235,7 @@ where ( storage : core::option::Option< Definition::Storage >, context : core::option::Option< Definition::Context >, - on_end : End + on_end : Definition::End, ) -> Self { Self @@ -273,9 +273,9 @@ where } -impl< E, Definition > ContainerSubformer< E, Definition, ReturnFormed > +impl< E, T, Definition > ContainerSubformer< E, Definition > where - Definition : FormerDefinition< Context = () >, + Definition : FormerDefinition< Context = (), Storage = T, Formed = T, End = ReturnStorage >, Definition::Storage : ContainerAdd< Element = E >, Definition::Storage : StoragePerform< Formed = Definition::Formed >, { @@ -293,15 +293,16 @@ where ( None, None, - ReturnFormed, + ReturnStorage, + // ReturnFormed, ) } } -impl< E, Definition, End > ContainerSubformer< E, Definition, End > +impl< E, Definition > ContainerSubformer< E, Definition > where - End : FormingEnd< Definition >, + // End : FormingEnd< Definition >, Definition : FormerDefinition, Definition::Storage : ContainerAdd< Element = E >, { @@ -327,21 +328,21 @@ where // -impl< E, Definition, End > FormerBegin< Definition > -for ContainerSubformer< E, Definition, End > +impl< E, Definition > FormerBegin< Definition > +for ContainerSubformer< E, Definition > where - End : FormingEnd< Definition >, + // End : FormingEnd< Definition >, Definition : FormerDefinition, Definition::Storage : ContainerAdd< Element = E >, { - type End = End; + type End = Definition::End; #[ inline( always ) ] fn _begin ( storage : core::option::Option< Definition::Storage >, context : core::option::Option< Definition::Context >, - on_end : End, + on_end : Definition::End, ) -> Self { diff --git a/module/core/former/src/hash_map.rs b/module/core/former/src/hash_map.rs index 3788eecf67..b2ea774f13 100644 --- a/module/core/former/src/hash_map.rs +++ b/module/core/former/src/hash_map.rs @@ -45,7 +45,7 @@ where } -// +// = storage impl< K, E > Storage for HashMap< K, E > @@ -68,19 +68,21 @@ where } } -// +// = definition #[ derive( Debug ) ] -pub struct HashMapDefinition< K, E, Context = () > +pub struct HashMapDefinition< K, E, Context, End > where K : ::core::cmp::Eq + ::core::hash::Hash, + End : FormingEnd< Self >, { - _phantom : ::core::marker::PhantomData< ( K, E, Context ) >, + _phantom : ::core::marker::PhantomData< ( K, E, Context, End ) >, } -impl< K, E, Context > HashMapDefinition< K, E, Context > +impl< K, E, Context, End > HashMapDefinition< K, E, Context, End > where K : ::core::cmp::Eq + ::core::hash::Hash, + End : FormingEnd< Self >, { pub fn new() -> Self { @@ -88,14 +90,16 @@ where } } -impl< K, E, Context > FormerDefinition -for HashMapDefinition< K, E, Context > +impl< K, E, Context, End > FormerDefinition +for HashMapDefinition< K, E, Context, End > where K : ::core::cmp::Eq + ::core::hash::Hash, + End : FormingEnd< Self >, { type Storage = HashMap< K, E >; type Formed = HashMap< K, E >; type Context = Context; + type End = End; } /// A builder for constructing hash map-like structures with a fluent interface. @@ -136,7 +140,7 @@ where /// # } /// ``` -pub type HashMapSubformer< K, E, Context = () > = ContainerSubformer::< ( K, E ), HashMapDefinition< K, E, Context > >; +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 > diff --git a/module/core/former/src/hash_set.rs b/module/core/former/src/hash_set.rs index 9d8836b6d3..4be2c7bbf5 100644 --- a/module/core/former/src/hash_set.rs +++ b/module/core/former/src/hash_set.rs @@ -33,7 +33,7 @@ where } } -// +// = storage impl< K > Storage for HashSet< K > @@ -56,19 +56,21 @@ where } } -// +// = definition #[ derive( Debug ) ] -pub struct HashSetDefinition< K, Context = () > +pub struct HashSetDefinition< K, Context, End > where K : ::core::cmp::Eq + ::core::hash::Hash, + End : FormingEnd< Self >, { - _phantom : ::core::marker::PhantomData< ( K, Context ) >, + _phantom : ::core::marker::PhantomData< ( K, Context, End ) >, } -impl< K, Context > HashSetDefinition< K, Context > +impl< K, Context, End > HashSetDefinition< K, Context, End > where K : ::core::cmp::Eq + ::core::hash::Hash, + End : FormingEnd< Self >, { pub fn new() -> Self { @@ -76,14 +78,16 @@ where } } -impl< K, Context > FormerDefinition -for HashSetDefinition< K, Context > +impl< K, Context, End > FormerDefinition +for HashSetDefinition< K, Context, End > where K : ::core::cmp::Eq + ::core::hash::Hash, + End : FormingEnd< Self >, { type Storage = HashSet< K >; type Formed = HashSet< K >; type Context = Context; + type End = End; } /// Facilitates building `HashSetLike` containers with a fluent API. @@ -118,7 +122,7 @@ where /// # } /// ``` -pub type HashSetSubformer< K, Context = () > = ContainerSubformer::< K, HashSetDefinition< K, Context > >; +pub type HashSetSubformer< K, Context, End > = ContainerSubformer::< K, HashSetDefinition< K, Context, End > >; // #[ derive( Debug, Default ) ] // pub struct HashSetSubformer< K, Definition, Context, End > diff --git a/module/core/former/src/vector.rs b/module/core/former/src/vector.rs index bab7601563..d35824cb55 100644 --- a/module/core/former/src/vector.rs +++ b/module/core/former/src/vector.rs @@ -45,12 +45,16 @@ for Vec< E > // #[ derive( Debug ) ] -pub struct VectorDefinition< E, Context = () > +pub struct VectorDefinition< E, Context, End > +where + End : FormingEnd< Self > { - _phantom : core::marker::PhantomData< ( E, Context ) >, + _phantom : core::marker::PhantomData< ( E, Context, End ) >, } -impl< E, Context > VectorDefinition< E, Context > +impl< E, Context, End > VectorDefinition< E, Context, End > +where + End : FormingEnd< Self > { pub fn new() -> Self { @@ -58,12 +62,15 @@ impl< E, Context > VectorDefinition< E, Context > } } -impl< E, Context > FormerDefinition -for VectorDefinition< E, Context > +impl< E, Context, End > FormerDefinition +for VectorDefinition< E, Context, End > +where + End : FormingEnd< Self > { type Storage = Vec< E >; type Formed = Vec< E >; type Context = Context; + type End = End; } /// A builder for constructing `VectorLike` containers, facilitating a fluent and flexible interface. @@ -71,7 +78,7 @@ for VectorDefinition< E, Context > /// `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, Context = () > = ContainerSubformer::< E, VectorDefinition< E, Context > >; +pub type VectorSubformer< E, Context, End > = ContainerSubformer::< E, VectorDefinition< E, Context, End > >; // #[ derive( Debug, Default ) ] // pub struct VectorSubformer< E, Definition, Context, 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 4f886ba463..09fce459a0 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 @@ -8,42 +8,46 @@ use collection_tools::Vec; fn push() { - 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 ); - - // - - 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 ); + // 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 ); // - let got : Vec< String > = the_module::VectorSubformer::< String, () >::new() - .push( "a" ) - .push( "b" ) - .form(); - let exp = vec! - [ - "a".to_string(), - "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 ); +// +// // +// +// let got : Vec< String > = the_module::VectorSubformer::< String, () >::new() +// .push( "a" ) +// .push( "b" ) +// .form(); +// let exp = vec! +// [ +// "a".to_string(), +// "b".to_string(), +// ]; +// a_id!( got, exp ); }