From 3baa864fe6830afa9bf6385ae22cff8a4c1a85c6 Mon Sep 17 00:00:00 2001 From: wandalen Date: Thu, 21 Mar 2024 22:23:57 +0200 Subject: [PATCH] former : making subforming more friendly --- module/core/former/Readme.md | 20 ++--- .../former/examples/former_trivial_expaned.rs | 20 ++--- module/core/former/src/axiomatic.rs | 34 ++++----- module/core/former/src/vector.rs | 2 +- .../a_containers_with_runtime_manual.rs | 75 ++++++++++--------- .../inc/former_tests/attribute_setter.rs | 4 +- .../inc/former_tests/subformer_shortcut.rs | 6 +- module/core/former_meta/src/derive/former.rs | 4 +- module/core/former_meta/src/lib.rs | 18 ++--- 9 files changed, 94 insertions(+), 89 deletions(-) diff --git a/module/core/former/Readme.md b/module/core/former/Readme.md index cdd6e00700..42a37ad6c6 100644 --- a/module/core/former/Readme.md +++ b/module/core/former/Readme.md @@ -101,20 +101,20 @@ pub struct UserProfileFormerStorage pub struct UserProfileFormer < - FormerContext = UserProfile, - FormerEnd = former::ReturnFormed, + Context = UserProfile, + End = former::ReturnFormed, > where - FormerEnd : former::FormingEnd< UserProfile, FormerContext >, + End : former::FormingEnd< UserProfile, Context >, { storage : UserProfileFormerStorage, - context : Option< FormerContext >, - on_end : Option< FormerEnd >, + context : Option< Context >, + on_end : Option< End >, } -impl< FormerContext, FormerEnd > UserProfileFormer< FormerContext, FormerEnd > +impl< Context, End > UserProfileFormer< Context, End > where - FormerEnd : former::FormingEnd< UserProfile, FormerContext >, + End : former::FormingEnd< UserProfile, Context >, { #[ inline( always ) ] pub fn form( mut self ) -> UserProfile @@ -212,8 +212,8 @@ where #[ inline( always ) ] pub fn begin ( - context : Option< FormerContext >, - on_end : FormerEnd, + context : Option< Context >, + on_end : End, ) -> Self { Self @@ -225,7 +225,7 @@ where } #[ inline( always ) ] - pub fn end( mut self ) -> FormerContext + pub fn end( mut self ) -> Context { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); diff --git a/module/core/former/examples/former_trivial_expaned.rs b/module/core/former/examples/former_trivial_expaned.rs index 0eaeba8b41..5b8d8cd8c2 100644 --- a/module/core/former/examples/former_trivial_expaned.rs +++ b/module/core/former/examples/former_trivial_expaned.rs @@ -54,20 +54,20 @@ fn main() pub struct UserProfileFormer < - FormerContext = UserProfile, - FormerEnd = former::ReturnFormed, + Context = UserProfile, + End = former::ReturnFormed, > where - FormerEnd : former::FormingEnd< UserProfile, FormerContext >, + End : former::FormingEnd< UserProfile, Context >, { storage : UserProfileFormerStorage, - context : Option< FormerContext >, - on_end : Option< FormerEnd >, + context : Option< Context >, + on_end : Option< End >, } - impl< FormerContext, FormerEnd > UserProfileFormer< FormerContext, FormerEnd > + impl< Context, End > UserProfileFormer< Context, End > where - FormerEnd : former::FormingEnd< UserProfile, FormerContext >, + End : former::FormingEnd< UserProfile, Context >, { #[ inline( always ) ] pub fn form( mut self ) -> UserProfile @@ -165,8 +165,8 @@ fn main() #[ inline( always ) ] pub fn begin ( - context : Option< FormerContext >, - on_end : FormerEnd, + context : Option< Context >, + on_end : End, ) -> Self { Self @@ -178,7 +178,7 @@ fn main() } #[ inline( always ) ] - pub fn end( mut self ) -> FormerContext + pub fn end( mut self ) -> Context { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); diff --git a/module/core/former/src/axiomatic.rs b/module/core/former/src/axiomatic.rs index 2abaa561d5..789375f758 100644 --- a/module/core/former/src/axiomatic.rs +++ b/module/core/former/src/axiomatic.rs @@ -101,21 +101,21 @@ for FormingEndWrapper< Formed, 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 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. /// @@ -158,7 +158,7 @@ for ReturnFormed /// potentially using the provided `Context`. /// -pub trait FormerBegin< Formed, Context > +pub trait FormerBegin< Storage, Formed, Context > { /// * `End` - Specifies the trait bound for the closure or handler that gets called at the completion @@ -184,7 +184,7 @@ pub trait FormerBegin< Formed, Context > /// fn _begin ( - formed : core::option::Option< Formed >, + storage : core::option::Option< Storage >, context : core::option::Option< Context >, on_end : Self::End, ) -> Self; diff --git a/module/core/former/src/vector.rs b/module/core/former/src/vector.rs index e40e1be1c7..46426d8733 100644 --- a/module/core/former/src/vector.rs +++ b/module/core/former/src/vector.rs @@ -185,7 +185,7 @@ where // -impl< E, Formed, Context, End > FormerBegin< Formed, Context > +impl< E, Formed, Context, End > FormerBegin< Formed, Formed, Context > for VectorSubformer< E, Formed, Context, End > where End : FormingEnd< Formed, Context >, diff --git a/module/core/former/tests/inc/former_tests/a_containers_with_runtime_manual.rs b/module/core/former/tests/inc/former_tests/a_containers_with_runtime_manual.rs index fac9782a88..87a3bec1eb 100644 --- a/module/core/former/tests/inc/former_tests/a_containers_with_runtime_manual.rs +++ b/module/core/former/tests/inc/former_tests/a_containers_with_runtime_manual.rs @@ -47,20 +47,20 @@ impl Default for Struct1FormerStorage pub struct Struct1Former < - FormerContext = Struct1, - FormerEnd = the_module::ReturnFormed, + Context = Struct1, + End = the_module::ReturnFormed, > where - FormerEnd : the_module::FormingEnd< Struct1, FormerContext >, + End : the_module::FormingEnd< Struct1, Context >, { storage : Struct1FormerStorage, - context : ::core::option::Option< FormerContext >, - on_end : ::core::option::Option< FormerEnd >, + context : ::core::option::Option< Context >, + on_end : ::core::option::Option< End >, } -impl< FormerContext, FormerEnd > Struct1Former< FormerContext, FormerEnd > +impl< Context, End > Struct1Former< Context, End > where - FormerEnd : the_module::FormingEnd< Struct1, FormerContext >, + End : the_module::FormingEnd< Struct1, Context >, { #[ inline( always ) ] @@ -127,8 +127,8 @@ where pub fn begin ( mut storage : ::core::option::Option< Struct1FormerStorage >, - context : ::core::option::Option< FormerContext >, - on_end : FormerEnd, + context : ::core::option::Option< Context >, + on_end : End, ) -> Self { if storage.is_none() @@ -144,7 +144,7 @@ where } #[ inline( always ) ] - pub fn end( mut self ) -> FormerContext + pub fn end( mut self ) -> Context { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); @@ -156,7 +156,12 @@ where pub fn __vec_1< Former2 >( self ) -> Former2 where - Former2 : former::FormerBegin< Vec< String >, Self, End = former::FormingEndWrapper< Vec< String >, Self > >, + Former2 : former::FormerBegin + < + Vec< String >, + Vec< String >, + Self, End = former::FormingEndWrapper< Vec< String >, Self >, + >, { let on_end = | formed : Vec< String >, super_former : ::core::option::Option< Self > | -> Self { @@ -243,9 +248,9 @@ where } -// impl< FormerContext, FormerEnd > Struct1Former< FormerContext, FormerEnd > +// impl< Context, End > Struct1Former< Context, End > // where -// FormerEnd: the_module::FormingEnd, +// End: the_module::FormingEnd, impl Struct1Former< Struct1, the_module::ReturnFormed > { @@ -260,30 +265,30 @@ impl Struct1Former< Struct1, the_module::ReturnFormed > // -// impl< FormerContext, FormerEnd > Struct1Former< FormerContext, FormerEnd > +// impl< Context, End > Struct1Former< Context, End > // where -// FormerEnd : the_module::FormingEnd< Struct1, FormerContext >, +// End : the_module::FormingEnd< Struct1, Context >, -// impl< FormerContext, FormerEnd > former::FormerBegin< Struct1, FormerContext > -// for Struct1Former< FormerContext, FormerEnd > -// where -// End : the_module::FormingEnd< Struct1, FormerContext >, -// { -// type End = End; -// -// #[ inline( always ) ] -// fn _begin -// ( -// storage : core::option::Option< Struct1FormerStorage >, /* xxx2 : that should be storage */ -// context : core::option::Option< FormerContext >, -// on_end : End, -// ) -> Self -// { -// debug_assert!( storage.is_none() ); -// Self::begin( None, context, on_end ) -// } -// -// } +impl< Context, End > former::FormerBegin< Struct1FormerStorage, Struct1, Context > +for Struct1Former< Context, End > +where + End : the_module::FormingEnd< Struct1, Context >, +{ + type End = End; + + #[ inline( always ) ] + fn _begin + ( + storage : core::option::Option< Struct1FormerStorage >, /* xxx2 : that should be storage */ + context : core::option::Option< Context >, + on_end : End, + ) -> Self + { + debug_assert!( storage.is_none() ); + Self::begin( None, context, on_end ) + } + +} // diff --git a/module/core/former/tests/inc/former_tests/attribute_setter.rs b/module/core/former/tests/inc/former_tests/attribute_setter.rs index c9139c2f90..14e852373e 100644 --- a/module/core/former/tests/inc/former_tests/attribute_setter.rs +++ b/module/core/former/tests/inc/former_tests/attribute_setter.rs @@ -9,9 +9,9 @@ pub struct StructWithCustomSetters magic : String, } -impl< FormerContext, FormerEnd > StructWithCustomSettersFormer< FormerContext, FormerEnd > +impl< Context, End > StructWithCustomSettersFormer< Context, End > where - FormerEnd: the_module::FormingEnd< StructWithCustomSetters, FormerContext >, + End: the_module::FormingEnd< StructWithCustomSetters, Context >, { /// Custom alternative setter of ordinary field. diff --git a/module/core/former/tests/inc/former_tests/subformer_shortcut.rs b/module/core/former/tests/inc/former_tests/subformer_shortcut.rs index 0b73af03b9..bbcc8b9b4e 100644 --- a/module/core/former/tests/inc/former_tests/subformer_shortcut.rs +++ b/module/core/former/tests/inc/former_tests/subformer_shortcut.rs @@ -26,7 +26,7 @@ pub struct TemplateParameters } -impl< Context, End > former::FormerBegin< TemplateParameterDescriptor, Context > +impl< Context, End > former::FormerBegin< TemplateParameterDescriptorFormerStorage, TemplateParameterDescriptor, Context > for TemplateParameterDescriptorFormer< Context, End > where End : the_module::FormingEnd< TemplateParameterDescriptor, Context >, @@ -36,7 +36,7 @@ where #[ inline( always ) ] fn _begin ( - storage : core::option::Option< TemplateParameterDescriptor >, /* xxx2 : that should be storage */ + storage : core::option::Option< TemplateParameterDescriptorFormerStorage >, /* xxx2 : that should be storage */ context : core::option::Option< Context >, on_end : End, ) -> Self @@ -56,7 +56,7 @@ where pub fn descriptor3< Former2 >( self ) -> Former2 where - Former2 : former::FormerBegin< TemplateParameterDescriptor, Self, End = former::FormingEndWrapper< TemplateParameterDescriptor, Self > >, + Former2 : former::FormerBegin< TemplateParameterDescriptorFormerStorage, TemplateParameterDescriptor, Self, End = former::FormingEndWrapper< TemplateParameterDescriptor, Self > >, // FieldContainer : ContainerAdd, { let on_end = | descriptor : TemplateParameterDescriptor, super_former : core::option::Option< Self > | -> Self diff --git a/module/core/former_meta/src/derive/former.rs b/module/core/former_meta/src/derive/former.rs index 3e60e046e3..5ca9feb66c 100644 --- a/module/core/former_meta/src/derive/former.rs +++ b/module/core/former_meta/src/derive/former.rs @@ -1073,9 +1073,9 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > { let _example = r#" -impl< FormerContext, FormerEnd > UserProfileFormer< FormerContext, FormerEnd > +impl< Context, End > UserProfileFormer< Context, End > where - FormerEnd : former::FormingEnd< UserProfile, FormerContext >, + End : former::FormingEnd< UserProfile, Context >, { pub fn age< Src >( mut self, src : Src ) -> Self where diff --git a/module/core/former_meta/src/lib.rs b/module/core/former_meta/src/lib.rs index 3045a5076f..e02c582eaf 100644 --- a/module/core/former_meta/src/lib.rs +++ b/module/core/former_meta/src/lib.rs @@ -135,20 +135,20 @@ mod derive /// /// pub struct UserProfileFormer /// < -/// FormerContext = UserProfile, -/// FormerEnd = former::ReturnFormed, +/// Context = UserProfile, +/// End = former::ReturnFormed, /// > /// where -/// FormerEnd : former::FormingEnd< UserProfile, FormerContext >, +/// End : former::FormingEnd< UserProfile, Context >, /// { /// storage : UserProfileFormerStorage, -/// context : Option< FormerContext >, -/// on_end : Option< FormerEnd >, +/// context : Option< Context >, +/// on_end : Option< End >, /// } /// -/// impl< FormerContext, FormerEnd > UserProfileFormer< FormerContext, FormerEnd > +/// impl< Context, End > UserProfileFormer< Context, End > /// where -/// FormerEnd : former::FormingEnd< UserProfile, FormerContext >, +/// End : former::FormingEnd< UserProfile, Context >, /// { /// #[ inline( always ) ] /// pub fn form( mut self ) -> UserProfile @@ -195,7 +195,7 @@ mod derive /// } /// /// #[ inline( always ) ] -/// pub fn begin( context : Option< FormerContext >, on_end : FormerEnd ) -> Self +/// pub fn begin( context : Option< Context >, on_end : End ) -> Self /// { /// Self /// { @@ -206,7 +206,7 @@ mod derive /// } /// /// #[ inline( always ) ] -/// pub fn end( mut self ) -> FormerContext +/// pub fn end( mut self ) -> Context /// { /// let on_end = self.on_end.take().unwrap(); /// let context = self.context.take();