Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AUTO : Forward from former_friendlier to alpha #1232

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions module/core/former/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ pub struct UserProfile
impl UserProfile
{
#[ inline( always ) ]
pub fn former() -> UserProfileFormer< UserProfile, former::ReturnFormed >
pub fn former() -> UserProfileFormer< UserProfile, former::ReturnStorage >
{
UserProfileFormer::< UserProfile, former::ReturnFormed >::new()
UserProfileFormer::< UserProfile, former::ReturnStorage >::new()
}
}

Expand All @@ -103,7 +103,7 @@ pub struct UserProfileFormerStorage
pub struct UserProfileFormer
<
Context = UserProfile,
End = former::ReturnFormed,
End = former::ReturnStorage,
>
where
End : former::FormingEnd< UserProfile, Context >,
Expand Down Expand Up @@ -205,9 +205,9 @@ where
}

#[ inline( always ) ]
pub fn new() -> UserProfileFormer< UserProfile, former::ReturnFormed >
pub fn new() -> UserProfileFormer< UserProfile, former::ReturnStorage >
{
UserProfileFormer::< UserProfile, former::ReturnFormed >::begin( None, former::ReturnFormed )
UserProfileFormer::< UserProfile, former::ReturnStorage >::begin( None, former::ReturnStorage )
}

#[ inline( always ) ]
Expand All @@ -226,7 +226,7 @@ where
}

#[ inline( always ) ]
pub fn end( mut self ) -> Context
pub fn end( mut self ) -> Formed
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
Expand Down
12 changes: 6 additions & 6 deletions module/core/former/examples/former_trivial_expaned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ fn main()
impl UserProfile
{
#[ inline( always ) ]
pub fn former() -> UserProfileFormer< UserProfile, former::ReturnFormed >
pub fn former() -> UserProfileFormer< UserProfile, former::ReturnStorage >
{
UserProfileFormer::< UserProfile, former::ReturnFormed >::new()
UserProfileFormer::< UserProfile, former::ReturnStorage >::new()
}
}

Expand All @@ -55,7 +55,7 @@ fn main()
pub struct UserProfileFormer
<
Context = UserProfile,
End = former::ReturnFormed,
End = former::ReturnStorage,
>
where
End : former::FormingEnd< UserProfile, Context >,
Expand Down Expand Up @@ -157,9 +157,9 @@ fn main()
}

#[ inline( always ) ]
pub fn new() -> UserProfileFormer< UserProfile, former::ReturnFormed >
pub fn new() -> UserProfileFormer< UserProfile, former::ReturnStorage >
{
UserProfileFormer::< UserProfile, former::ReturnFormed >::begin( None, former::ReturnFormed )
UserProfileFormer::< UserProfile, former::ReturnStorage >::begin( None, former::ReturnStorage )
}

#[ inline( always ) ]
Expand All @@ -178,7 +178,7 @@ fn main()
}

#[ inline( always ) ]
pub fn end( mut self ) -> Context
pub fn end( mut self ) -> Formed
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
Expand Down
165 changes: 85 additions & 80 deletions module/core/former/src/axiomatic.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
//! ....

/// 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.
/// Implementors can define how to transform or pass through the context during the forming process's completion.
///
/// # Parameters
/// - `Formed`: The type of the container being processed.
/// - `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< Formed, Context >
pub trait FormingEnd< Former : FormerDescriptor, Context >
{
/// Called at the end of the subforming process to return the modified or original context.
///
Expand All @@ -18,21 +32,40 @@ pub trait FormingEnd< Formed, Context >
///
/// # Returns
/// Returns the transformed or original context based on the implementation.
#[ allow( dead_code ) ]
fn call( &self, storage : Formed, context : core::option::Option< Context > ) -> Context;
// #[ allow( dead_code ) ]
fn call( &self, storage : Former::Storage, context : core::option::Option< Context > ) -> Former::Formed;
}

impl< Storage, Context, F > FormingEnd< Storage, Context > for F
impl< Former : FormerDescriptor, Context, F > FormingEnd< Former, Context > for F
where
F : Fn( Storage, core::option::Option< Context > ) -> Context,
F : Fn( Former::Storage, core::option::Option< Context > ) -> Former::Formed,
{
#[ inline( always ) ]
fn call( &self, storage : Storage, context : core::option::Option< Context > ) -> Context
fn call( &self, storage : Former::Storage, context : core::option::Option< Context > ) -> Former::Formed
{
self( storage, context )
}
}

/// A `FormingEnd` implementation that returns the formed container itself instead of the context.
///
/// This struct is useful when the forming process should result in the formed container being returned directly,
/// bypassing any additional context processing. It simplifies scenarios where the formed container is the final result.
#[ derive( Debug, Default ) ]
pub struct ReturnStorage;

impl< Former : FormerDescriptor > FormingEnd< Former, () >
for ReturnStorage
// where
// Storage : StoragePreform<>,
{
#[ inline( always ) ]
fn call( &self, storage : Former::Storage, _context : core::option::Option< () > ) -> Former::Formed
{
storage.preform()
}
}

/// A wrapper around a closure to be used as a `FormingEnd`.
///
/// This struct allows for dynamic dispatch of a closure that matches the
Expand All @@ -47,14 +80,14 @@ where
/// * `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 >
pub struct FormingEndWrapper< Former : FormerDescriptor, Context >
{
closure : Box< dyn Fn( Storage, Option< Context > ) -> Context >,
_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 > FormingEndWrapper< Storage, Context >
impl< Former : FormerDescriptor, Context > FormingEndWrapper< Former, Context >
{
/// Constructs a new `FormingEndWrapper` with the provided closure.
///
Expand All @@ -67,7 +100,7 @@ impl< Storage, Context > FormingEndWrapper< Storage, Context >
/// # Returns
///
/// Returns an instance of `FormingEndWrapper` encapsulating the provided closure.
pub fn new( closure : impl Fn( Storage, Option< Context > ) -> Context + 'static ) -> Self
pub fn new( closure : impl Fn( Former::Storage, Option< Context > ) -> Former::Formed + 'static ) -> Self
{
Self
{
Expand All @@ -80,7 +113,7 @@ impl< Storage, Context > FormingEndWrapper< Storage, Context >
#[ cfg( not( feature = "no_std" ) ) ]
use std::fmt;
#[ cfg( not( feature = "no_std" ) ) ]
impl< Storage, Context > fmt::Debug for FormingEndWrapper< Storage, Context >
impl< Former : FormerDescriptor, Context > fmt::Debug for FormingEndWrapper< Former, Context >
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
Expand All @@ -92,96 +125,68 @@ impl< Storage, Context > fmt::Debug for FormingEndWrapper< Storage, Context >
}

#[ cfg( not( feature = "no_std" ) ) ]
impl< Storage, Context > FormingEnd< Storage, Context >
for FormingEndWrapper< Storage, Context >
impl< Former : FormerDescriptor, Context > FormingEnd< Former, Context >
for FormingEndWrapper< Former, Context >
{
fn call( &self, storage : Storage, context : Option< Context > ) -> Context
fn call( &self, storage : Former::Storage, context : Option< Context > ) -> Former::Formed
{
( self.closure )( storage, 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 formed container itself instead of the context.
/// A trait for initiating a structured subforming process with contextual and intermediary storage linkage.
///
/// This struct is useful when the forming process should result in the formed container being returned directly,
/// bypassing any additional context processing. It simplifies scenarios where the formed container is the final result.
#[ derive( Debug, Default ) ]
pub struct ReturnFormed;

impl< Storage > FormingEnd< Storage, Storage >
for ReturnFormed
{
#[ inline( always ) ]
fn call( &self, storage : Storage, _context : core::option::Option< Storage > ) -> Storage
{
storage
}
}

//

/// A trait defining the initialization process for a subformer with contextual linkage.
///
/// This trait is designed for types that need to initiate a subforming process,
/// passing themselves as the context and specifying a closure or handler (`on_end`) to be
/// called upon completion. It facilitates the construction of builder pattern chains
/// that maintain stateful context through each step of the process.
/// This trait facilitates the creation of a subformer that carries through a builder pattern chain,
/// utilizing intermediary storage for accumulating state or data before finally transforming it into
/// a `Formed` structure. It is designed for scenarios where a multi-step construction or transformation
/// process benefits from maintaining both transient state (`Storage`) and contextual information (`Context`),
/// before concluding with the generation of a final product (`Formed`).
///
/// # Type Parameters
///
/// * `Formed` - Represents the type that is being constructed or transformed by the subformer.
/// * `Context` - Denotes the contextual information or the environment in which `Formed` is being formed.
/// This could be a reference to a parent builder, configuration settings, or any other
/// relevant state.
/// * `Storage` - Represents a mutable intermediary storage structure used throughout the subforming process
/// to accumulate data, state, or partial computations. This storage is internal to the
/// subformer and is eventually converted into the final `Formed` structure by the subformer,
/// not directly by implementations of this trait.
///
/// * `Formed` - Denotes the final type that results from the subforming process. This is the intended outcome
/// of the builder chain, constructed or transformed from the `Storage` with consideration of
/// the provided `Context`.
///
/// # Associated Types
/// * `Context` - Specifies the contextual backdrop against which the subforming process unfolds. This could
/// encompass references to parent builders, configuration data, or any state influencing how
/// `Storage` transitions into `Formed`.
///
/// * `End` - Specifies the trait bound for the closure or handler that gets called at the completion
/// of the subforming process. This type must implement the `FormingEnd<Formed, Context>`
/// trait, which defines how the final transformation or construction of `Formed` is handled,
/// potentially using the provided `Context`.
/// # Functions
///
/// * `_begin` - This function launches the subforming process, marking the start of a construction or transformation
/// sequence defined by the implementing type. It establishes the foundational `Storage` and `Context`,
/// alongside specifying an `on_end` completion handler that dictates the final conversion into `Formed`.
///
/// The `FormerBegin` trait, by decoupling `Storage` from `Formed` and introducing a contextual layer, enables
/// sophisticated and flexible construction patterns conducive to complex data transformations or object creation
/// sequences within builder patterns.

// xxx2 : change sequence
pub trait FormerBegin< Storage, Formed, Context >
{

/// * `End` - Specifies the trait bound for the closure or handler that gets called at the completion
/// of the subforming process. This type must implement the `FormingEnd<Formed, Context>`
/// trait, which defines how the final transformation or construction of `Formed` is handled,
/// potentially using the provided `Context`.
type End : FormingEnd< Formed, Context >;
/// * `End` - A trait bound marking the closure or handler invoked upon completing the subforming process. Implementers
/// of this trait (`End`) are tasked with applying the final transformations that transition `Storage`
/// into `Formed`, optionally utilizing `Context` to guide this transformation. It is crucial that this
/// associated type satisfies the `FormingEnd<Formed, Context>` trait, defining the precise mechanics of
/// how the subformer concludes its operation.
type End : FormingEnd< Storage, Context, Formed >;

/// Initializes the subforming process by setting the context and specifying an `on_end` completion handler.
///
/// This function is the entry point for initiating a subforming sequence, allowing the caller
/// to establish initial contextual information and define how the process concludes.
/// Launches the subforming process with an initial storage and context, setting up an `on_end` completion handler.
///
/// # Parameters
///
/// * `context` - An optional parameter providing initial context for the subforming process. This
/// might include configuration data, references to parent structures, or any state
/// relevant to the formation of `Formed`.
///
/// * `on_end` - A closure or handler of type `Self::End` that is invoked at the completion of
/// the subforming process. This handler is responsible for applying any final transformations
/// to `Formed` and potentially utilizing `Context` to influence the outcome.
///
/// * `storage` - An optional initial state for the intermediary storage structure.
/// * `context` - An optional initial setting providing contextual information for the subforming process.
/// * `on_end` - A completion handler responsible for transforming the accumulated `Storage` into the final `Formed` structure.
fn _begin
(
storage : core::option::Option< Storage >,
Expand Down
Loading
Loading