Skip to content

Commit

Permalink
former : experimenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Mar 23, 2024
1 parent e3c245f commit c5f9e93
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 88 deletions.
145 changes: 58 additions & 87 deletions module/core/former/tests/inc/former_tests/a_primitives_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Struct1

// = formed

#[automatically_derived]
impl Struct1
{
#[doc = r""]
Expand All @@ -26,9 +27,7 @@ impl Struct1
}
}

// = descriptor

#[ derive( Debug ) ]
#[derive(Debug)]
pub struct Struct1FormerDescriptor;

impl Struct1FormerDescriptor
Expand All @@ -39,15 +38,13 @@ impl Struct1FormerDescriptor
}
}

impl former::FormerDescriptor
for Struct1FormerDescriptor
impl former::FormerDescriptor for Struct1FormerDescriptor
{
type Storage = Struct1FormerStorage;
type Formed = Struct1;
}

// = storage

#[doc = "Container of a corresponding former."]
pub struct Struct1FormerStorage
{
#[doc = r" A field"]
Expand Down Expand Up @@ -75,153 +72,127 @@ impl ::core::default::Default for Struct1FormerStorage
}
}

impl former::Storage
for Struct1FormerStorage
impl former::Storage for Struct1FormerStorage
{
type Descriptor = Struct1FormerDescriptor;
}

impl former::StoragePerform
for Struct1FormerStorage
impl former::StoragePerform for Struct1FormerStorage
{

fn preform( mut self ) -> Struct1
fn preform(mut self) -> Struct1
{

let int_1 = if self.int_1.is_some()
{
self.int_1.take().unwrap()
}
else
{
let val : i32 = Default::default();
val
};

let string_1 = if self.string_1.is_some()
{
self.string_1.take().unwrap()
}
else
{
let val : String = Default::default();
val
};

let int_optional_1 = if self.int_optional_1.is_some()
{
Some( self.int_optional_1.take().unwrap() )
}
else
{
None
};

let string_optional_1 = if self.string_optional_1.is_some()
{
Some( self.string_optional_1.take().unwrap() )
}
else
{
None
};

// xxx : Rust failt to use parameter here
// < < Self as former::Storage >::Descriptor as former::FormerDescriptor >::Formed
Struct1
{
int_1,
string_1,
int_optional_1,
string_optional_1,
int_1 : self.int_1.take().or_else(|| Some(::core::marker::PhantomData::<i32>::maybe_default())).unwrap(),
string_1 : self.string_1.take().or_else(|| Some(::core::marker::PhantomData::<String>::maybe_default())).unwrap(),
int_optional_1 : self.int_optional_1,
string_optional_1 : self.string_optional_1,
}

}
}

trait MaybeDefault<T>
{
fn maybe_default() -> T;
}

// = former
impl MaybeDefault<i32> for ::core::marker::PhantomData<i32>
where i32 : ::core::default::Default,
{
fn maybe_default() -> i32
{
i32::default()
}
}

impl MaybeDefault<String> for ::core::marker::PhantomData<String>
where String : ::core::default::Default,
{
fn maybe_default() -> String
{
String::default()
}
}

#[doc =
" Object to form [Struct1]. If field's values is not set then default value of the field is set.\n\nFor specifying custom default value use attribute `default`. For example:\n```\n\nuse former::Former;\n#[ derive( Former ) ]\npub struct Struct1\n{\n #[default( 31 ) ]\n field1 : i32,\n}\n\n```\n"]
#[automatically_derived]
pub struct Struct1Former<FormerContext = Struct1, FormerEnd = former::ReturnStorage>
where FormerEnd : former::FormingEnd<Struct1FormerDescriptor, FormerContext>,
pub struct Struct1Former<__FormerContext = Struct1, __FormerEnd = former::ReturnStorage>
where __FormerEnd : former::FormingEnd<Struct1FormerDescriptor, __FormerContext>,
{
storage : Struct1FormerStorage,
context : core::option::Option<FormerContext>,
on_end : core::option::Option<FormerEnd>,
context : core::option::Option<__FormerContext>,
on_end : core::option::Option<__FormerEnd>,
}

#[automatically_derived]
impl<FormerContext, FormerEnd> Struct1Former<FormerContext, FormerEnd>
where FormerEnd : former::FormingEnd<Struct1FormerDescriptor, FormerContext>,
impl<__FormerContext, __FormerEnd> Struct1Former<__FormerContext, __FormerEnd>
where __FormerEnd : former::FormingEnd<Struct1FormerDescriptor, __FormerContext>,
{
#[doc = r""]
#[doc = r" Finish setting options and return formed entity."]
#[doc = r""]
#[doc = r" `perform` has no effect on method `form`, but change behavior and returned type of method `perform`."]
#[doc = r""]
#[inline(always)]
pub fn preform(self) -> < Struct1FormerDescriptor as former::FormerDescriptor >::Formed
pub fn preform(self) -> <Struct1FormerDescriptor as former::FormerDescriptor>::Formed
{
< Struct1FormerStorage as former::StoragePerform >::preform( self.storage )
<Struct1FormerStorage as former::StoragePerform>::preform(self.storage)
}

#[doc = r""]
#[doc = r" Finish setting options and call perform on formed entity."]
#[doc = r""]
#[doc = r" If `perform` defined then associated method is called and its result returned instead of entity."]
#[doc = r" For example `perform()` of structure with : `#[ perform( fn after1() -> &str > )` returns `&str`."]
#[doc = r" If `perform` defined then associated method is called and its result returned instead of entity."]
#[doc = r" For example `perform()` of structure with : `#[ perform( fn after1() -> &str > )` returns `&str`."]
#[doc = r""]
#[inline(always)]
pub fn perform( self ) -> < Struct1FormerDescriptor as former::FormerDescriptor >::Formed
pub fn perform(self) -> <Struct1FormerDescriptor as former::FormerDescriptor>::Formed
{
let result = self.form();
return result;
}

#[doc = r""]
#[doc = r" Begin the process of forming. Expects context of forming to return it after forming."]
#[doc = r" Begin the process of forming. Expects context of forming to return it after forming."]
#[doc = r""]
#[inline(always)]
pub fn begin(mut storage : core::option::Option<Struct1FormerStorage>, context : core::option::Option<FormerContext>, on_end : FormerEnd,) -> Self
pub fn begin(mut storage : core::option::Option<Struct1FormerStorage>, context : core::option::Option<__FormerContext>, on_end : __FormerEnd,) -> Self
{
if storage.is_none()
{
storage = Some(::core::default::Default::default());
}
Self
{
storage: storage.unwrap(),
context: context,
on_end: ::core::option::Option::Some(on_end),
storage : storage.unwrap(),
context : context,
on_end : ::core::option::Option::Some(on_end),
}
}

#[doc = r""]
#[doc = r" End the process of forming returning original context of forming."]
#[doc = r" End the process of forming returning original context of forming."]
#[doc = r""]
#[inline(always)]
pub fn form( self ) -> < Struct1FormerDescriptor as former::FormerDescriptor >::Formed
pub fn form(self) -> <Struct1FormerDescriptor as former::FormerDescriptor>::Formed
{
self.end()
}

#[doc = r""]
#[doc = r" End the process of forming returning original context of forming."]
#[doc = r" End the process of forming returning original context of forming."]
#[doc = r""]
#[inline(always)]
pub fn end(mut self) -> < Struct1FormerDescriptor as former::FormerDescriptor >::Formed
pub fn end(mut self) -> <Struct1FormerDescriptor as former::FormerDescriptor>::Formed
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
// let storage = self.form();
on_end.call(self.storage, context)
}

#[doc = "Setter for the 'int_1' field."]
#[inline]
pub fn int_1<Src>(mut self, src: Src) -> Self
where Src: ::core::convert::Into<i32>,
where Src : ::core::convert::Into<i32>,
{
debug_assert!(self.storage.int_1.is_none());
self.storage.int_1 = ::core::option::Option::Some(src.into());
Expand All @@ -231,7 +202,7 @@ where FormerEnd : former::FormingEnd<Struct1FormerDescriptor, FormerContext>,
#[doc = "Setter for the 'string_1' field."]
#[inline]
pub fn string_1<Src>(mut self, src: Src) -> Self
where Src: ::core::convert::Into<String>,
where Src : ::core::convert::Into<String>,
{
debug_assert!(self.storage.string_1.is_none());
self.storage.string_1 = ::core::option::Option::Some(src.into());
Expand All @@ -241,7 +212,7 @@ where FormerEnd : former::FormingEnd<Struct1FormerDescriptor, FormerContext>,
#[doc = "Setter for the 'int_optional_1' field."]
#[inline]
pub fn int_optional_1<Src>(mut self, src: Src) -> Self
where Src: ::core::convert::Into<i32>,
where Src : ::core::convert::Into<i32>,
{
debug_assert!(self.storage.int_optional_1.is_none());
self.storage.int_optional_1 = ::core::option::Option::Some(src.into());
Expand All @@ -251,7 +222,7 @@ where FormerEnd : former::FormingEnd<Struct1FormerDescriptor, FormerContext>,
#[doc = "Setter for the 'string_optional_1' field."]
#[inline]
pub fn string_optional_1<Src>(mut self, src: Src) -> Self
where Src: ::core::convert::Into<String>,
where Src : ::core::convert::Into<String>,
{
debug_assert!(self.storage.string_optional_1.is_none());
self.storage.string_optional_1 = ::core::option::Option::Some(src.into());
Expand Down
3 changes: 2 additions & 1 deletion module/core/former_meta/src/derive/former.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,8 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream >
#generics_where
{

fn preform( mut self ) -> #former_storage_name_ident #generics_ty
// fn preform( mut self ) -> #former_storage_name_ident #generics_ty
fn preform( mut self ) -> < former_descriptor_name_ident #generics_ty as former::FormerDescriptor >::Formed
{
Self
{
Expand Down

0 comments on commit c5f9e93

Please sign in to comment.