Skip to content

Commit

Permalink
former : making subforming more friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Mar 21, 2024
1 parent c9135fd commit 50d2580
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 33 deletions.
46 changes: 30 additions & 16 deletions module/core/former/src/axiomatic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
//! ....

/// xxx2
pub trait StoragePerform
{
type Formed;
fn preform( self ) -> Self::Formed;
}

/// xxx2
pub trait FormerDescriptor
{
type Storage : StoragePerform< Formed = Self::Formed >;
type Formed;
}

/// Defines a handler for the end of a subforming process, enabling the return of the original context.
///
/// This trait is designed to be flexible, allowing for various end-of-forming behaviors in builder patterns.
Expand All @@ -8,7 +22,7 @@
/// # Parameters
/// - `Storage`: The type of the container being processed.
/// - `Context`: The type of the context that might be altered or returned upon completion.
pub trait FormingEnd< Storage, Context, Formed >
pub trait FormingEnd< Former : FormerDescriptor, Context >
{
/// Called at the end of the subforming process to return the modified or original context.
///
Expand All @@ -19,15 +33,15 @@ pub trait FormingEnd< Storage, Context, Formed >
/// # Returns
/// Returns the transformed or original context based on the implementation.
// #[ allow( dead_code ) ]
fn call( &self, storage : Storage, context : core::option::Option< Context > ) -> Formed;
fn call( &self, storage : Former::Storage, context : core::option::Option< Context > ) -> Former::Formed;
}

impl< Storage, Context, Formed, F > FormingEnd< Storage, Context, Formed > for F
impl< Former : FormerDescriptor, Context, F > FormingEnd< Former, Context > for F
where
F : Fn( Storage, core::option::Option< Context > ) -> Formed,
F : Fn( Former::Storage, core::option::Option< Context > ) -> Former::Formed,
{
#[ inline( always ) ]
fn call( &self, storage : Storage, context : core::option::Option< Context > ) -> Formed
fn call( &self, storage : Former::Storage, context : core::option::Option< Context > ) -> Former::Formed
{
self( storage, context )
}
Expand All @@ -40,13 +54,13 @@ where
#[ derive( Debug, Default ) ]
pub struct ReturnStorage;

impl< Storage, Formed > FormingEnd< Storage, (), Formed >
impl< Former : FormerDescriptor > FormingEnd< Former, () >
for ReturnStorage
// where
// Storage : StoragePreform<>,
{
#[ inline( always ) ]
fn call( &self, storage : Storage, _context : core::option::Option< () > ) -> Formed
fn call( &self, storage : Former::Storage, _context : core::option::Option< () > ) -> Former::Formed
{
storage.preform()
}
Expand All @@ -66,14 +80,14 @@ for ReturnStorage
/// * `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< Storage, Context, Formed >
pub struct FormingEndWrapper< Former : FormerDescriptor, Context >
{
closure : Box< dyn Fn( Storage, Option< Context > ) -> Formed >,
_marker : std::marker::PhantomData< Storage >,
closure : Box< dyn Fn( Former::Storage, Option< Context > ) -> Former::Formed >,
_marker : std::marker::PhantomData< Former::Storage >,
}

#[ cfg( not( feature = "no_std" ) ) ]
impl< Storage, Context, Formed > FormingEndWrapper< Storage, Context, Formed >
impl< Former : FormerDescriptor, Context > FormingEndWrapper< Former, Context >
{
/// Constructs a new `FormingEndWrapper` with the provided closure.
///
Expand All @@ -86,7 +100,7 @@ impl< Storage, Context, Formed > FormingEndWrapper< Storage, Context, Formed >
/// # Returns
///
/// Returns an instance of `FormingEndWrapper` encapsulating the provided closure.
pub fn new( closure : impl Fn( Storage, Option< Context > ) -> Formed + 'static ) -> Self
pub fn new( closure : impl Fn( Former::Storage, Option< Context > ) -> Former::Formed + 'static ) -> Self
{
Self
{
Expand All @@ -99,7 +113,7 @@ impl< Storage, Context, Formed > FormingEndWrapper< Storage, Context, Formed >
#[ cfg( not( feature = "no_std" ) ) ]
use std::fmt;
#[ cfg( not( feature = "no_std" ) ) ]
impl< Storage, Context, Formed > fmt::Debug for FormingEndWrapper< Storage, Context, Formed >
impl< Former : FormerDescriptor, Context > fmt::Debug for FormingEndWrapper< Former, Context >
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
Expand All @@ -111,10 +125,10 @@ impl< Storage, Context, Formed > fmt::Debug for FormingEndWrapper< Storage, Cont
}

#[ cfg( not( feature = "no_std" ) ) ]
impl< Storage, Context, Formed > FormingEnd< Storage, Context, Formed >
for FormingEndWrapper< Storage, Context, Formed >
impl< Former : FormerDescriptor, Context > FormingEnd< Former, Context >
for FormingEndWrapper< Former, Context >
{
fn call( &self, storage : Storage, context : Option< Context > ) -> Formed
fn call( &self, storage : Former::Storage, context : Option< Context > ) -> Former::Formed
{
( self.closure )( storage, context )
}
Expand Down
60 changes: 43 additions & 17 deletions module/core/former/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ impl< E > VectorLike< E > for Vec< E >
}
}

impl< E > StoragePerform for VectorLike< E >
where
Self : Sized,
{
type Formed = Self;
fn preform( self ) -> Self::Formed
{
self
}
}

impl< E, Former, Formed, Context, End, > FormerDescriptor
for VectorSubformer< Former, Context, End >
where
Formed : StoragePerform< Formed = Formed > + VectorLike< E > + core::default::Default,
End : FormingEnd< Self, Context >,
Former : FormerDescriptor,
{
type Storage = Formed;
type Formed = Formed;
}

/// A builder for constructing `VectorLike` containers, facilitating a fluent and flexible interface.
///
/// `VectorSubformer` leverages the `VectorLike` trait to enable the construction and manipulation
Expand All @@ -48,26 +70,30 @@ impl< E > VectorLike< E > for Vec< E >
///
// xxx2 : change sequence of parameters
#[ derive( Debug, Default ) ]
pub struct VectorSubformer< E, Formed, Context, ContainerEnd >
// pub struct VectorSubformer< E, Formed, Context, End >
pub struct VectorSubformer< Former, Context, End >
where
Formed : VectorLike< E > + core::default::Default,
ContainerEnd : FormingEnd< Formed, Context, Formed >,
// Formed : StoragePerform< Formed = Formed > + VectorLike< E > + core::default::Default,
Former : FormerDescriptor,
End : FormingEnd< Self, Context >,
{
formed : core::option::Option< Formed >,
formed : core::option::Option< Former::Formed >,
context : core::option::Option< Context >,
on_end : core::option::Option< ContainerEnd >,
_phantom : core::marker::PhantomData< E >,
on_end : core::option::Option< End >,
// _phantom : core::marker::PhantomData< E >,
}

impl< E, Formed, Context, ContainerEnd > VectorSubformer< E, Formed, Context, ContainerEnd >
impl< Former, Context, End > VectorSubformer< Former, Context, End >
where
Formed : VectorLike< E > + core::default::Default,
ContainerEnd : FormingEnd< Formed, Context, Formed >,
Former : FormerDescriptor,
End : FormingEnd< Self, Context >,
// Formed : VectorLike< E > + core::default::Default,
// End : FormingEnd< Self, Context >,
{

/// Form current former into target structure.
#[ inline( always ) ]
pub fn form( mut self ) -> Formed
pub fn form( mut self ) -> Former::Formed
{
let formed = if self.formed.is_some()
{
Expand All @@ -85,9 +111,9 @@ where
#[ inline( always ) ]
pub fn begin
(
formed : core::option::Option< Formed >,
formed : core::option::Option< Former::Formed >,
context : core::option::Option< Context >,
on_end : ContainerEnd
on_end : End
) -> Self
{
Self
Expand All @@ -101,7 +127,7 @@ where

/// Finalizes the building process, returning the formed or a context incorporating it.
#[ inline( always ) ]
pub fn end( mut self ) -> Formed
pub fn end( mut self ) -> Former::Formed
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
Expand All @@ -111,7 +137,7 @@ where

/// Replaces the current formed with a provided one, allowing for a reset or redirection of the building process.
#[ inline( always ) ]
pub fn replace( mut self, vector : Formed ) -> Self
pub fn replace( mut self, vector : Former::Formed ) -> Self
{
self.formed = Some( vector );
self
Expand Down Expand Up @@ -143,10 +169,10 @@ where

}

impl< E, Formed, Context, ContainerEnd > VectorSubformer< E, Formed, Context, ContainerEnd >
impl< E, Formed, Context, End > VectorSubformer< E, Formed, Context, End >
where
Formed : VectorLike< E > + core::default::Default,
ContainerEnd : FormingEnd< Formed, Context, Formed >,
End : FormingEnd< Self, Context >,
{

/// Appends an element to the end of the formed, expanding the internal collection.
Expand All @@ -172,7 +198,7 @@ where
impl< E, Formed, Context, End > FormerBegin< Formed, Formed, Context >
for VectorSubformer< E, Formed, Context, End >
where
End : FormingEnd< Formed, Context, Formed >,
End : FormingEnd< Self, Context >,
Formed : VectorLike< E > + Default,
{
type End = End;
Expand Down

0 comments on commit 50d2580

Please sign in to comment.