From becb7ede230f290e130c5cb5b6d830d54055af1b Mon Sep 17 00:00:00 2001 From: wandalen Date: Thu, 21 Mar 2024 22:46:04 +0200 Subject: [PATCH] former : making subforming more friendly --- module/core/former/src/axiomatic.rs | 34 +++++++++---------- .../inc/former_tests/subformer_shortcut.rs | 8 ++++- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/module/core/former/src/axiomatic.rs b/module/core/former/src/axiomatic.rs index 789375f758..161a0f3ea8 100644 --- a/module/core/former/src/axiomatic.rs +++ b/module/core/former/src/axiomatic.rs @@ -22,12 +22,12 @@ pub trait FormingEnd< Formed, Context > fn call( &self, storage : Formed, context : core::option::Option< Context > ) -> Context; } -impl< Formed, Context, F > FormingEnd< Formed, Context > for F +impl< Storage, Context, F > FormingEnd< Storage, Context > for F where - F : Fn( Formed, core::option::Option< Context > ) -> Context, + F : Fn( Storage, core::option::Option< Context > ) -> Context, { #[ inline( always ) ] - fn call( &self, storage : Formed, context : core::option::Option< Context > ) -> Context + fn call( &self, storage : Storage, context : core::option::Option< Context > ) -> Context { self( storage, context ) } @@ -42,19 +42,19 @@ where /// /// # Type Parameters /// -/// * `Formed` - The type of the container being processed. This type is passed to the closure +/// * `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 FormingEndWrapper< Formed, Context > +pub struct FormingEndWrapper< Storage, Context > { - closure : Box< dyn Fn( Formed, Option< Context > ) -> Context >, - _marker : std::marker::PhantomData< Formed >, + closure : Box< dyn Fn( Storage, Option< Context > ) -> Context >, + _marker : std::marker::PhantomData< Storage >, } #[ cfg( not( feature = "no_std" ) ) ] -impl< Formed, Context > FormingEndWrapper< Formed, Context > +impl< Storage, Context > FormingEndWrapper< Storage, Context > { /// Constructs a new `FormingEndWrapper` with the provided closure. /// @@ -67,7 +67,7 @@ impl< Formed, Context > FormingEndWrapper< Formed, Context > /// # Returns /// /// Returns an instance of `FormingEndWrapper` encapsulating the provided closure. - pub fn new( closure : impl Fn( Formed, Option< Context > ) -> Context + 'static ) -> Self + pub fn new( closure : impl Fn( Storage, Option< Context > ) -> Context + 'static ) -> Self { Self { @@ -80,7 +80,7 @@ impl< Formed, Context > FormingEndWrapper< Formed, Context > #[ cfg( not( feature = "no_std" ) ) ] use std::fmt; #[ cfg( not( feature = "no_std" ) ) ] -impl< Formed, Context > fmt::Debug for FormingEndWrapper< Formed, Context > +impl< Storage, Context > fmt::Debug for FormingEndWrapper< Storage, Context > { fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result { @@ -92,12 +92,12 @@ impl< Formed, Context > fmt::Debug for FormingEndWrapper< Formed, Context > } #[ cfg( not( feature = "no_std" ) ) ] -impl< Formed, Context > FormingEnd< Formed, Context > -for FormingEndWrapper< Formed, Context > +impl< Storage, Context > FormingEnd< Storage, Context > +for FormingEndWrapper< Storage, Context > { - fn call( &self, formed : Formed, context : Option< Context > ) -> Context + fn call( &self, storage : Storage, context : Option< Context > ) -> Context { - ( self.closure )( formed, context ) + ( self.closure )( storage, context ) } } @@ -124,13 +124,13 @@ for FormingEndWrapper< Formed, Context > #[ derive( Debug, Default ) ] pub struct ReturnFormed; -impl< Formed > FormingEnd< Formed, Formed > +impl< Storage > FormingEnd< Storage, Storage > for ReturnFormed { #[ inline( always ) ] - fn call( &self, formed : Formed, _context : core::option::Option< Formed > ) -> Formed + fn call( &self, storage : Storage, _context : core::option::Option< Storage > ) -> Storage { - formed + storage } } 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 bbcc8b9b4e..bf8c825690 100644 --- a/module/core/former/tests/inc/former_tests/subformer_shortcut.rs +++ b/module/core/former/tests/inc/former_tests/subformer_shortcut.rs @@ -56,7 +56,13 @@ where pub fn descriptor3< Former2 >( self ) -> Former2 where - Former2 : former::FormerBegin< TemplateParameterDescriptorFormerStorage, 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