From e10f6a1e676160fc7630b14236540bdb7cea304f Mon Sep 17 00:00:00 2001 From: wandalen Date: Sat, 23 Mar 2024 17:41:28 +0200 Subject: [PATCH] former : experimenting --- .../tests/inc/former_tests/a_primitives.rs | 228 +----------------- .../inc/former_tests/a_primitives_expanded.rs | 4 +- .../inc/former_tests/a_primitives_manual.rs | 177 +++++++------- .../a_primitives_manual_original.rs | 192 --------------- .../inc/former_tests/only_test/primitives.rs | 19 +- module/core/former/tests/inc/mod.rs | 4 +- 6 files changed, 112 insertions(+), 512 deletions(-) delete mode 100644 module/core/former/tests/inc/former_tests/a_primitives_manual_original.rs diff --git a/module/core/former/tests/inc/former_tests/a_primitives.rs b/module/core/former/tests/inc/former_tests/a_primitives.rs index 7a63846439..c39429b63a 100644 --- a/module/core/former/tests/inc/former_tests/a_primitives.rs +++ b/module/core/former/tests/inc/former_tests/a_primitives.rs @@ -12,230 +12,4 @@ pub struct Struct1 // -// // generated by former -// impl Struct1 -// { -// pub fn former() -> Struct1Former< (), the_module::ReturnStorage > -// { -// Struct1Former::new() -// } -// } -// -// // -// -// // generated by former -// pub struct Struct1FormerStorage -// { -// pub int_1 : core::option::Option< i32 >, -// pub string_1 : core::option::Option< String >, -// pub int_optional_1 : core::option::Option< i32 >, -// pub string_optional_1 : core::option::Option< String >, -// } -// -// impl Default for Struct1FormerStorage -// { -// -// #[ inline( always ) ] -// fn default() -> Self -// { -// Self -// { -// int_1 : core::option::Option::None, -// string_1 : core::option::Option::None, -// int_optional_1 : core::option::Option::None, -// string_optional_1 : core::option::Option::None, -// } -// } -// -// } -// -// #[ derive( Debug ) ] -// pub struct Struct1FormerDescriptor; -// -// impl Struct1FormerDescriptor -// { -// pub fn new() -> Self -// { -// Self -// } -// } -// -// impl former::FormerDescriptor -// for Struct1FormerDescriptor -// { -// type Storage = Struct1FormerStorage; -// type Formed = Struct1; -// } -// -// impl former::Storage -// for Struct1FormerStorage -// { -// type Descriptor = Struct1FormerDescriptor; -// } -// -// impl former::StoragePerform -// for Struct1FormerStorage -// { -// -// fn preform( mut self ) -> < < Self as former::Storage >::Descriptor as former::FormerDescriptor >::Formed -// { -// -// let int_1 = if self.int_1.is_some() -// { -// self.int_1.take().unwrap() -// } -// else -// { -// let val : i32 = Default::default(); -// val -// }; -// -// let string_1 = if self.string_1.is_some() -// { -// self.string_1.take().unwrap() -// } -// else -// { -// let val : String = Default::default(); -// val -// }; -// -// let int_optional_1 = if self.int_optional_1.is_some() -// { -// Some( self.int_optional_1.take().unwrap() ) -// } -// else -// { -// None -// }; -// -// let string_optional_1 = if self.string_optional_1.is_some() -// { -// Some( self.string_optional_1.take().unwrap() ) -// } -// else -// { -// None -// }; -// -// // xxx : Rust failt to use parameter here -// // < < Self as former::Storage >::Descriptor as former::FormerDescriptor >::Formed -// Struct1 -// { -// int_1, -// string_1, -// int_optional_1, -// string_optional_1, -// } -// -// } -// -// } -// -// // -// -// pub struct Struct1Former -// < -// FormerContext = Struct1, -// FormerEnd = the_module::ReturnStorage, -// > -// where -// FormerEnd : the_module::FormingEnd< Struct1FormerDescriptor, FormerContext >, -// { -// storage : Struct1FormerStorage, -// context : core::option::Option< FormerContext >, -// on_end : core::option::Option< FormerEnd >, -// } -// -// impl< FormerContext, FormerEnd > Struct1Former< FormerContext, FormerEnd > -// where -// FormerEnd: the_module::FormingEnd< Struct1FormerDescriptor, FormerContext >, -// { -// -// fn preform( self ) -> < Struct1FormerDescriptor as former::FormerDescriptor >::Formed -// { -// former::StoragePerform::preform( self.storage ) -// } -// -// #[ inline( always ) ] -// pub fn perform(self) -> < Struct1FormerDescriptor as former::FormerDescriptor >::Formed -// { -// let result = self.form(); -// return result; -// } -// -// #[ inline( always ) ] -// pub fn begin -// ( -// mut storage : core::option::Option< < Struct1FormerDescriptor as former::FormerDescriptor >::Storage >, -// // xxx : cover by test existance of these 3 parameters in the function -// context : core::option::Option< FormerContext >, -// on_end : FormerEnd, -// ) -> Self -// { -// if storage.is_none() -// { -// storage = Some( core::default::Default::default() ); -// } -// Self -// { -// storage : storage.unwrap(), -// context : context, -// on_end : ::core::option::Option::Some( on_end ), -// } -// } -// -// #[ inline( always ) ] -// pub fn end( mut self ) -> < Struct1FormerDescriptor as former::FormerDescriptor >::Formed -// { -// let on_end = self.on_end.take().unwrap(); -// let context = self.context.take(); -// on_end.call( self.storage, context ) -// } -// -// #[ inline( always ) ] -// pub fn form( self ) -> < Struct1FormerDescriptor as former::FormerDescriptor >::Formed -// { -// self.end() -// } -// -// pub fn int_1< Src >( mut self, src : Src ) -> Self -// where Src : core::convert::Into< i32 >, -// { -// debug_assert!( self.storage.int_1.is_none() ); -// self.storage.int_1 = Some( src.into() ); -// self -// } -// -// pub fn string_1< Src >( mut self, src : Src ) -> Self -// where Src : core::convert::Into< String >, -// { -// debug_assert!( self.storage.string_1.is_none() ); -// self.storage.string_1 = Some( src.into() ); -// self -// } -// -// pub fn string_optional_1< Src >( mut self, src : Src ) -> Self -// where Src : core::convert::Into< String > -// { -// debug_assert!( self.storage.string_optional_1.is_none() ); -// self.storage.string_optional_1 = Some( src.into() ); -// self -// } -// -// } -// -// impl Struct1Former< (), the_module::ReturnStorage > -// { -// -// #[ inline( always ) ] -// pub fn new() -> Struct1Former< (), the_module::ReturnStorage > -// { -// Struct1Former::< (), the_module::ReturnStorage >::begin( None, None, the_module::ReturnStorage ) -// } -// -// } - -// - -// include!( "./only_test/primitives.rs" ); +include!( "./only_test/primitives.rs" ); diff --git a/module/core/former/tests/inc/former_tests/a_primitives_expanded.rs b/module/core/former/tests/inc/former_tests/a_primitives_expanded.rs index 8771d29773..812d40be6e 100644 --- a/module/core/former/tests/inc/former_tests/a_primitives_expanded.rs +++ b/module/core/former/tests/inc/former_tests/a_primitives_expanded.rs @@ -1,8 +1,8 @@ #[ allow( unused_imports ) ] use super::*; -// #[ derive( Debug, PartialEq ) ] -#[ derive( Debug, PartialEq, the_module::Former ) ] #[ debug ] +#[ derive( Debug, PartialEq ) ] +// #[ derive( Debug, PartialEq, the_module::Former ) ] #[ debug ] pub struct Struct1 { pub int_1 : i32, diff --git a/module/core/former/tests/inc/former_tests/a_primitives_manual.rs b/module/core/former/tests/inc/former_tests/a_primitives_manual.rs index 7462251a80..054f83e6e0 100644 --- a/module/core/former/tests/inc/former_tests/a_primitives_manual.rs +++ b/module/core/former/tests/inc/former_tests/a_primitives_manual.rs @@ -10,7 +10,7 @@ pub struct Struct1 string_optional_1 : Option< String >, } -// +// = formed // generated by former impl Struct1 @@ -21,7 +21,27 @@ impl Struct1 } } -// +// = descriptor + +#[ derive( Debug ) ] +pub struct Struct1FormerDescriptor; + +impl Struct1FormerDescriptor +{ + pub fn new() -> Self + { + Self + } +} + +impl former::FormerDescriptor +for Struct1FormerDescriptor +{ + type Storage = Struct1FormerStorage; + type Formed = Struct1; +} + +// = storage // generated by former pub struct Struct1FormerStorage @@ -32,7 +52,6 @@ pub struct Struct1FormerStorage pub string_optional_1 : core::option::Option< String >, } -// xxx : cover by test impl Default for Struct1FormerStorage { @@ -50,7 +69,72 @@ impl Default for Struct1FormerStorage } -// +impl former::Storage +for Struct1FormerStorage +{ + type Descriptor = Struct1FormerDescriptor; +} + +impl former::StoragePerform +for Struct1FormerStorage +{ + + fn preform( mut self ) -> < < Self as former::Storage >::Descriptor as former::FormerDescriptor >::Formed + { + + let int_1 = if self.int_1.is_some() + { + self.int_1.take().unwrap() + } + else + { + let val : i32 = Default::default(); + val + }; + + let string_1 = if self.string_1.is_some() + { + self.string_1.take().unwrap() + } + else + { + let val : String = Default::default(); + val + }; + + let int_optional_1 = if self.int_optional_1.is_some() + { + Some( self.int_optional_1.take().unwrap() ) + } + else + { + None + }; + + let string_optional_1 = if self.string_optional_1.is_some() + { + Some( self.string_optional_1.take().unwrap() ) + } + else + { + None + }; + + // Rust failt to use parameter here + // < < Self as former::Storage >::Descriptor as former::FormerDescriptor >::Formed + Struct1 + { + int_1, + string_1, + int_optional_1, + string_optional_1, + } + + } + +} + +// = former pub struct Struct1Former < @@ -86,9 +170,9 @@ where pub fn begin ( mut storage : core::option::Option< < Struct1FormerDescriptor as former::FormerDescriptor >::Storage >, - // xxx : cover by test existance of these 3 parameters in the function context : core::option::Option< FormerContext >, on_end : FormerEnd, + // xxx : cover by test existance of these 3 parameters in the function ) -> Self { if storage.is_none() @@ -154,89 +238,6 @@ impl Struct1Former< (), the_module::ReturnStorage > } -#[ derive( Debug ) ] -pub struct Struct1FormerDescriptor; - -impl Struct1FormerDescriptor -{ - pub fn new() -> Self - { - Self - } -} - -impl former::FormerDescriptor -for Struct1FormerDescriptor -{ - type Storage = Struct1FormerStorage; - type Formed = Struct1; -} - -impl former::Storage -for Struct1FormerStorage -{ - type Descriptor = Struct1FormerDescriptor; -} - -impl former::StoragePerform -for Struct1FormerStorage -{ - - fn preform( mut self ) -> < < Self as former::Storage >::Descriptor as former::FormerDescriptor >::Formed - { - - let int_1 = if self.int_1.is_some() - { - self.int_1.take().unwrap() - } - else - { - let val : i32 = Default::default(); - val - }; - - let string_1 = if self.string_1.is_some() - { - self.string_1.take().unwrap() - } - else - { - let val : String = Default::default(); - val - }; - - let int_optional_1 = if self.int_optional_1.is_some() - { - Some( self.int_optional_1.take().unwrap() ) - } - else - { - None - }; - - let string_optional_1 = if self.string_optional_1.is_some() - { - Some( self.string_optional_1.take().unwrap() ) - } - else - { - None - }; - - // xxx : Rust failt to use parameter here - // < < Self as former::Storage >::Descriptor as former::FormerDescriptor >::Formed - Struct1 - { - int_1, - string_1, - int_optional_1, - string_optional_1, - } - - } - -} - // include!( "./only_test/primitives.rs" ); diff --git a/module/core/former/tests/inc/former_tests/a_primitives_manual_original.rs b/module/core/former/tests/inc/former_tests/a_primitives_manual_original.rs deleted file mode 100644 index 5db0ab5a74..0000000000 --- a/module/core/former/tests/inc/former_tests/a_primitives_manual_original.rs +++ /dev/null @@ -1,192 +0,0 @@ -#[ allow( unused_imports ) ] -use super::*; - -#[ derive( Debug, PartialEq ) ] -pub struct Struct1 -{ - pub int_1 : i32, - string_1 : String, - int_optional_1 : core::option::Option< i32 >, - string_optional_1 : Option< String >, -} - -// - -// generated by former -impl Struct1 -{ - pub fn former() -> Struct1Former< Struct1, Former::ReturnFormed > - { - Struct1Former::< Struct1, the_module::ReturnFormed >::new() - } -} - -// - -// generated by former -pub struct Struct1FormerStorage -{ - pub int_1 : core::option::Option< i32 >, - pub string_1 : core::option::Option< String >, - pub int_optional_1 : core::option::Option< i32 >, - pub string_optional_1 : core::option::Option< String >, -} - -impl Default for Struct1FormerStorage -{ - - #[ inline( always ) ] - fn default() -> Self - { - Self - { - int_1 : core::option::Option::None, - string_1 : core::option::Option::None, - int_optional_1 : core::option::Option::None, - string_optional_1 : core::option::Option::None, - } - } - -} - -// - -pub struct Struct1Former -< - FormerContext = Struct1, - FormerEnd = the_module::ReturnFormed, -> -where - FormerEnd : the_module::FormingEnd< Struct1, FormerContext >, -{ - storage : Struct1FormerStorage, - context : core::option::Option< FormerContext >, - on_end : core::option::Option< FormerEnd >, -} - -impl< FormerContext, FormerEnd > Struct1Former< FormerContext, FormerEnd > -where - FormerEnd: the_module::FormingEnd, -{ - - fn form( mut self ) -> Struct1 - { - - let int_1 = if self.storage.int_1.is_some() - { - self.storage.int_1.take().unwrap() - } - else - { - let val : i32 = Default::default(); - val - }; - - let string_1 = if self.storage.string_1.is_some() - { - self.storage.string_1.take().unwrap() - } - else - { - let val : String = Default::default(); - val - }; - - let int_optional_1 = if self.storage.int_optional_1.is_some() - { - Some( self.storage.int_optional_1.take().unwrap() ) - } - else - { - None - }; - - let string_optional_1 = if self.storage.string_optional_1.is_some() - { - Some( self.storage.string_optional_1.take().unwrap() ) - } - else - { - None - }; - - Struct1 - { - int_1, - string_1, - int_optional_1, - string_optional_1, - } - - } - - #[ inline( always ) ] - pub fn perform(self) -> Struct1 - { - let result = self.form(); - return result; - } - - #[ inline( always ) ] - pub fn new() -> Struct1Former - { - Struct1Former:: - < - Struct1, - the_module::ReturnFormed, - >::begin(None, the_module::ReturnFormed) - } - - #[ inline( always ) ] - pub fn begin - ( - context : core::option::Option< FormerContext >, - on_end : FormerEnd, - ) -> Self - { - Self - { - storage : core::default::Default::default(), - context : context, - on_end : ::core::option::Option::Some( on_end ), - } - } - - #[ inline( always ) ] - pub fn end( mut self ) -> FormerContext - { - let on_end = self.on_end.take().unwrap(); - let context = self.context.take(); - let formed = self.form(); - on_end.call( formed, context ) - } - - pub fn int_1< Src >( mut self, src : Src ) -> Self - where Src : core::convert::Into< i32 >, - { - debug_assert!( self.storage.int_1.is_none() ); - self.storage.int_1 = Some( src.into() ); - self - } - - pub fn string_1< Src >( mut self, src : Src ) -> Self - where Src : core::convert::Into< String >, - { - debug_assert!( self.storage.string_1.is_none() ); - self.storage.string_1 = Some( src.into() ); - self - } - - pub fn string_optional_1< Src >( mut self, src : Src ) -> Self - where Src : core::convert::Into< String > - { - debug_assert!( self.storage.string_optional_1.is_none() ); - self.storage.string_optional_1 = Some( src.into() ); - self - } - -} - -// - -include!( "./only_test/primitives.rs" ); diff --git a/module/core/former/tests/inc/former_tests/only_test/primitives.rs b/module/core/former/tests/inc/former_tests/only_test/primitives.rs index 546e1679ae..f57fa78f30 100644 --- a/module/core/former/tests/inc/former_tests/only_test/primitives.rs +++ b/module/core/former/tests/inc/former_tests/only_test/primitives.rs @@ -43,6 +43,18 @@ tests_impls! // + fn begin() + { + + let mut storage = Struct1FormerStorage::default(); + storage.int_1 = Some( 13 ); + // Struct1Former::< (), the_module::ReturnStorage >::begin( storage, None, the_module::ReturnStorage ) + // xxx + + } + + // + fn preform() { @@ -88,11 +100,15 @@ tests_impls! { // descriptor exists and has Storage - // use former::StoragePerform; let got = < Struct1FormerStorage as the_module::StoragePerform >::preform( Struct1::former().storage ); let exp = Struct1::former().form(); a_id!( got, exp ); + // default is implemented for Storage + let got = Struct1FormerStorage::default().preform(); + let exp = Struct1::former().storage.preform(); + a_id!( got, exp ); + // descriptor exists and has Storage use former::StoragePerform; let got = Struct1::former().storage.preform(); @@ -290,6 +306,7 @@ tests_impls! tests_index! { internals, + begin, preform, descriptor, storage, diff --git a/module/core/former/tests/inc/mod.rs b/module/core/former/tests/inc/mod.rs index 8d654b850d..21d986470d 100644 --- a/module/core/former/tests/inc/mod.rs +++ b/module/core/former/tests/inc/mod.rs @@ -8,8 +8,8 @@ mod former_tests use super::*; mod a_primitives_manual; - // mod a_primitives; - mod a_primitives_expanded; + mod a_primitives; + // mod a_primitives_expanded; // mod a_containers_without_runtime_manual; // mod a_containers_without_runtime; // #[ cfg( not( feature = "no_std" ) ) ]