diff --git a/module/core/former/src/hash_map.rs b/module/core/former/src/hash_map.rs index 4b54113128..91bae2fd5e 100644 --- a/module/core/former/src/hash_map.rs +++ b/module/core/former/src/hash_map.rs @@ -70,8 +70,8 @@ // // // = 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 >, @@ -79,17 +79,6 @@ // _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 @@ -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 > {} +// } diff --git a/module/core/former/src/hash_set.rs b/module/core/former/src/hash_set.rs index 2c179181f3..d143fe68b6 100644 --- a/module/core/former/src/hash_set.rs +++ b/module/core/former/src/hash_set.rs @@ -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 > {} +} diff --git a/module/core/former/src/vector.rs b/module/core/former/src/vector.rs index c170e12368..fd6fe22f6e 100644 --- a/module/core/former/src/vector.rs +++ b/module/core/former/src/vector.rs @@ -44,17 +44,15 @@ for Vec< E > #[ derive( Debug, Default ) ] pub struct VectorDefinition< E, Context = (), Formed = Vec< E >, End = ReturnStorage > -// pub struct VectorDefinition< E, Context, Formed, End > +where + End : FormingEnd< VectorDefinition< E, Context, Formed, NoEnd > >, + // axiomatic::NoEnd: axiomatic::FormingEnd>, { _phantom : core::marker::PhantomData< ( E, Context, Formed, End ) >, } impl< E, Context, Formed > FormerDefinitionTypes -// for VectorDefinition< E, Context, Formed > for VectorDefinition< E, Context, Formed, NoEnd > -// for VectorDefinition< E, Context, Formed, End > -// where - // End : FormingEnd< Self >, { type Storage = Vec< E >; type Formed = Formed;