Skip to content

Commit

Permalink
former : experimenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Mar 30, 2024
1 parent 1308122 commit 2fb4a30
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
16 changes: 8 additions & 8 deletions module/core/former/src/axiomatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ where
/// a closure needs to be stored or passed around as an object implementing
/// `FormingEnd`.
#[ cfg( not( feature = "no_std" ) ) ]
pub struct FormingEndWrapper< Definition : FormerDefinitionTypes >
pub struct FormingEndClosure< Definition : FormerDefinitionTypes >
{
closure : Box< dyn Fn( Definition::Storage, Option< Definition::Context > ) -> Definition::Formed >,
_marker : std::marker::PhantomData< Definition::Storage >,
}

impl< T, Definition > From< T > for FormingEndWrapper< Definition >
impl< T, Definition > From< T > for FormingEndClosure< Definition >
where
T : Fn( Definition::Storage, Option< Definition::Context > ) -> Definition::Formed + 'static,
Definition : FormerDefinitionTypes,
Expand All @@ -153,9 +153,9 @@ where
}

#[ cfg( not( feature = "no_std" ) ) ]
impl< Definition : FormerDefinitionTypes > FormingEndWrapper< Definition >
impl< Definition : FormerDefinitionTypes > FormingEndClosure< Definition >
{
/// Constructs a new `FormingEndWrapper` with the provided closure.
/// Constructs a new `FormingEndClosure` with the provided closure.
///
/// # Parameters
///
Expand All @@ -165,7 +165,7 @@ impl< Definition : FormerDefinitionTypes > FormingEndWrapper< Definition >
///
/// # Returns
///
/// Returns an instance of `FormingEndWrapper` encapsulating the provided closure.
/// Returns an instance of `FormingEndClosure` encapsulating the provided closure.
pub fn new( closure : impl Fn( Definition::Storage, Option< Definition::Context > ) -> Definition::Formed + 'static ) -> Self
{
Self
Expand All @@ -179,11 +179,11 @@ impl< Definition : FormerDefinitionTypes > FormingEndWrapper< Definition >
#[ cfg( not( feature = "no_std" ) ) ]
use std::fmt;
#[ cfg( not( feature = "no_std" ) ) ]
impl< Definition : FormerDefinitionTypes > fmt::Debug for FormingEndWrapper< Definition >
impl< Definition : FormerDefinitionTypes > fmt::Debug for FormingEndClosure< Definition >
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
f.debug_struct( "FormingEndWrapper" )
f.debug_struct( "FormingEndClosure" )
.field( "closure", &format_args!{ "- closure -" } )
.field( "_marker", &self._marker )
.finish()
Expand All @@ -192,7 +192,7 @@ impl< Definition : FormerDefinitionTypes > fmt::Debug for FormingEndWrapper< Def

#[ cfg( not( feature = "no_std" ) ) ]
impl< Definition : FormerDefinitionTypes > FormingEnd< Definition >
for FormingEndWrapper< Definition >
for FormingEndClosure< Definition >
{
fn call( &self, storage : Definition::Storage, context : Option< Definition::Context > ) -> Definition::Formed
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ where
<
Vec< String >,
Vec< String >,
Self, End = former::FormingEndWrapper< Vec< String >, Self >,
Self, End = former::FormingEndClosure< Vec< String >, Self >,
>,
{
let on_end = | formed : Vec< String >, super_former : ::core::option::Option< Self > | -> Self
Expand All @@ -178,7 +178,7 @@ where
}
super_former
};
Former2::_begin( None, Some( self ), former::FormingEndWrapper::new( on_end ) )
Former2::_begin( None, Some( self ), former::FormingEndClosure::new( on_end ) )
}

// xxx2 : continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ where
type End = End;
}

// zzz : documentation
pub type Struct1FormerWithClosure< Context, Formed > =
Struct1FormerDefinition< Context, Formed, former::FormingEndClosure< Struct1FormerDefinitionTypes< Context, Formed > > >;

// = storage

// generated by former
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,15 @@ fn custom_definition_custom_end()
impl former::FormerDefinition for Return13
{
type Types = Return13;
type End = former::FormingEndWrapper< < Self as former::FormerDefinition >::Types >;
type End = former::FormingEndClosure< < Self as former::FormerDefinition >::Types >;
}

fn return_13( _storage : Vec< String >, _context : Option< () > ) -> i32
{
13
}

let end_wrapper : the_module::FormingEndWrapper< Return13 > = the_module::FormingEndWrapper::new( return_13 );
let end_wrapper : the_module::FormingEndClosure< Return13 > = the_module::FormingEndClosure::new( return_13 );
let got = the_module::ContainerSubformer::< String, Return13 >::new( end_wrapper )
.add( "a" )
.add( "b" )
Expand Down
46 changes: 32 additions & 14 deletions module/core/former/tests/inc/former_tests/only_test/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tests_impls!

// custom params
let got = Struct1Former
::< Struct1FormerDefinition< i32, i32, former::FormingEndWrapper< Struct1FormerDefinitionTypes< i32, i32 > > > >
::< Struct1FormerDefinition< i32, i32, former::FormingEndClosure< Struct1FormerDefinitionTypes< i32, i32 > > > >
::begin
(
None,
Expand All @@ -62,7 +62,7 @@ tests_impls!

// custom params with into
let got = Struct1Former
::< Struct1FormerDefinition< i32, i32, former::FormingEndWrapper< Struct1FormerDefinitionTypes< i32, i32 > > > >
::< Struct1FormerDefinition< i32, i32, former::FormingEndClosure< Struct1FormerDefinitionTypes< i32, i32 > > > >
::begin
(
None,
Expand All @@ -75,7 +75,7 @@ tests_impls!

// custom params begin
let got = Struct1Former
::< Struct1FormerDefinition< i32, i32, former::FormingEndWrapper< Struct1FormerDefinitionTypes< i32, i32 > > > >
::< Struct1FormerDefinition< i32, i32, former::FormingEndClosure< Struct1FormerDefinitionTypes< i32, i32 > > > >
::begin
(
None,
Expand Down Expand Up @@ -135,7 +135,7 @@ tests_impls!
(
None,
Some( 3 ),
former::FormingEndWrapper::new
former::FormingEndClosure::new
(
| storage : Struct1FormerStorage, context | { 2 * ( storage.int_1.unwrap() + context.unwrap() ) }
),
Expand All @@ -146,7 +146,7 @@ tests_impls!

// custom params with into
let got = Struct1Former
::< Struct1FormerDefinition< i32, i32, former::FormingEndWrapper< Struct1FormerDefinitionTypes< i32, i32 > > > >
::< Struct1FormerDefinition< i32, i32, former::FormingEndClosure< Struct1FormerDefinitionTypes< i32, i32 > > > >
::_begin_precise
(
None,
Expand Down Expand Up @@ -188,9 +188,9 @@ tests_impls!
{
former::StoragePreform::preform( storage )
}
let end_wrapper : former::FormingEndWrapper< Struct1FormerDefinitionTypes< (), Struct1 > > = former::FormingEndWrapper::new( f1 );
let end_wrapper : former::FormingEndClosure< Struct1FormerDefinitionTypes< (), Struct1 > > = former::FormingEndClosure::new( f1 );
let got = Struct1Former
::< Struct1FormerDefinition< (), Struct1, former::FormingEndWrapper< Struct1FormerDefinitionTypes< (), Struct1 > > > >
::< Struct1FormerDefinition< (), Struct1, former::FormingEndClosure< Struct1FormerDefinitionTypes< (), Struct1 > > > >
::new( end_wrapper )
.int_1( 13 )
.form();
Expand All @@ -199,17 +199,35 @@ tests_impls!

// default explicit params with wrapper and closure
let got = Struct1Former
::< Struct1FormerDefinition< (), Struct1, former::FormingEndWrapper< Struct1FormerDefinitionTypes< (), Struct1 > > > >
::new( former::FormingEndWrapper::new( | storage, _context | { former::StoragePreform::preform( storage ) } ) )
::< Struct1FormerDefinition< (), Struct1, former::FormingEndClosure< Struct1FormerDefinitionTypes< (), Struct1 > > > >
::new( former::FormingEndClosure::new( | storage, _context | { former::StoragePreform::preform( storage ) } ) )
.int_1( 13 )
.form();
let exp = Struct1::former().int_1( 13 ).form();
a_id!( got, exp );

// default explicit params with wrapper and closure, auto types
let got = Struct1Former
::< Struct1FormerDefinition< _, _, former::FormingEndWrapper< Struct1FormerDefinitionTypes< (), Struct1 > > > >
::new( former::FormingEndWrapper::new( | storage, _context : Option< () > | { former::StoragePreform::preform( storage ) } ) )
::< Struct1FormerDefinition< _, _, former::FormingEndClosure< Struct1FormerDefinitionTypes< (), Struct1 > > > >
::new( former::FormingEndClosure::new( | storage, _context : Option< () > | { former::StoragePreform::preform( storage ) } ) )
.int_1( 13 )
.form();
let exp = Struct1::former().int_1( 13 ).form();
a_id!( got, exp );

// default explicit params with wrapper and closure
let got = Struct1Former
::< Struct1FormerWithClosure< (), Struct1 > >
::new( former::FormingEndClosure::new( | storage, _context | { former::StoragePreform::preform( storage ) } ) )
.int_1( 13 )
.form();
let exp = Struct1::former().int_1( 13 ).form();
a_id!( got, exp );

// default explicit params with wrapper and closure
let got = Struct1Former
::< Struct1FormerWithClosure< (), Struct1 > >
::new( | storage, _context | { former::StoragePreform::preform( storage ) } )
.int_1( 13 )
.form();
let exp = Struct1::former().int_1( 13 ).form();
Expand Down Expand Up @@ -244,7 +262,7 @@ tests_impls!
{
former::StoragePreform::preform( storage )
}
let end_wrapper : former::FormingEndWrapper< Struct1FormerDefinitionTypes< (), Struct1 > > = former::FormingEndWrapper::new( f1 );
let end_wrapper : former::FormingEndClosure< Struct1FormerDefinitionTypes< (), Struct1 > > = former::FormingEndClosure::new( f1 );
let got = Struct1Former
::< Struct1FormerDefinition< (), Struct1, _ > >
::_new_precise( end_wrapper )
Expand All @@ -256,7 +274,7 @@ tests_impls!
// default explicit params with wrapper and closure
let got = Struct1Former
::< Struct1FormerDefinition< (), Struct1, _ > >
::_new_precise( former::FormingEndWrapper::new( | storage, _context | { former::StoragePreform::preform( storage ) } ) )
::_new_precise( former::FormingEndClosure::new( | storage, _context | { former::StoragePreform::preform( storage ) } ) )
.int_1( 13 )
.form();
let exp = Struct1::former().int_1( 13 ).form();
Expand All @@ -265,7 +283,7 @@ tests_impls!
// default explicit params with wrapper and closure, auto types
let got = Struct1Former
::< Struct1FormerDefinition< _, _, _ > >
::_new_precise( former::FormingEndWrapper::new( | storage, _context : Option< () > | { former::StoragePreform::preform( storage ) } ) )
::_new_precise( former::FormingEndClosure::new( | storage, _context : Option< () > | { former::StoragePreform::preform( storage ) } ) )
.int_1( 13 )
.form();
let exp = Struct1::former().int_1( 13 ).form();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
TemplateParameterDefinitionFormerStorage,
TemplateParameterDefinition,
Self,
End = former::FormingEndWrapper< TemplateParameterDefinition, Self >,
End = former::FormingEndClosure< TemplateParameterDefinition, Self >,
>,
// FieldContainer : ContainerAdd,
{
Expand All @@ -78,7 +78,7 @@ where
}
super_former
};
Former2::_begin( None, Some( self ), former::FormingEndWrapper::new( on_end ) )
Former2::_begin( None, Some( self ), former::FormingEndClosure::new( on_end ) )
}

// xxx2 : move to a trait and make easier to use subformer, trait with generic interface of a container should help
Expand Down

0 comments on commit 2fb4a30

Please sign in to comment.