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 8c8363f commit 630816a
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 188 deletions.
48 changes: 34 additions & 14 deletions module/core/former/src/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,15 @@
//
// // = definition
//
// #[ derive( Debug ) ]
// pub struct HashMapDefinition< K, E, Context, 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 ) >,
// }
//
// impl< K, E, Context, End > HashMapDefinition< K, E, Context, End >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// End : FormingEnd< Self >,
// {
// pub fn new() -> Self
// {
// Self { _phantom : ::core::marker::PhantomData }
// }
// }
//
// impl< K, E, Context, End > FormerDefinitionTypes
// for HashMapDefinition< K, E, Context, End >
// where
Expand Down Expand Up @@ -140,4 +129,35 @@
// /// # }
// /// ```
//
// pub type HashMapSubformer< K, E, Context, End > = ContainerSubformer::< ( K, E ), HashMapDefinition< K, E, Context, End > >;
// // 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 >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// {
// 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 > {}
// }
317 changes: 148 additions & 169 deletions module/core/former/src/hash_set.rs
Original file line number Diff line number Diff line change
@@ -1,169 +1,148 @@
// //! # HashSetLike Trait and HashSetSubformer Struct
// //!
// //! This part of the crate provides a flexible interface (`HashSetLike`) and a builder pattern implementation (`HashSetSubformer`) for `HashSet`-like containers. It's designed to extend the builder pattern, allowing for fluent and dynamic construction of sets within custom data structures.
//
// use super::*;
// use collection_tools::HashSet;
//
// /// A trait for containers behaving like a `HashSet`, allowing insertion operations.
// ///
// /// Implementing this trait enables the associated formed to be used with `HashSetSubformer`,
// /// facilitating a builder pattern that is both intuitive and concise.
// ///
// /// # Example Implementation
// ///
// /// Implementing `HashSetLike` for `std::collections::HashSet`:
// ///
//
// pub trait HashSetLike< K >
// where
// K : core::cmp::Eq + core::hash::Hash,
// {
// /// Inserts a key-value pair into the map.
// fn insert( &mut self, element : K ) -> Option< K >;
// }
//
// impl< K > HashSetLike< K > for HashSet< K >
// where
// K : core::cmp::Eq + core::hash::Hash,
// {
// fn insert( &mut self, element : K ) -> Option< K >
// {
// HashSet::replace( self, element )
// }
// }
//
// // = storage
//
// impl< K > Storage
// for HashSet< K >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// {
// // type Types = HashSetDefinition< K >;
// type Formed = HashSet< K >;
// }
//
// impl< K > StoragePerform
// for HashSet< K >
// 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 HashSetDefinition< K, Context, End >
// // where
// // K : ::core::cmp::Eq + ::core::hash::Hash,
// // End : FormingEnd< Self >,
// // {
// // _phantom : ::core::marker::PhantomData< ( K, Context, End ) >,
// // }
//
// #[ derive( Debug, Default ) ]
// pub struct HashSetDefinition< K, Context = (), Formed = HashSet< K >, End = ReturnStorage >
// {
// _phantom : core::marker::PhantomData< ( K, Context, Formed, End ) >,
// }
//
// // impl< K, Context, End > HashSetDefinition< K, Context, End >
// // where
// // K : ::core::cmp::Eq + ::core::hash::Hash,
// // End : FormingEnd< Self >,
// // {
// // pub fn new() -> Self
// // {
// // Self { _phantom : ::core::marker::PhantomData }
// // }
// // }
//
// impl< K, Context, Formed > FormerDefinitionTypes
// for HashSetDefinition< K, Context, Formed >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// // End : FormingEnd< Self >,
// {
// type Storage = HashSet< K >;
// type Formed = Formed;
// type Context = Context;
// }
//
// impl< K, Context, Formed, End > FormerDefinition
// for HashSetDefinition< K, Context, Formed, End >
// where
// K : ::core::cmp::Eq + ::core::hash::Hash,
// End : FormingEnd< Self >,
// {
// type Types = HashSetDefinition< K, Context, Formed >;
// type End = End;
// }
//
// /// Facilitates building `HashSetLike` containers with a fluent API.
// ///
// /// `HashSetSubformer` leverages the `HashSetLike` trait to enable a concise and expressive way
// /// of populating `HashSet`-like containers. It exemplifies the crate's builder pattern variation for sets.
// ///
// /// # Example Usage
// ///
// /// Using `HashSetSubformer` to populate a `HashSet` within a struct:
// ///
// /// ```rust
// /// # #[ cfg( all( feature = "enabled", not( feature = "no_std" ) ) ) ]
// /// # {
// /// # use test_tools::exposed::*;
// ///
// /// #[ derive( Debug, PartialEq, former::Former ) ]
// /// pub struct StructWithSet
// /// {
// /// #[ subformer( former::HashSetSubformer ) ]
// /// set : std::collections::HashSet< &'static str >,
// /// }
// ///
// /// let instance = StructWithSet::former()
// /// .set()
// /// .insert( "apple" )
// /// .insert( "banana" )
// /// .end()
// /// .form();
// ///
// /// assert_eq!(instance, StructWithSet { set : hset![ "apple", "banana" ] });
// /// # }
// /// ```
//
// // xxx : update documentation
// // pub type HashSetSubformer< K, Context, End > = ContainerSubformer::< K, HashSetDefinition< K, Context, End > >;
// pub type HashSetSubformer< K, Context, Formed, End > =
// ContainerSubformer::< K, HashSetDefinition< K, Context, Formed, End > >;
//
// // = extension
//
// // pub trait HashSetExt< K > : sealed::Sealed
// // where
// // K : ::core::cmp::Eq + ::core::hash::Hash,
// // {
// // fn former() -> HashSetSubformer< K, (), HashSet< K >, ReturnStorage >;
// // }
//
// // impl< K > HashSetExt< K > for HashSet< K >
// // where
// // K : ::core::cmp::Eq + ::core::hash::Hash,
// // {
// // fn former() -> HashSetSubformer< K, (), HashSet< K >, ReturnStorage >
// // {
// // HashSetSubformer::< K, (), HashSet< K >, ReturnStorage >::new( ReturnStorage::default() )
// // }
// // }
//
// mod sealed
// {
// use super::HashSet;
// pub trait Sealed {}
// impl< K > Sealed for HashSet< K > {}
// }
//! # HashSetLike Trait and HashSetSubformer Struct
//!
//! This part of the crate provides a flexible interface (`HashSetLike`) and a builder pattern implementation (`HashSetSubformer`) for `HashSet`-like containers. It's designed to extend the builder pattern, allowing for fluent and dynamic construction of sets within custom data structures.

use super::*;
use collection_tools::HashSet;

/// A trait for containers behaving like a `HashSet`, allowing insertion operations.
///
/// Implementing this trait enables the associated formed to be used with `HashSetSubformer`,
/// facilitating a builder pattern that is both intuitive and concise.
///
/// # Example Implementation
///
/// Implementing `HashSetLike` for `std::collections::HashSet`:
///

pub trait HashSetLike< K >
where
K : core::cmp::Eq + core::hash::Hash,
{
/// Inserts a key-value pair into the map.
fn insert( &mut self, element : K ) -> Option< K >;
}

impl< K > HashSetLike< K > for HashSet< K >
where
K : core::cmp::Eq + core::hash::Hash,
{
fn insert( &mut self, element : K ) -> Option< K >
{
HashSet::replace( self, element )
}
}

// = storage

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

impl< K > StoragePerform
for HashSet< K >
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 HashSetDefinition< K, Context = (), Formed = HashSet< K >, End = ReturnStorage >
{
_phantom : core::marker::PhantomData< ( K, Context, Formed, End ) >,
}

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

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

/// Facilitates building `HashSetLike` containers with a fluent API.
///
/// `HashSetSubformer` leverages the `HashSetLike` trait to enable a concise and expressive way
/// of populating `HashSet`-like containers. It exemplifies the crate's builder pattern variation for sets.
///
/// # Example Usage
///
/// Using `HashSetSubformer` to populate a `HashSet` within a struct:
///
/// ```rust
/// # #[ cfg( all( feature = "enabled", not( feature = "no_std" ) ) ) ]
/// # {
/// # use test_tools::exposed::*;
///
/// #[ derive( Debug, PartialEq, former::Former ) ]
/// pub struct StructWithSet
/// {
/// #[ subformer( former::HashSetSubformer ) ]
/// set : std::collections::HashSet< &'static str >,
/// }
///
/// let instance = StructWithSet::former()
/// .set()
/// .insert( "apple" )
/// .insert( "banana" )
/// .end()
/// .form();
///
/// assert_eq!(instance, StructWithSet { set : hset![ "apple", "banana" ] });
/// # }
/// ```

// xxx : update documentation
// pub type HashSetSubformer< K, Context, End > = ContainerSubformer::< K, HashSetDefinition< K, Context, End > >;
pub type HashSetSubformer< K, Context, Formed, End > =
ContainerSubformer::< K, HashSetDefinition< K, Context, Formed, End > >;

// = extension

pub trait HashSetExt< K > : sealed::Sealed
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
fn former() -> HashSetSubformer< K, (), HashSet< K >, ReturnStorage >;
}

impl< K > HashSetExt< K > for HashSet< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
fn former() -> HashSetSubformer< K, (), HashSet< K >, ReturnStorage >
{
HashSetSubformer::< K, (), HashSet< K >, ReturnStorage >::new( ReturnStorage::default() )
}
}

mod sealed
{
use super::HashSet;
pub trait Sealed {}
impl< K > Sealed for HashSet< K > {}
}

0 comments on commit 630816a

Please sign in to comment.