Skip to content

Commit

Permalink
experimenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Mar 27, 2024
1 parent 630816a commit 55c1058
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 144 deletions.
4 changes: 2 additions & 2 deletions module/core/former/src/axiomatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ pub struct NoEnd;
impl< Definition > FormingEnd< Definition >
for NoEnd
where
Definition : FormerDefinitionTypes< Context = () >,
Definition : FormerDefinitionTypes,
{
#[ inline( always ) ]
fn call( &self, storage : Definition::Storage, _context : core::option::Option< () > ) -> Definition::Formed
fn call( &self, storage : Definition::Storage, _context : core::option::Option< Definition::Context > ) -> Definition::Formed
{
unreachable!();
}
Expand Down
317 changes: 178 additions & 139 deletions module/core/former/src/hash_map.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,105 @@
// use super::*;
//
// use collection_tools::HashMap;
//
// /// A trait for types that behave like hash maps, supporting insertion and custom forming behaviors.
// ///
// /// This trait allows for generic operations on hash map-like data structures, enabling the insertion
// /// of key-value pairs and the creation of formers for more complex construction patterns.
// ///
// /// # Type Parameters
// /// - `K`: The type of keys stored in the hash map. Must implement `Eq` and `Hash`.
// /// - `E`: The type of elements (values) stored in the hash map.
// pub trait HashMapLike< K, E >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// Self : Sized + Default,
// {
//
// /// Inserts a key-value pair into the map.
// fn insert( &mut self, k : K, e : E ) -> Option< E >;
//
// // /// Return former.
// // #[ inline( always ) ]
// // fn former< Definition : FormerDefinitionTypes >( self )
// // ->
// // HashMapSubformer< K, E, Definition, (), impl FormingEnd< Self, Self > >
// // {
// // HashMapSubformer::begin( Some( self ), None, ReturnFormed )
// // }
// // xxx : uncomment and cover by tests
//
// }
//
// impl< K, E > HashMapLike< K, E > for HashMap< K, E >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// Self : Sized + Default,
// {
//
// #[ inline( always ) ]
// fn insert( &mut self, k : K, e : E ) -> Option< E >
// {
// HashMap::insert( self, k, e )
// }
//
// }
//
// // = storage
//
// impl< K, E > Storage
// for HashMap< K, E >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// {
// // type Types = HashMapDefinition< K, E >;
// type Formed = HashMap< K, E >;
// }
//
// impl< K, E > StoragePerform
// for HashMap< K, E >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// {
// // fn preform( self ) -> < < Self as Storage >::Definition as FormerDefinitionTypes >::Formed
// fn preform( self ) -> Self::Formed
// {
// self
// }
// }
//
// // = definition
//
use super::*;

use collection_tools::HashMap;

/// A trait for types that behave like hash maps, supporting insertion and custom forming behaviors.
///
/// This trait allows for generic operations on hash map-like data structures, enabling the insertion
/// of key-value pairs and the creation of formers for more complex construction patterns.
///
/// # Type Parameters
/// - `K`: The type of keys stored in the hash map. Must implement `Eq` and `Hash`.
/// - `E`: The type of elements (values) stored in the hash map.
pub trait HashMapLike< K, E >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
Self : Sized + Default,
{

/// Inserts a key-value pair into the map.
fn insert( &mut self, k : K, e : E ) -> Option< E >;

// /// Return former.
// #[ inline( always ) ]
// fn former< Definition : FormerDefinitionTypes >( self )
// ->
// HashMapSubformer< K, E, Definition, (), impl FormingEnd< Self, Self > >
// {
// HashMapSubformer::begin( Some( self ), None, ReturnFormed )
// }
// xxx : uncomment and cover by tests

}

impl< K, E > HashMapLike< K, E > for HashMap< K, E >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
Self : Sized + Default,
{

#[ inline( always ) ]
fn insert( &mut self, k : K, e : E ) -> Option< E >
{
HashMap::insert( self, k, e )
}

}

// = storage

impl< K, E > Storage
for HashMap< K, E >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
// type Types = HashMapDefinition< K, E >;
type Formed = HashMap< K, E >;
}

impl< K, E > StoragePerform
for HashMap< K, E >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
// fn preform( self ) -> < < Self as Storage >::Definition as FormerDefinitionTypes >::Formed
fn preform( self ) -> Self::Formed
{
self
}
}

// = definition

#[ derive( Debug, Default ) ]
pub struct HashMapDefinition< K, E, Context = (), Formed = HashMap< K, E >, End = ReturnStorage >
{
_phantom : core::marker::PhantomData< ( K, E, Context, Formed, End ) >,
}

impl< K, E, Context, Formed > FormerDefinitionTypes
for HashMapDefinition< K, E, Context, Formed, NoEnd >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
type Storage = HashMap< K, E >;
type Formed = Formed;
type Context = Context;
}

impl< K, E, Context, Formed, End > FormerDefinition
for HashMapDefinition< K, E, Context, Formed, End >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
End : FormingEnd< HashMapDefinition< K, E, Context, Formed, NoEnd > >,
{
type Types = HashMapDefinition< K, E, Context, Formed, NoEnd >;
type End = End;
}

// #[ derive( Debug, Default ) ]
// pub struct HashMapDefinition< K, E, Context = (), Formed = HashMap< K, E >, End = ReturnStorage >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// End : FormingEnd< Self >,
// {
// _phantom : ::core::marker::PhantomData< ( K, E, Context, End ) >,
// }
Expand All @@ -91,73 +116,87 @@
// type End = End;
// }
//
// /// A builder for constructing hash map-like structures with a fluent interface.
// ///
// /// `HashMapSubformer` leverages the `HashMapLike` trait to enable a flexible and customizable
// /// way to build hash map-like structures. It supports the chaining of insert operations and
// /// allows for the definition of custom end actions to finalize the building process.
// ///
// /// # Type Parameters
// /// - `K`: Key type, must implement `Eq` and `Hash`.
// /// - `E`: Element (value) type.
// /// - `Formed`: The hash map-like formed being built.
// /// - `Context`: Type of the optional context used during the building process.
// /// - `End`: End-of-forming action to be executed upon completion.
// ///
// /// # Examples
// /// ```
// /// # #[ cfg( all( feature = "enabled", not( feature = "no_std" ) ) ) ]
// /// # {
// /// # use test_tools::exposed::*;
// ///
// /// #[ derive( Debug, PartialEq, former::Former ) ]
// /// pub struct StructWithMap
// /// {
// /// #[ subformer( former::HashMapSubformer ) ]
// /// map : std::collections::HashMap< &'static str, &'static str >,
// /// }
// ///
// /// let struct1 = StructWithMap::former()
// /// .map()
// /// .insert( "a", "b" )
// /// .insert( "c", "d" )
// /// .end()
// /// .form()
// /// ;
// /// assert_eq!( struct1, StructWithMap { map : hmap!{ "a" => "b", "c" => "d" } } );
// ///
// /// # }
// /// ```
//
// // pub type HashMapSubformer< K, E, Context, End > = ContainerSubformer::< ( K, E ), HashMapDefinition< K, E, Context, End > >;
//
// // xxx : update documentation
// // pub type HashMapSubformer< K, E, Context, End > = ContainerSubformer::< K, HashMapDefinition< K, Context, End > >;
// pub type HashMapSubformer< K, E, Context, Formed, End > =
// ContainerSubformer::< K, HashMapDefinition< K, Context, Formed, End > >;
//
// // = extension
//
// pub trait HashMapExt< K, E > : sealed::Sealed
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// {
// fn former() -> HashMapSubformer< K, E, (), HashMap< K, E >, ReturnStorage >;
// }
//
// impl< K, E > HashMapExt< K, E > for HashMap< K, E >
// impl< K, E, Context, End > FormerDefinitionTypes
// for HashMapDefinition< K, E, Context, End >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// End : FormingEnd< Self >,
// {
// fn former() -> HashMapSubformer< K, E, (), HashMap< K, E >, ReturnStorage >
// {
// HashMapSubformer::< K, (), HashMap< K, E >, ReturnStorage >::new( ReturnStorage::default() )
// }
// }
//
// mod sealed
// {
// use super::HashMap;
// pub trait Sealed {}
// impl< K, E > Sealed for HashMap< K, E > {}
// type Storage = HashMap< K, E >;
// type Formed = HashMap< K, E >;
// type Context = Context;
// type End = End;
// }

// = subformer

/// A builder for constructing hash map-like structures with a fluent interface.
///
/// `HashMapSubformer` leverages the `HashMapLike` trait to enable a flexible and customizable
/// way to build hash map-like structures. It supports the chaining of insert operations and
/// allows for the definition of custom end actions to finalize the building process.
///
/// # Type Parameters
/// - `K`: Key type, must implement `Eq` and `Hash`.
/// - `E`: Element (value) type.
/// - `Formed`: The hash map-like formed being built.
/// - `Context`: Type of the optional context used during the building process.
/// - `End`: End-of-forming action to be executed upon completion.
///
/// # Examples
/// ```
/// # #[ cfg( all( feature = "enabled", not( feature = "no_std" ) ) ) ]
/// # {
/// # use test_tools::exposed::*;
///
/// #[ derive( Debug, PartialEq, former::Former ) ]
/// pub struct StructWithMap
/// {
/// #[ subformer( former::HashMapSubformer ) ]
/// map : std::collections::HashMap< &'static str, &'static str >,
/// }
///
/// let struct1 = StructWithMap::former()
/// .map()
/// .insert( "a", "b" )
/// .insert( "c", "d" )
/// .end()
/// .form()
/// ;
/// assert_eq!( struct1, StructWithMap { map : hmap!{ "a" => "b", "c" => "d" } } );
///
/// # }
/// ```
// pub type HashMapSubformer< K, E, Context, End > = ContainerSubformer::< ( K, E ), HashMapDefinition< K, E, Context, End > >;

// xxx : update documentation
// pub type HashMapSubformer< K, E, Context, End > = ContainerSubformer::< K, HashMapDefinition< K, E, Context, End > >;
pub type HashMapSubformer< K, E, Context, Formed, End > =
ContainerSubformer::< ( K, E ), HashMapDefinition< K, E, Context, Formed, End > >;

// = extension

pub trait HashMapExt< K, E > : sealed::Sealed
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
fn former() -> HashMapSubformer< K, E, (), HashMap< K, E >, ReturnStorage >;
}

impl< K, E > HashMapExt< K, E > for HashMap< K, E >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
fn former() -> HashMapSubformer< K, E, (), HashMap< K, E >, ReturnStorage >
{
HashMapSubformer::< K, E, (), HashMap< K, E >, ReturnStorage >::new( ReturnStorage::default() )
}
}

mod sealed
{
use super::HashMap;
pub trait Sealed {}
impl< K, E > Sealed for HashMap< K, E > {}
}
2 changes: 2 additions & 0 deletions module/core/former/src/hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ where
type End = End;
}

// = subformer

/// Facilitates building `HashSetLike` containers with a fluent API.
///
/// `HashSetSubformer` leverages the `HashSetLike` trait to enable a concise and expressive way
Expand Down
5 changes: 2 additions & 3 deletions module/core/former/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ for Vec< E >

#[ derive( Debug, Default ) ]
pub struct VectorDefinition< E, Context = (), Formed = Vec< E >, End = ReturnStorage >
where
End : FormingEnd< VectorDefinition< E, Context, Formed, NoEnd > >,
// axiomatic::NoEnd: axiomatic::FormingEnd<VectorDefinition<E, Context, Formed, axiomatic::NoEnd>>,
// where
// End : FormingEnd< VectorDefinition< E, Context, Formed, NoEnd > >,
{
_phantom : core::marker::PhantomData< ( E, Context, Formed, End ) >,
}
Expand Down

0 comments on commit 55c1058

Please sign in to comment.