diff --git a/module/core/derive_tools/Cargo.toml b/module/core/derive_tools/Cargo.toml index d54788975c..5709a8e50f 100644 --- a/module/core/derive_tools/Cargo.toml +++ b/module/core/derive_tools/Cargo.toml @@ -67,6 +67,7 @@ default = [ "derive_from", "derive_inner_from", + "derive_new", # "use_std", ] @@ -111,16 +112,17 @@ full = [ "derive_from", "derive_inner_from", + "derive_new", # "use_std", ] no_std = [] use_alloc = [ "no_std", "clone_dyn/use_alloc" ] -enabled = [] +enabled = [ "derive_tools_meta/enabled" ] # nightly = [ "derive_more/nightly" ] -type_variadic_from = [ "variadic_from/type_variadic_from" ] +type_variadic_from = [ "variadic_from/enabled", "variadic_from/type_variadic_from" ] derive_variadic_from = [ "type_variadic_from", "derive_tools_meta/derive_variadic_from", "variadic_from/derive_variadic_from" ] # enable_track_caller = [ "derive_more", "derive_more/track-caller" ] @@ -163,13 +165,15 @@ derive_strum = [ "strum/std", "strum/derive", "strum/strum_macros" ] strum_phf = [ "strum/std", "strum/phf", "strum/strum_macros" ] # zzz : review features -derive_clone_dyn = [ "clone_dyn", "clone_dyn/enabled" ] +derive_clone_dyn = [ "clone_dyn/enabled" ] # derive_clone_dyn_use_std = [ "derive_clone_dyn", "clone_dyn/use_std" ] # derive_clone_dyn_no_std = [ "derive_clone_dyn", "clone_dyn/no_std" ] # derive_clone_dyn_use_alloc = [ "derive_clone_dyn", "clone_dyn/use_alloc" ] derive_from = [ "derive_tools_meta/derive_from" ] derive_inner_from = [ "derive_tools_meta/derive_inner_from" ] +derive_new = [ "derive_tools_meta/derive_new" ] + parse_display = [ "parse-display" ] [dependencies] @@ -180,10 +184,11 @@ strum = { version = "~0.25", optional = true, default-features = false } # strum_macros = { version = "~0.25.3", optional = true, default-features = false } parse-display = { version = "~0.8.2", optional = true, default-features = false } + ## internal -derive_tools_meta = { workspace = true, features = [ "enabled" ] } +derive_tools_meta = { workspace = true, features = [] } variadic_from = { workspace = true, features = [] } -clone_dyn = { workspace = true, optional = true } +clone_dyn = { workspace = true, features = [] } [dev-dependencies] test_tools = { workspace = true } diff --git a/module/core/derive_tools/tests/inc/from_inner_manual_test.rs b/module/core/derive_tools/tests/inc/from/manual_test.rs similarity index 86% rename from module/core/derive_tools/tests/inc/from_inner_manual_test.rs rename to module/core/derive_tools/tests/inc/from/manual_test.rs index 2b48bca774..4add4ff66b 100644 --- a/module/core/derive_tools/tests/inc/from_inner_manual_test.rs +++ b/module/core/derive_tools/tests/inc/from/manual_test.rs @@ -15,4 +15,4 @@ impl From< bool > for IsTransparent } } -include!( "./only_test/from_inner.rs" ); +include!( "./only_test/basic.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_multiple_manual_test.rs b/module/core/derive_tools/tests/inc/from/multiple_manual_test.rs similarity index 83% rename from module/core/derive_tools/tests/inc/from_inner_multiple_manual_test.rs rename to module/core/derive_tools/tests/inc/from/multiple_manual_test.rs index 9d49fa7e36..b44fc6b651 100644 --- a/module/core/derive_tools/tests/inc/from_inner_multiple_manual_test.rs +++ b/module/core/derive_tools/tests/inc/from/multiple_manual_test.rs @@ -12,4 +12,4 @@ impl From< ( i32, bool ) > for StructWithManyFields } } -include!( "./only_test/from_inner_multiple.rs" ); +include!( "./only_test/multiple.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_multiple_named_manual_test.rs b/module/core/derive_tools/tests/inc/from/multiple_named_manual_test.rs similarity index 82% rename from module/core/derive_tools/tests/inc/from_inner_multiple_named_manual_test.rs rename to module/core/derive_tools/tests/inc/from/multiple_named_manual_test.rs index 148dff7a42..02b756b5f5 100644 --- a/module/core/derive_tools/tests/inc/from_inner_multiple_named_manual_test.rs +++ b/module/core/derive_tools/tests/inc/from/multiple_named_manual_test.rs @@ -16,4 +16,4 @@ impl From< ( i32, bool ) > for StructNamedFields } } -include!( "./only_test/from_inner_multiple_named.rs" ); +include!( "./only_test/multiple_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_multiple_named_test.rs b/module/core/derive_tools/tests/inc/from/multiple_named_test.rs similarity index 68% rename from module/core/derive_tools/tests/inc/from_inner_multiple_named_test.rs rename to module/core/derive_tools/tests/inc/from/multiple_named_test.rs index f12692c40a..ddb088c624 100644 --- a/module/core/derive_tools/tests/inc/from_inner_multiple_named_test.rs +++ b/module/core/derive_tools/tests/inc/from/multiple_named_test.rs @@ -7,4 +7,4 @@ struct StructNamedFields b: bool, } -include!( "./only_test/from_inner_multiple_named.rs" ); +include!( "./only_test/multiple_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_multiple_test.rs b/module/core/derive_tools/tests/inc/from/multiple_test.rs similarity index 69% rename from module/core/derive_tools/tests/inc/from_inner_multiple_test.rs rename to module/core/derive_tools/tests/inc/from/multiple_test.rs index db54cef54b..925cd66ca0 100644 --- a/module/core/derive_tools/tests/inc/from_inner_multiple_test.rs +++ b/module/core/derive_tools/tests/inc/from/multiple_test.rs @@ -3,4 +3,4 @@ use super::*; #[ derive( Debug, PartialEq, Eq, the_module::From ) ] struct StructWithManyFields( i32, bool ); -include!( "./only_test/from_inner_multiple.rs" ); +include!( "./only_test/multiple.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_named_manual_test.rs b/module/core/derive_tools/tests/inc/from/named_manual_test.rs similarity index 80% rename from module/core/derive_tools/tests/inc/from_inner_named_manual_test.rs rename to module/core/derive_tools/tests/inc/from/named_manual_test.rs index d80f626439..6118d32e69 100644 --- a/module/core/derive_tools/tests/inc/from_inner_named_manual_test.rs +++ b/module/core/derive_tools/tests/inc/from/named_manual_test.rs @@ -15,4 +15,4 @@ impl From< i32 > for MyStruct } } -include!( "./only_test/from_inner_named.rs" ); +include!( "./only_test/named.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_named_test.rs b/module/core/derive_tools/tests/inc/from/named_test.rs similarity index 68% rename from module/core/derive_tools/tests/inc/from_inner_named_test.rs rename to module/core/derive_tools/tests/inc/from/named_test.rs index 0540470a01..49a1e564d1 100644 --- a/module/core/derive_tools/tests/inc/from_inner_named_test.rs +++ b/module/core/derive_tools/tests/inc/from/named_test.rs @@ -6,4 +6,4 @@ struct MyStruct a : i32, } -include!( "./only_test/from_inner_named.rs" ); +include!( "./only_test/named.rs" ); diff --git a/module/core/derive_tools/tests/inc/only_test/from_inner.rs b/module/core/derive_tools/tests/inc/from/only_test/basic.rs similarity index 97% rename from module/core/derive_tools/tests/inc/only_test/from_inner.rs rename to module/core/derive_tools/tests/inc/from/only_test/basic.rs index df3fca75ea..f7f591e3c0 100644 --- a/module/core/derive_tools/tests/inc/only_test/from_inner.rs +++ b/module/core/derive_tools/tests/inc/from/only_test/basic.rs @@ -1,6 +1,6 @@ #[ test ] -fn from_inner_test() +fn from_test() { // let got = IsTransparent::default(); diff --git a/module/core/derive_tools/tests/inc/only_test/from_inner_multiple.rs b/module/core/derive_tools/tests/inc/from/only_test/multiple.rs similarity index 87% rename from module/core/derive_tools/tests/inc/only_test/from_inner_multiple.rs rename to module/core/derive_tools/tests/inc/from/only_test/multiple.rs index 4ef1750651..0386486e7c 100644 --- a/module/core/derive_tools/tests/inc/only_test/from_inner_multiple.rs +++ b/module/core/derive_tools/tests/inc/from/only_test/multiple.rs @@ -1,5 +1,5 @@ #[ test ] -fn from_inner_named() +fn from_named() { let got : StructWithManyFields = StructWithManyFields::from((10, true)); let exp = StructWithManyFields( 10 , true ); diff --git a/module/core/derive_tools/tests/inc/only_test/from_inner_multiple_named.rs b/module/core/derive_tools/tests/inc/from/only_test/multiple_named.rs similarity index 87% rename from module/core/derive_tools/tests/inc/only_test/from_inner_multiple_named.rs rename to module/core/derive_tools/tests/inc/from/only_test/multiple_named.rs index 3ba0ced760..b88a4f3872 100644 --- a/module/core/derive_tools/tests/inc/only_test/from_inner_multiple_named.rs +++ b/module/core/derive_tools/tests/inc/from/only_test/multiple_named.rs @@ -1,5 +1,5 @@ #[ test ] -fn from_inner_named() +fn from_named() { let got : StructNamedFields = StructNamedFields::from((10, true)); let exp = StructNamedFields{ a : 10 , b : true }; diff --git a/module/core/derive_tools/tests/inc/only_test/from_inner_named.rs b/module/core/derive_tools/tests/inc/from/only_test/named.rs similarity index 83% rename from module/core/derive_tools/tests/inc/only_test/from_inner_named.rs rename to module/core/derive_tools/tests/inc/from/only_test/named.rs index ff62e904eb..d749625ce6 100644 --- a/module/core/derive_tools/tests/inc/only_test/from_inner_named.rs +++ b/module/core/derive_tools/tests/inc/from/only_test/named.rs @@ -1,5 +1,5 @@ #[ test ] -fn from_inner_named() +fn from_named() { let got : MyStruct = MyStruct::from( 13 ); let exp = MyStruct { a : 13 }; diff --git a/module/core/derive_tools/tests/inc/only_test/from_inner_unit.rs b/module/core/derive_tools/tests/inc/from/only_test/unit.rs similarity index 82% rename from module/core/derive_tools/tests/inc/only_test/from_inner_unit.rs rename to module/core/derive_tools/tests/inc/from/only_test/unit.rs index 1cc2c51750..7e5b22ad51 100644 --- a/module/core/derive_tools/tests/inc/only_test/from_inner_unit.rs +++ b/module/core/derive_tools/tests/inc/from/only_test/unit.rs @@ -1,5 +1,5 @@ #[ test ] -fn from_inner_named() +fn from_named() { let got : UnitStruct = UnitStruct::from( () ); let exp = UnitStruct; diff --git a/module/core/derive_tools/tests/inc/only_test/from_inner_variants.rs b/module/core/derive_tools/tests/inc/from/only_test/variants.rs similarity index 100% rename from module/core/derive_tools/tests/inc/only_test/from_inner_variants.rs rename to module/core/derive_tools/tests/inc/from/only_test/variants.rs diff --git a/module/core/derive_tools/tests/inc/only_test/from_inner_variants_duplicates.rs b/module/core/derive_tools/tests/inc/from/only_test/variants_duplicates.rs similarity index 100% rename from module/core/derive_tools/tests/inc/only_test/from_inner_variants_duplicates.rs rename to module/core/derive_tools/tests/inc/from/only_test/variants_duplicates.rs diff --git a/module/core/derive_tools/tests/inc/only_test/from_inner_variants_generics.rs b/module/core/derive_tools/tests/inc/from/only_test/variants_generics.rs similarity index 100% rename from module/core/derive_tools/tests/inc/only_test/from_inner_variants_generics.rs rename to module/core/derive_tools/tests/inc/from/only_test/variants_generics.rs diff --git a/module/core/derive_tools/tests/inc/from_inner_test.rs b/module/core/derive_tools/tests/inc/from/test.rs similarity index 83% rename from module/core/derive_tools/tests/inc/from_inner_test.rs rename to module/core/derive_tools/tests/inc/from/test.rs index 98890e6c2e..1214ad5a43 100644 --- a/module/core/derive_tools/tests/inc/from_inner_test.rs +++ b/module/core/derive_tools/tests/inc/from/test.rs @@ -7,4 +7,4 @@ use super::*; pub struct IsTransparent( bool ); // include!( "./manual/basic.rs" ); -include!( "./only_test/from_inner.rs" ); +include!( "./only_test/basic.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_unit_manual_test.rs b/module/core/derive_tools/tests/inc/from/unit_manual_test.rs similarity index 80% rename from module/core/derive_tools/tests/inc/from_inner_unit_manual_test.rs rename to module/core/derive_tools/tests/inc/from/unit_manual_test.rs index 78b7578956..dc767e9fbb 100644 --- a/module/core/derive_tools/tests/inc/from_inner_unit_manual_test.rs +++ b/module/core/derive_tools/tests/inc/from/unit_manual_test.rs @@ -12,4 +12,4 @@ impl From< () > for UnitStruct } } -include!( "./only_test/from_inner_unit.rs" ); +include!( "./only_test/unit.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_unit_test.rs b/module/core/derive_tools/tests/inc/from/unit_test.rs similarity index 68% rename from module/core/derive_tools/tests/inc/from_inner_unit_test.rs rename to module/core/derive_tools/tests/inc/from/unit_test.rs index e551263159..82690e5190 100644 --- a/module/core/derive_tools/tests/inc/from_inner_unit_test.rs +++ b/module/core/derive_tools/tests/inc/from/unit_test.rs @@ -3,4 +3,4 @@ use super::*; #[ derive( Debug, Clone, Copy, PartialEq, the_module::From ) ] struct UnitStruct; -include!( "./only_test/from_inner_unit.rs" ); +include!( "./only_test/unit.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_variants_collisions.rs b/module/core/derive_tools/tests/inc/from/variants_collisions.rs similarity index 89% rename from module/core/derive_tools/tests/inc/from_inner_variants_collisions.rs rename to module/core/derive_tools/tests/inc/from/variants_collisions.rs index d7bb05fa83..aefa3be96f 100644 --- a/module/core/derive_tools/tests/inc/from_inner_variants_collisions.rs +++ b/module/core/derive_tools/tests/inc/from/variants_collisions.rs @@ -24,4 +24,4 @@ pub enum GetData // == begin of generated // == end of generated -include!( "./only_test/from_inner_variants.rs" ); +include!( "./only_test/variants.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_variants_derive.rs b/module/core/derive_tools/tests/inc/from/variants_derive.rs similarity index 85% rename from module/core/derive_tools/tests/inc/from_inner_variants_derive.rs rename to module/core/derive_tools/tests/inc/from/variants_derive.rs index 1596a7f623..27792afbdc 100644 --- a/module/core/derive_tools/tests/inc/from_inner_variants_derive.rs +++ b/module/core/derive_tools/tests/inc/from/variants_derive.rs @@ -15,4 +15,4 @@ pub enum GetData // == begin of generated // == end of generated -include!( "./only_test/from_inner_variants.rs" ); +include!( "./only_test/variants.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_variants_duplicates_all_off.rs b/module/core/derive_tools/tests/inc/from/variants_duplicates_all_off.rs similarity index 92% rename from module/core/derive_tools/tests/inc/from_inner_variants_duplicates_all_off.rs rename to module/core/derive_tools/tests/inc/from/variants_duplicates_all_off.rs index 8d352850ff..1eb00d2920 100644 --- a/module/core/derive_tools/tests/inc/from_inner_variants_duplicates_all_off.rs +++ b/module/core/derive_tools/tests/inc/from/variants_duplicates_all_off.rs @@ -42,4 +42,4 @@ impl From< ( String, String ) > for GetData // == end of generated -include!( "./only_test/from_inner_variants_duplicates.rs" ); +include!( "./only_test/variants_duplicates.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_variants_duplicates_some_off.rs b/module/core/derive_tools/tests/inc/from/variants_duplicates_some_off.rs similarity index 87% rename from module/core/derive_tools/tests/inc/from_inner_variants_duplicates_some_off.rs rename to module/core/derive_tools/tests/inc/from/variants_duplicates_some_off.rs index 1fa60cb598..094d57a5f1 100644 --- a/module/core/derive_tools/tests/inc/from_inner_variants_duplicates_some_off.rs +++ b/module/core/derive_tools/tests/inc/from/variants_duplicates_some_off.rs @@ -22,4 +22,4 @@ pub enum GetData // == end of generated -include!( "./only_test/from_inner_variants_duplicates.rs" ); +include!( "./only_test/variants_duplicates.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_variants_duplicates_some_off_default_off.rs b/module/core/derive_tools/tests/inc/from/variants_duplicates_some_off_default_off.rs similarity index 88% rename from module/core/derive_tools/tests/inc/from_inner_variants_duplicates_some_off_default_off.rs rename to module/core/derive_tools/tests/inc/from/variants_duplicates_some_off_default_off.rs index 9d60a25204..282b327e23 100644 --- a/module/core/derive_tools/tests/inc/from_inner_variants_duplicates_some_off_default_off.rs +++ b/module/core/derive_tools/tests/inc/from/variants_duplicates_some_off_default_off.rs @@ -24,4 +24,4 @@ pub enum GetData // == begin of generated // == end of generated -include!( "./only_test/from_inner_variants_duplicates.rs" ); +include!( "./only_test/variants_duplicates.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_variants_generics.rs b/module/core/derive_tools/tests/inc/from/variants_generics.rs similarity index 82% rename from module/core/derive_tools/tests/inc/from_inner_variants_generics.rs rename to module/core/derive_tools/tests/inc/from/variants_generics.rs index 3c30a7d406..c163e39b7f 100644 --- a/module/core/derive_tools/tests/inc/from_inner_variants_generics.rs +++ b/module/core/derive_tools/tests/inc/from/variants_generics.rs @@ -14,4 +14,4 @@ pub enum GetData< 'a, T : ToString + ?Sized = str > // == begin of generated // == end of generated -include!( "./only_test/from_inner_variants_generics.rs" ); +include!( "./only_test/variants_generics.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_variants_generics_where.rs b/module/core/derive_tools/tests/inc/from/variants_generics_where.rs similarity index 83% rename from module/core/derive_tools/tests/inc/from_inner_variants_generics_where.rs rename to module/core/derive_tools/tests/inc/from/variants_generics_where.rs index cfe6e82394..ec96c5313b 100644 --- a/module/core/derive_tools/tests/inc/from_inner_variants_generics_where.rs +++ b/module/core/derive_tools/tests/inc/from/variants_generics_where.rs @@ -16,4 +16,4 @@ where // == begin of generated // == end of generated -include!( "./only_test/from_inner_variants_generics.rs" ); +include!( "./only_test/variants_generics.rs" ); diff --git a/module/core/derive_tools/tests/inc/from_inner_variants_manual.rs b/module/core/derive_tools/tests/inc/from/variants_manual.rs similarity index 92% rename from module/core/derive_tools/tests/inc/from_inner_variants_manual.rs rename to module/core/derive_tools/tests/inc/from/variants_manual.rs index 749d0d7d0d..9cd6e1e723 100644 --- a/module/core/derive_tools/tests/inc/from_inner_variants_manual.rs +++ b/module/core/derive_tools/tests/inc/from/variants_manual.rs @@ -38,4 +38,4 @@ impl From< &'static [ u8 ] > for GetData } } -include!( "./only_test/from_inner_variants.rs" ); +include!( "./only_test/variants.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_manual_test.rs b/module/core/derive_tools/tests/inc/inner_from/manual_test.rs similarity index 87% rename from module/core/derive_tools/tests/inc/inner_from_manual_test.rs rename to module/core/derive_tools/tests/inc/inner_from/manual_test.rs index 7e1d5a5ee6..4313f84564 100644 --- a/module/core/derive_tools/tests/inc/inner_from_manual_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/manual_test.rs @@ -15,4 +15,4 @@ impl From< IsTransparent > for bool } } -include!( "./only_test/inner_from.rs" ); +include!( "./only_test/basic.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_multiple_manual_test.rs b/module/core/derive_tools/tests/inc/inner_from/multiple_manual_test.rs similarity index 83% rename from module/core/derive_tools/tests/inc/inner_from_multiple_manual_test.rs rename to module/core/derive_tools/tests/inc/inner_from/multiple_manual_test.rs index 5b59a8a389..2bc7587221 100644 --- a/module/core/derive_tools/tests/inc/inner_from_multiple_manual_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/multiple_manual_test.rs @@ -12,4 +12,4 @@ impl From< StructWithManyFields > for ( i32, bool ) } } -include!( "./only_test/inner_from_multiple.rs" ); +include!( "./only_test/multiple.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_multiple_named_manual_test.rs b/module/core/derive_tools/tests/inc/inner_from/multiple_named_manual_test.rs similarity index 81% rename from module/core/derive_tools/tests/inc/inner_from_multiple_named_manual_test.rs rename to module/core/derive_tools/tests/inc/inner_from/multiple_named_manual_test.rs index 69db46283f..0a934e454f 100644 --- a/module/core/derive_tools/tests/inc/inner_from_multiple_named_manual_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/multiple_named_manual_test.rs @@ -16,4 +16,4 @@ impl From< StructNamedFields > for ( i32, bool ) } } -include!( "./only_test/inner_from_multiple_named.rs" ); +include!( "./only_test/multiple_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_multiple_named_test.rs b/module/core/derive_tools/tests/inc/inner_from/multiple_named_test.rs similarity index 69% rename from module/core/derive_tools/tests/inc/inner_from_multiple_named_test.rs rename to module/core/derive_tools/tests/inc/inner_from/multiple_named_test.rs index 07f93b2d15..6177841adf 100644 --- a/module/core/derive_tools/tests/inc/inner_from_multiple_named_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/multiple_named_test.rs @@ -7,4 +7,4 @@ struct StructNamedFields b: bool, } -include!( "./only_test/inner_from_multiple_named.rs" ); +include!( "./only_test/multiple_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_multiple_test.rs b/module/core/derive_tools/tests/inc/inner_from/multiple_test.rs similarity index 70% rename from module/core/derive_tools/tests/inc/inner_from_multiple_test.rs rename to module/core/derive_tools/tests/inc/inner_from/multiple_test.rs index 6c2fe1f1ef..c99e112ca4 100644 --- a/module/core/derive_tools/tests/inc/inner_from_multiple_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/multiple_test.rs @@ -3,4 +3,4 @@ use super::*; #[ derive( Debug, PartialEq, Eq, the_module::InnerFrom ) ] struct StructWithManyFields( i32, bool ); -include!( "./only_test/inner_from_multiple.rs" ); +include!( "./only_test/multiple.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_named_manual_test.rs b/module/core/derive_tools/tests/inc/inner_from/named_manual_test.rs similarity index 80% rename from module/core/derive_tools/tests/inc/inner_from_named_manual_test.rs rename to module/core/derive_tools/tests/inc/inner_from/named_manual_test.rs index d79107577a..4d7805644d 100644 --- a/module/core/derive_tools/tests/inc/inner_from_named_manual_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/named_manual_test.rs @@ -15,4 +15,4 @@ impl From< MyStruct > for i32 } } -include!( "./only_test/inner_from_named.rs" ); +include!( "./only_test/named.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_named_test.rs b/module/core/derive_tools/tests/inc/inner_from/named_test.rs similarity index 69% rename from module/core/derive_tools/tests/inc/inner_from_named_test.rs rename to module/core/derive_tools/tests/inc/inner_from/named_test.rs index da449524f3..2f6d3ccccc 100644 --- a/module/core/derive_tools/tests/inc/inner_from_named_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/named_test.rs @@ -6,4 +6,4 @@ struct MyStruct a: i32, } -include!( "./only_test/inner_from_named.rs" ); +include!( "./only_test/named.rs" ); diff --git a/module/core/derive_tools/tests/inc/only_test/inner_from.rs b/module/core/derive_tools/tests/inc/inner_from/only_test/basic.rs similarity index 100% rename from module/core/derive_tools/tests/inc/only_test/inner_from.rs rename to module/core/derive_tools/tests/inc/inner_from/only_test/basic.rs diff --git a/module/core/derive_tools/tests/inc/only_test/inner_from_multiple.rs b/module/core/derive_tools/tests/inc/inner_from/only_test/multiple.rs similarity index 85% rename from module/core/derive_tools/tests/inc/only_test/inner_from_multiple.rs rename to module/core/derive_tools/tests/inc/inner_from/only_test/multiple.rs index dca4fc2884..776347fd66 100644 --- a/module/core/derive_tools/tests/inc/only_test/inner_from_multiple.rs +++ b/module/core/derive_tools/tests/inc/inner_from/only_test/multiple.rs @@ -1,5 +1,5 @@ #[ test ] -fn from_inner_named() +fn from_named() { let got : ( i32, bool ) = StructWithManyFields( 10, true ).into(); let exp = ( 10 , true ); diff --git a/module/core/derive_tools/tests/inc/only_test/inner_from_multiple_named.rs b/module/core/derive_tools/tests/inc/inner_from/only_test/multiple_named.rs similarity index 85% rename from module/core/derive_tools/tests/inc/only_test/inner_from_multiple_named.rs rename to module/core/derive_tools/tests/inc/inner_from/only_test/multiple_named.rs index 0ac967c2f7..05b9a66e9d 100644 --- a/module/core/derive_tools/tests/inc/only_test/inner_from_multiple_named.rs +++ b/module/core/derive_tools/tests/inc/inner_from/only_test/multiple_named.rs @@ -1,5 +1,5 @@ #[ test ] -fn from_inner_named() +fn from_named() { let got : ( i32, bool ) = StructNamedFields{ a: 10, b: true }.into(); let exp = ( 10 , true ); diff --git a/module/core/derive_tools/tests/inc/only_test/inner_from_named.rs b/module/core/derive_tools/tests/inc/inner_from/only_test/named.rs similarity index 100% rename from module/core/derive_tools/tests/inc/only_test/inner_from_named.rs rename to module/core/derive_tools/tests/inc/inner_from/only_test/named.rs diff --git a/module/core/derive_tools/tests/inc/only_test/inner_from_unit.rs b/module/core/derive_tools/tests/inc/inner_from/only_test/unit.rs similarity index 100% rename from module/core/derive_tools/tests/inc/only_test/inner_from_unit.rs rename to module/core/derive_tools/tests/inc/inner_from/only_test/unit.rs diff --git a/module/core/derive_tools/tests/inc/inner_from_test.rs b/module/core/derive_tools/tests/inc/inner_from/test.rs similarity index 69% rename from module/core/derive_tools/tests/inc/inner_from_test.rs rename to module/core/derive_tools/tests/inc/inner_from/test.rs index b2c70b3eed..25ff2921e0 100644 --- a/module/core/derive_tools/tests/inc/inner_from_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/test.rs @@ -6,5 +6,4 @@ use super::*; #[ derive( Debug, Clone, Copy, PartialEq, the_module::InnerFrom ) ] pub struct IsTransparent( bool ); -// include!( "./manual/basic.rs" ); -include!( "./only_test/inner_from.rs" ); +include!( "./only_test/basic.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_unit_manual_test.rs b/module/core/derive_tools/tests/inc/inner_from/unit_manual_test.rs similarity index 83% rename from module/core/derive_tools/tests/inc/inner_from_unit_manual_test.rs rename to module/core/derive_tools/tests/inc/inner_from/unit_manual_test.rs index a4da6ca8f7..351db13dbb 100644 --- a/module/core/derive_tools/tests/inc/inner_from_unit_manual_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/unit_manual_test.rs @@ -13,4 +13,4 @@ impl From< UnitStruct > for () } // include!( "./manual/basic.rs" ); -include!( "./only_test/inner_from_unit.rs" ); +include!( "./only_test/unit.rs" ); diff --git a/module/core/derive_tools/tests/inc/inner_from_unit_test.rs b/module/core/derive_tools/tests/inc/inner_from/unit_test.rs similarity index 70% rename from module/core/derive_tools/tests/inc/inner_from_unit_test.rs rename to module/core/derive_tools/tests/inc/inner_from/unit_test.rs index 0bc0f38fe5..6d60f9cc6a 100644 --- a/module/core/derive_tools/tests/inc/inner_from_unit_test.rs +++ b/module/core/derive_tools/tests/inc/inner_from/unit_test.rs @@ -4,4 +4,4 @@ use super::*; pub struct UnitStruct; -include!( "./only_test/inner_from_unit.rs" ); +include!( "./only_test/unit.rs" ); diff --git a/module/core/derive_tools/tests/inc/mod.rs b/module/core/derive_tools/tests/inc/mod.rs index 779ae76708..f100189dbf 100644 --- a/module/core/derive_tools/tests/inc/mod.rs +++ b/module/core/derive_tools/tests/inc/mod.rs @@ -48,53 +48,52 @@ mod deref_mut_manual_test; mod deref_mut_test; #[ cfg( feature = "derive_from" ) ] -mod from_inner_named_test; -mod from_inner_named_manual_test; +#[ path = "from" ] +mod tests +{ + #[ allow( unused_imports ) ] + use super::*; + + mod named_test; + mod named_manual_test; + + mod manual_test; + mod multiple_named_manual_test; + mod multiple_manual_test; + mod unit_manual_test; + mod test; + mod multiple_named_test; + mod unit_test; + mod multiple_test; + + mod variants_manual; + mod variants_derive; + + mod variants_duplicates_all_off; + mod variants_duplicates_some_off; + mod variants_duplicates_some_off_default_off; + + mod variants_generics; + mod variants_generics_where; + mod variants_collisions; +} -mod from_inner_manual_test; -mod from_inner_multiple_named_manual_test; -mod from_inner_multiple_manual_test; -mod from_inner_unit_manual_test; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_test; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_multiple_named_test; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_unit_test; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_multiple_test; - -#[ cfg( feature = "derive_from" ) ] -mod from_inner_variants_manual; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_variants_derive; - -#[ cfg( feature = "derive_from" ) ] -mod from_inner_variants_duplicates_all_off; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_variants_duplicates_some_off; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_variants_duplicates_some_off_default_off; - -#[ cfg( feature = "derive_from" ) ] -mod from_inner_variants_generics; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_variants_generics_where; -#[ cfg( feature = "derive_from" ) ] -mod from_inner_variants_collisions; - -mod inner_from_manual_test; -mod inner_from_named_manual_test; -mod inner_from_multiple_named_manual_test; -mod inner_from_multiple_manual_test; -mod inner_from_unit_manual_test; -#[ cfg( feature = "derive_inner_from" ) ] -mod inner_from_test; -#[ cfg( feature = "derive_inner_from" ) ] -mod inner_from_named_test; -#[ cfg( feature = "derive_inner_from" ) ] -mod inner_from_multiple_named_test; -#[ cfg( feature = "derive_inner_from" ) ] -mod inner_from_unit_test; #[ cfg( feature = "derive_inner_from" ) ] -mod inner_from_multiple_test; +#[ path = "inner_from" ] +mod inner_from_tests +{ + #[ allow( unused_imports ) ] + use super::*; + + mod manual_test; + mod named_manual_test; + mod multiple_named_manual_test; + mod multiple_manual_test; + mod unit_manual_test; + mod test; + mod named_test; + mod multiple_named_test; + mod unit_test; + mod multiple_test; + +} diff --git a/module/core/derive_tools/tests/inc/only_test/reflect_struct.rs b/module/core/derive_tools/tests/inc/only_test/reflect_struct.rs deleted file mode 100644 index a376802fc5..0000000000 --- a/module/core/derive_tools/tests/inc/only_test/reflect_struct.rs +++ /dev/null @@ -1,28 +0,0 @@ -#[ test ] -fn reflect_basic_test() -{ - use reflect::Entity; - - let ins = Struct1 - { - f1 : 1, - f2 : "2".into(), - f3 : "3", - }; - - a_id!( reflect::reflect( &ins ).is_container(), true ); - a_id!( reflect::reflect( &ins ).len(), 3 ); - a_id!( reflect::reflect( &ins ).type_name(), "derive_tests::inc::reflect_struct_manual_test::Struct1" ); - let names = reflect::reflect( &ins ).elements().map( | e | e.key ).collect::< Vec< _ > >(); - a_id!( names, vec![ reflect::Primitive::str( "f1" ), reflect::Primitive::str( "f2" ), reflect::Primitive::str( "f3" ) ] ); - let types = reflect::reflect( &ins ).elements().map( | e | e.val.type_name() ).collect::< Vec< _ > >(); - a_id!( types, vec![ "i32", "alloc::string::String", "&str" ] ); - - let f1 = reflect::reflect( &ins ).elements().next().unwrap(); - a_id!( f1.key, reflect::Primitive::str( "f1" ) ); - a_id!( f1.val.is_container(), false ); - a_id!( f1.val.len(), 0 ); - a_id!( f1.val.type_name(), "i32" ); - a_id!( f1.val.elements().collect::< Vec< _ > >(), vec![] ); - -} diff --git a/module/core/derive_tools/tests/inc/only_test/reflect_struct_in_struct.rs b/module/core/derive_tools/tests/inc/only_test/reflect_struct_in_struct.rs deleted file mode 100644 index 9d205fc776..0000000000 --- a/module/core/derive_tools/tests/inc/only_test/reflect_struct_in_struct.rs +++ /dev/null @@ -1,31 +0,0 @@ -#[ test ] -fn reflect_struct_in_struct() -{ - use reflect::Entity; - - let ins = Struct1 - { - f1 : 1, - f2 : "2".into(), - f3 : Struct2 { s1 : 10, s2 : "20".into(), s3 : "30" }, - }; - - a_id!( reflect::reflect( &ins ).is_container(), true ); - a_id!( reflect::reflect( &ins ).len(), 3 ); - a_id!( reflect::reflect( &ins ).type_name(), "derive_tests::inc::reflect_struct_in_struct_manual_test::Struct1" ); - let names = reflect::reflect( &ins ).elements().map( | e | e.key ).collect::< Vec< _ > >(); - a_id!( names, vec![ reflect::Primitive::str( "f1" ), reflect::Primitive::str( "f2" ), reflect::Primitive::str( "f3" ) ] ); - let types = reflect::reflect( &ins ).elements().map( | e | e.val.type_name() ).collect::< Vec< _ > >(); - a_id!( types, vec![ "i32", "alloc::string::String", "derive_tests::inc::reflect_struct_in_struct_manual_test::Struct2" ] ); - - let f3 = reflect::reflect( &ins ).elements().skip( 2 ).next().unwrap(); - a_id!( f3.key, reflect::Primitive::str( "f3" ) ); - a_id!( f3.val.is_container(), true ); - a_id!( f3.val.len(), 3 ); - a_id!( f3.val.type_name(), "derive_tests::inc::reflect_struct_in_struct_manual_test::Struct2" ); - let names = f3.val.elements().map( | e | e.key ).collect::< Vec< _ > >(); - a_id!( names, vec![ reflect::Primitive::str( "s1" ), reflect::Primitive::str( "s2" ), reflect::Primitive::str( "s3" ) ] ); - let types = f3.val.elements().map( | e | e.val.type_name() ).collect::< Vec< _ > >(); - a_id!( types, vec![ "i32", "alloc::string::String", "&str" ] ); - -} diff --git a/module/core/derive_tools/tests/inc/only_test/reflect_struct_with_lifetime.rs b/module/core/derive_tools/tests/inc/only_test/reflect_struct_with_lifetime.rs deleted file mode 100644 index 35adf13d24..0000000000 --- a/module/core/derive_tools/tests/inc/only_test/reflect_struct_with_lifetime.rs +++ /dev/null @@ -1,49 +0,0 @@ -#[ test ] -fn reflect_struct_with_lifetime() -{ - use reflect::Entity; - - // assumptions - a_id!( core::any::TypeId::of::< &'static str >(), core::any::TypeId::of::< &str >() ); - - // structure - let x = 1; - let z = "3"; - let ins = Struct1 - { - f1 : &x, - f2 : 2, - f3 : &z, - }; - - // for information - println!( "Struct1 : {:?}", reflect( &ins ).type_id() ); - println!( "Struct1.f1 : {:?}", reflect( &ins ).elements().next().unwrap().val.type_id() ); - println!( "Struct1.f2 : {:?}", reflect( &ins ).elements().skip( 1 ).next().unwrap().val.type_id() ); - println!( "Struct1.f3 : {:?}", reflect( &ins ).elements().skip( 2 ).next().unwrap().val.type_id() ); - - println!( "i32.type_id : {:?}", reflect( &1i32 ).type_id() ); - println!( "i32.type_name : {:?}", reflect( &1i32 ).type_name() ); - println!( "&i32.type_id : {:?}", reflect( &&1i32 ).type_id() ); - println!( "&i32.type_name : {:?}", reflect( &&1i32 ).type_name() ); - - // inspection of structure - a_id!( reflect::reflect( &ins ).is_container(), true ); - a_id!( reflect::reflect( &ins ).len(), 3 ); - a_id!( reflect::reflect( &ins ).type_name(), "derive_tests::inc::reflect_struct_with_lifetime_manual_test::Struct1" ); - a_id!( reflect::reflect( &ins ).type_id(), core::any::TypeId::of::< Struct1< 'static, 'static > >() ); - let names = reflect::reflect( &ins ).elements().map( | e | e.key ).collect::< Vec< _ > >(); - a_id!( names, vec![ reflect::Primitive::str( "f1" ), reflect::Primitive::str( "f2" ), reflect::Primitive::str( "f3" ) ] ); - let types = reflect::reflect( &ins ).elements().map( | e | e.val.type_name() ).collect::< Vec< _ > >(); - a_id!( types, vec![ "&i32", "i32", "&str" ] ); - - // inspection of a field - let f1 = reflect::reflect( &ins ).elements().next().unwrap(); - a_id!( f1.key, reflect::Primitive::str( "f1" ) ); - a_id!( f1.val.is_container(), false ); - a_id!( f1.val.len(), 0 ); - a_id!( f1.val.type_name(), "&i32" ); - a_id!( f1.val.type_id(), core::any::TypeId::of::< &'static i32 >() ); - a_id!( f1.val.elements().collect::< Vec< _ > >(), vec![] ); - -} diff --git a/module/core/derive_tools_meta/Cargo.toml b/module/core/derive_tools_meta/Cargo.toml index a266fd1ee6..747a5fa892 100644 --- a/module/core/derive_tools_meta/Cargo.toml +++ b/module/core/derive_tools_meta/Cargo.toml @@ -35,6 +35,7 @@ default = [ "derive_deref_mut", "derive_deref", "derive_from", + "derive_new", "derive_inner_from", "derive_as_ref", "derive_as_mut", @@ -45,28 +46,29 @@ full = [ "derive_deref_mut", "derive_deref", "derive_from", + "derive_new", "derive_inner_from", "derive_as_ref", "derive_as_mut", "derive_variadic_from", ] -enabled = [] +enabled = [ "macro_tools/enabled", "iter_tools/enabled", "former_types/enabled" ] derive_as_mut = [] derive_as_ref = [] derive_deref = [] derive_deref_mut = [] derive_from = [] +derive_new = [] derive_inner_from = [] derive_variadic_from = [] [dependencies] +# xxx : qqq : optimize features set macro_tools = { workspace = true, features = [ "full" ] } iter_tools = { workspace = true, features = [ "full" ] } -former_types = { workspace = true, features = [ "enabled", "types_component_assign" ] } +former_types = { workspace = true, features = [ "types_component_assign" ] } const_format = { version = "0.2.32" } -# qqq : optimize features set - [dev-dependencies] test_tools = { workspace = true } diff --git a/module/core/derive_tools_meta/src/derive.rs b/module/core/derive_tools_meta/src/derive.rs index 4f5242d2be..5008fe2fab 100644 --- a/module/core/derive_tools_meta/src/derive.rs +++ b/module/core/derive_tools_meta/src/derive.rs @@ -20,6 +20,8 @@ pub mod deref_mut; pub mod from; #[ cfg( feature = "derive_inner_from" ) ] pub mod inner_from; +#[ cfg( feature = "derive_new" ) ] +pub mod new; #[ cfg( feature = "derive_variadic_from" ) ] pub mod variadic_from; #[ cfg( feature = "derive_reflect" ) ] diff --git a/module/core/derive_tools_meta/src/derive/from.rs b/module/core/derive_tools_meta/src/derive/from.rs index d05b416235..88783c21e6 100644 --- a/module/core/derive_tools_meta/src/derive/from.rs +++ b/module/core/derive_tools_meta/src/derive/from.rs @@ -18,7 +18,7 @@ use item_attributes::*; pub fn from( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStream > { - use macro_tools::quote::ToTokens; + // use macro_tools::quote::ToTokens; let original_input = input.clone(); let parsed = syn::parse::< StructLike >( input )?; @@ -48,7 +48,7 @@ pub fn from( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStre &generics_where, ), ( 1, Some( mut field_names ) ) => - generate_from_single_field_named + generate_single_field_named ( item_name, &generics_impl, @@ -58,7 +58,7 @@ pub fn from( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStre &field_types.next().unwrap(), ), ( 1, None ) => - generate_from_single_field + generate_single_field ( item_name, &generics_impl, @@ -67,7 +67,7 @@ pub fn from( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStre &field_types.next().unwrap(), ), ( _, Some( field_names ) ) => - generate_from_multiple_fields_named + generate_multiple_fields_named ( item_name, &generics_impl, @@ -77,7 +77,7 @@ pub fn from( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStre field_types, ), ( _, None ) => - generate_from_multiple_fields + generate_multiple_fields ( item_name, &generics_impl, @@ -91,14 +91,14 @@ pub fn from( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStre StructLike::Enum( ref item ) => { - let mut map = std::collections::HashMap::new(); - item.variants.iter().for_each( | variant | - { - map - .entry( variant.fields.to_token_stream().to_string() ) - .and_modify( | e | *e += 1 ) - .or_insert( 1 ); - }); + // let mut map = std::collections::HashMap::new(); + // item.variants.iter().for_each( | variant | + // { + // map + // .entry( variant.fields.to_token_stream().to_string() ) + // .and_modify( | e | *e += 1 ) + // .or_insert( 1 ); + // }); let variants_result : Result< Vec< proc_macro2::TokenStream > > = item.variants.iter().map( | variant | { @@ -141,7 +141,7 @@ pub fn from( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStre Ok( result ) } -// qqq : document, add example of generated code +// qqq : document, add example of generated code fn generate_unit ( item_name : &syn::Ident, @@ -167,8 +167,8 @@ fn generate_unit } } -// qqq : document, add example of generated code -fn generate_from_single_field_named +// qqq : document, add example of generated code +fn generate_single_field_named ( item_name : &syn::Ident, generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, @@ -188,8 +188,8 @@ fn generate_from_single_field_named #generics_where { #[ inline( always ) ] - // fn from( src: i32 ) -> Self - fn from( src: #field_type ) -> Self + // fn from( src : i32 ) -> Self + fn from( src : #field_type ) -> Self { // Self { a: src } Self { #field_name: src } @@ -198,8 +198,8 @@ fn generate_from_single_field_named } } -// qqq : document, add example of generated code -fn generate_from_single_field +// qqq : document, add example of generated code +fn generate_single_field ( item_name : &syn::Ident, generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, @@ -219,18 +219,18 @@ fn generate_from_single_field #generics_where { #[ inline( always ) ] - // fn from( src: bool ) -> Self - fn from( src: #field_type ) -> Self + // fn from( src : bool ) -> Self + fn from( src : #field_type ) -> Self { - // Self(src) - Self(src) + // Self( src ) + Self( src ) } } } } // qqq : document, add example of generated code -fn generate_from_multiple_fields_named< 'a > +fn generate_multiple_fields_named< 'a > ( item_name : &syn::Ident, generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, @@ -260,7 +260,7 @@ fn generate_from_multiple_fields_named< 'a > #generics_where { #[ inline( always ) ] - // fn from( src: (i32, bool) ) -> Self + // fn from( src : (i32, bool) ) -> Self fn from( src : ( #( #field_types ),* ) ) -> Self { // StructNamedFields{ a: src.0, b: src.1 } @@ -271,8 +271,8 @@ fn generate_from_multiple_fields_named< 'a > } -// qqq : document, add example of generated code -fn generate_from_multiple_fields< 'a > +// qqq : document, add example of generated code +fn generate_multiple_fields< 'a > ( item_name : &syn::Ident, generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, @@ -301,7 +301,7 @@ fn generate_from_multiple_fields< 'a > #generics_where { #[ inline( always ) ] - // fn from( src: (i32, bool) ) -> Self + // fn from( src : (i32, bool) ) -> Self fn from( src : ( #( #field_types ),* ) ) -> Self { // StructWithManyFields( src.0, src.1 ) @@ -311,7 +311,7 @@ fn generate_from_multiple_fields< 'a > } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code fn variant_generate ( item_name : &syn::Ident, diff --git a/module/core/derive_tools_meta/src/derive/inner_from.rs b/module/core/derive_tools_meta/src/derive/inner_from.rs index 23020e93d4..2226df43d8 100644 --- a/module/core/derive_tools_meta/src/derive/inner_from.rs +++ b/module/core/derive_tools_meta/src/derive/inner_from.rs @@ -57,7 +57,7 @@ pub fn inner_from( input : proc_macro::TokenStream ) -> Result< proc_macro2::Tok Ok( result ) } -// qqq : document, add example of generated code +// qqq : document, add example of generated code fn from_impl_named ( item_name : &syn::Ident, @@ -73,8 +73,8 @@ fn from_impl_named impl From< #item_name > for #field_type { #[ inline( always ) ] - // fm from( src: MyStruct ) -> Self - fn from( src: #item_name ) -> Self + // fm from( src : MyStruct ) -> Self + fn from( src : #item_name ) -> Self { // src.a src.#field_name @@ -83,7 +83,7 @@ fn from_impl_named } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code fn from_impl ( item_name : &syn::Ident, @@ -98,8 +98,8 @@ fn from_impl impl From< #item_name > for #field_type { #[ inline( always ) ] - // fn from( src: IsTransparent ) -> Self - fn from( src: #item_name ) -> Self + // fn from( src : IsTransparent ) -> Self + fn from( src : #item_name ) -> Self { src.0 } @@ -107,7 +107,7 @@ fn from_impl } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code fn from_impl_multiple_fields< 'a > ( item_name : &syn::Ident, @@ -123,8 +123,8 @@ fn from_impl_multiple_fields< 'a > impl From< #item_name > for ( #(#field_types), *) { #[ inline( always ) ] - // fn from( src: StructWithManyFields ) -> Self - fn from( src: #item_name ) -> Self + // fn from( src : StructWithManyFields ) -> Self + fn from( src : #item_name ) -> Self { //( src.0, src.1 ) (#(#params), *) @@ -133,7 +133,7 @@ fn from_impl_multiple_fields< 'a > } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code fn unit( item_name : &syn::Ident ) -> proc_macro2::TokenStream { qt! @@ -144,8 +144,8 @@ fn unit( item_name : &syn::Ident ) -> proc_macro2::TokenStream impl From< #item_name > for () { #[ inline( always ) ] - // fn from( src: UnitStruct ) -> () - fn from( src: #item_name ) -> () + // fn from( src : UnitStruct ) -> () + fn from( src : #item_name ) -> () { () } diff --git a/module/core/derive_tools_meta/src/derive/new.rs b/module/core/derive_tools_meta/src/derive/new.rs new file mode 100644 index 0000000000..4bfb0761bb --- /dev/null +++ b/module/core/derive_tools_meta/src/derive/new.rs @@ -0,0 +1,403 @@ +use super::*; +use macro_tools:: +{ + attr, + diag, + generic_params, + item_struct, + struct_like::StructLike, + Result, +}; + +#[ path = "from/field_attributes.rs" ] +mod field_attributes; +use field_attributes::*; +#[ path = "from/item_attributes.rs" ] +mod item_attributes; +use item_attributes::*; + +// + +// xxx : qqq : implement +pub fn new( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStream > +{ + // use macro_tools::quote::ToTokens; + + let original_input = input.clone(); + let parsed = syn::parse::< StructLike >( input )?; + let has_debug = attr::has_debug( parsed.attrs().iter() )?; + let item_attrs = ItemAttributes::from_attrs( parsed.attrs().iter() )?; + let item_name = &parsed.ident(); + + let ( _generics_with_defaults, generics_impl, generics_ty, generics_where ) + = generic_params::decompose( &parsed.generics() ); + + let result = match parsed + { + StructLike::Unit( ref item ) | StructLike::Struct( ref item ) => + { + + let mut field_types = item_struct::field_types( &item ); + let field_names = item_struct::field_names( &item ); + + match ( field_types.len(), field_names ) + { + ( 0, _ ) => + generate_unit + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + ), + ( 1, Some( mut field_names ) ) => + generate_single_field_named + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + field_names.next().unwrap(), + &field_types.next().unwrap(), + ), + ( 1, None ) => + generate_single_field + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &field_types.next().unwrap(), + ), + ( _, Some( field_names ) ) => + generate_multiple_fields_named + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + field_names, + field_types, + ), + ( _, None ) => + generate_multiple_fields + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + field_types, + ), + } + + }, + StructLike::Enum( ref item ) => + { + + let variants_result : Result< Vec< proc_macro2::TokenStream > > = item.variants.iter().map( | variant | + { + variant_generate + ( + item_name, + &item_attrs, + &generics_impl, + &generics_ty, + &generics_where, + variant, + &original_input, + ) + }).collect(); + + let variants = variants_result?; + + qt! + { + #( #variants )* + } + }, + }; + + if has_debug + { + let about = format!( "derive : New\nstructure : {item_name}" ); + diag::report_print( about, &original_input, &result ); + } + + Ok( result ) +} + +// xxx : qqq : implement +// qqq : document, add example of generated code +fn generate_unit +( + item_name : &syn::Ident, + generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_ty : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_where: &syn::punctuated::Punctuated< syn::WherePredicate, syn::token::Comma >, +) +-> proc_macro2::TokenStream +{ + qt! + { + // impl UnitStruct + impl< #generics_impl > #item_name< #generics_ty > + where + #generics_where + { + #[ inline( always ) ] + fn new() -> Self + { + Self + } + } + } +} + +// xxx : qqq : implement +// qqq : document, add example of generated code +fn generate_single_field_named +( + item_name : &syn::Ident, + generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_ty : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_where: &syn::punctuated::Punctuated< syn::WherePredicate, syn::token::Comma >, + field_name : &syn::Ident, + field_type : &syn::Type, +) +-> proc_macro2::TokenStream +{ + qt! + { + #[ automatically_derived ] + // impl MyStruct + impl< #generics_impl > #item_name< #generics_ty > + where + #generics_where + { + #[ inline( always ) ] + // fn new( src : i32 ) -> Self + fn new( src : #field_type ) -> Self + { + // Self { a: src } + Self { #field_name: src } + } + } + } +} + +// xxx : qqq : implement +// qqq : document, add example of generated code +fn generate_single_field +( + item_name : &syn::Ident, + generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_ty : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_where: &syn::punctuated::Punctuated< syn::WherePredicate, syn::token::Comma >, + field_type : &syn::Type, +) +-> proc_macro2::TokenStream +{ + + qt! + { + #[automatically_derived] + // impl IsTransparent + impl< #generics_impl > #item_name< #generics_ty > + where + #generics_where + { + #[ inline( always ) ] + // fn new( src : bool ) -> Self + fn new( src : #field_type ) -> Self + { + // Self( src ) + Self( src ) + } + } + } +} + +// xxx : qqq : implement +// qqq : document, add example of generated code +fn generate_multiple_fields_named< 'a > +( + item_name : &syn::Ident, + generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_ty : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_where: &syn::punctuated::Punctuated< syn::WherePredicate, syn::token::Comma >, + field_names : Box< dyn macro_tools::IterTrait< 'a, &'a syn::Ident > + '_ >, + field_types : impl macro_tools::IterTrait< 'a, &'a syn::Type >, +) +-> proc_macro2::TokenStream +{ + + let params : Vec< proc_macro2::TokenStream > = field_names + .enumerate() + .map(| ( index, field_name ) | + { + let index = index.to_string().parse::< proc_macro2::TokenStream >().unwrap(); + qt! { #field_name : src.#index } + }) + .collect(); + + let field_types : Vec< _ > = field_types.collect(); + qt! + { + // impl StructNamedFields + impl< #generics_impl > #item_name< #generics_ty > + where + #generics_where + { + #[ inline( always ) ] + // fn new( src : ( i32, bool ) ) -> Self + fn new( src : ( #( #field_types ),* ) ) -> Self + { + // StructNamedFields{ a : src.0, b : src.1 } + #item_name { #(#params),* } + } + } + } + +} + +// xxx : qqq : implement +// qqq : document, add example of generated code +fn generate_multiple_fields< 'a > +( + item_name : &syn::Ident, + generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_ty : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_where: &syn::punctuated::Punctuated< syn::WherePredicate, syn::token::Comma >, + field_types : impl macro_tools::IterTrait< 'a, &'a macro_tools::syn::Type >, +) +-> proc_macro2::TokenStream +{ + + let params : Vec< proc_macro2::TokenStream > = ( 0..field_types.len() ) + .map( | index | + { + let index = index.to_string().parse::< proc_macro2::TokenStream >().unwrap(); + qt!( src.#index ) + }) + .collect(); + + let field_types : Vec< _ > = field_types.collect(); + + qt! + { + // impl StructWithManyFields + impl< #generics_impl > #item_name< #generics_ty > + where + #generics_where + { + #[ inline( always ) ] + // fn new( src : (i32, bool) ) -> Self + fn new( src : ( #( #field_types ),* ) ) -> Self + { + // StructWithManyFields( src.0, src.1 ) + #item_name( #( #params ),* ) + } + } + } +} + +// xxx : qqq : implement +// qqq : document, add example of generated code +fn variant_generate +( + item_name : &syn::Ident, + item_attrs : &ItemAttributes, + generics_impl : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_ty : &syn::punctuated::Punctuated< syn::GenericParam, syn::token::Comma >, + generics_where: &syn::punctuated::Punctuated< syn::WherePredicate, syn::token::Comma >, + variant : &syn::Variant, + original_input : &proc_macro::TokenStream, +) +-> Result< proc_macro2::TokenStream > +{ + let variant_name = &variant.ident; + let fields = &variant.fields; + let attrs = FieldAttributes::from_attrs( variant.attrs.iter() )?; + + if !attrs.config.enabled.value( item_attrs.config.enabled.value( true ) ) + { + return Ok( qt!{} ) + } + + if fields.len() <= 0 + { + return Ok( qt!{} ) + } + + let ( args, use_src ) = if fields.len() == 1 + { + let field = fields.iter().next().unwrap(); + ( + qt!{ #field }, + qt!{ src }, + ) + } + else + { + let src_i = ( 0..fields.len() ).map( | e | + { + let i = syn::Index::from( e ); + qt!{ src.#i, } + }); + ( + qt!{ #fields }, + qt!{ #( #src_i )* }, + // qt!{ src.0, src.1 }, + ) + }; + + // qqq : make `debug` working for all branches + if attrs.config.debug.value( false ) + { + let debug = format! + ( + r#" +#[ automatically_derived ] +impl< {0} > {item_name}< {1} > +where + {2} +{{ + #[ inline ] + fn new( src : {args} ) -> Self + {{ + Self::{variant_name}( {use_src} ) + }} +}} + "#, + format!( "{}", qt!{ #generics_impl } ), + format!( "{}", qt!{ #generics_ty } ), + format!( "{}", qt!{ #generics_where } ), + ); + let about = format! + ( +r#"derive : New +item : {item_name} +field : {variant_name}"#, + ); + diag::report_print( about, original_input, debug ); + } + + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > #item_name< #generics_ty > + where + #generics_where + { + #[ inline ] + fn new( src : #args ) -> Self + { + Self::#variant_name( #use_src ) + } + } + } + ) + +} diff --git a/module/core/derive_tools_meta/src/lib.rs b/module/core/derive_tools_meta/src/lib.rs index b2b6f19f6b..1a034690bc 100644 --- a/module/core/derive_tools_meta/src/lib.rs +++ b/module/core/derive_tools_meta/src/lib.rs @@ -19,19 +19,19 @@ )] #[ cfg( feature = "enabled" ) ] mod derive; -#[ cfg -( - any - ( - feature = "derive_as_mut", - feature = "derive_as_ref", - feature = "derive_deref", - feature = "derive_deref_mut", - feature = "derive_from", - feature = "derive_inner_from", - feature = "derive_variadic_from", - ) -)] +// #[ cfg +// ( +// any +// ( +// feature = "derive_as_mut", +// feature = "derive_as_ref", +// feature = "derive_deref", +// feature = "derive_deref_mut", +// feature = "derive_from", +// feature = "derive_inner_from", +// feature = "derive_variadic_from", +// ) +// )] // #[ cfg( feature = "enabled" ) ] // use derive::*; @@ -91,6 +91,61 @@ pub fn from( input : proc_macro::TokenStream ) -> proc_macro::TokenStream } } +/// +/// Provides an automatic `new` implementation for struct wrapping a single value. +/// +/// This macro simplifies the conversion of an inner type to an outer struct type +/// when the outer type is a simple wrapper around the inner type. +/// +/// ## Example Usage +/// +/// Instead of manually implementing `new` for `IsTransparent`: +/// +/// ```rust +/// pub struct IsTransparent( bool ); +/// +/// impl IsTransparent +/// { +/// #[ inline( always ) ] +/// fn new( src : bool ) -> Self +/// { +/// Self( src ) +/// } +/// } +/// ``` +/// +/// Use `#[ derive( New ) ]` to automatically generate the implementation: +/// +/// ```rust +/// # use derive_tools_meta::*; +/// #[ derive( New ) ] +/// pub struct IsTransparent( bool ); +/// ``` +/// +/// The macro facilitates the conversion without additional boilerplate code. +/// + +#[ cfg( feature = "enabled" ) ] +#[ cfg( feature = "derive_new" ) ] +#[ proc_macro_derive +( + New, + attributes + ( + debug, // struct + new, // field + ) +)] +pub fn new( input : proc_macro::TokenStream ) -> proc_macro::TokenStream +{ + let result = derive::new::new( input ); + match result + { + Ok( stream ) => stream.into(), + Err( err ) => err.to_compile_error().into(), + } +} + // /// // /// Alias for derive `From`. Provides an automatic `From` implementation for struct wrapping a single value. // /// @@ -128,7 +183,7 @@ pub fn from( input : proc_macro::TokenStream ) -> proc_macro::TokenStream // #[ cfg( feature = "enabled" ) ] // #[ cfg( feature = "derive_from" ) ] // #[ proc_macro_derive( FromInner, attributes( debug ) ) ] -// pub fn from_inner( input : proc_macro::TokenStream ) -> proc_macro::TokenStream +// pub fn from( input : proc_macro::TokenStream ) -> proc_macro::TokenStream // { // let result = derive::from::from( input ); // match result diff --git a/module/core/former/Readme.md b/module/core/former/Readme.md index 68c270f3b8..05852dba11 100644 --- a/module/core/former/Readme.md +++ b/module/core/former/Readme.md @@ -371,7 +371,7 @@ The provided code snippet illustrates a basic use-case of the Former, which is u where Src : ::core::convert::Into< i32 >, { debug_assert!(self.storage.age.is_none()); - self.storage.age = ::core::option::Option::Some(::core::convert::Into::into(src)); + self.storage.age = ::core::option::Option::Some(::core::convert::Into::into( src )); self } @@ -380,7 +380,7 @@ The provided code snippet illustrates a basic use-case of the Former, which is u where Src : ::core::convert::Into< String >, { debug_assert!(self.storage.username.is_none()); - self.storage.username = ::core::option::Option::Some(::core::convert::Into::into(src)); + self.storage.username = ::core::option::Option::Some(::core::convert::Into::into( src )); self } @@ -389,7 +389,7 @@ The provided code snippet illustrates a basic use-case of the Former, which is u where Src : ::core::convert::Into< String >, { debug_assert!(self.storage.bio_optional.is_none()); - self.storage.bio_optional = ::core::option::Option::Some(::core::convert::Into::into(src)); + self.storage.bio_optional = ::core::option::Option::Some(::core::convert::Into::into( src )); self } } diff --git a/module/core/former/examples/former_trivial_expaned.rs b/module/core/former/examples/former_trivial_expaned.rs index 484f19262b..4968b11838 100644 --- a/module/core/former/examples/former_trivial_expaned.rs +++ b/module/core/former/examples/former_trivial_expaned.rs @@ -317,7 +317,7 @@ fn main() where Src : ::core::convert::Into< i32 >, { debug_assert!(self.storage.age.is_none()); - self.storage.age = ::core::option::Option::Some(::core::convert::Into::into(src)); + self.storage.age = ::core::option::Option::Some(::core::convert::Into::into( src )); self } @@ -326,7 +326,7 @@ fn main() where Src : ::core::convert::Into< String >, { debug_assert!(self.storage.username.is_none()); - self.storage.username = ::core::option::Option::Some(::core::convert::Into::into(src)); + self.storage.username = ::core::option::Option::Some(::core::convert::Into::into( src )); self } @@ -335,7 +335,7 @@ fn main() where Src : ::core::convert::Into< String >, { debug_assert!(self.storage.bio_optional.is_none()); - self.storage.bio_optional = ::core::option::Option::Some(::core::convert::Into::into(src)); + self.storage.bio_optional = ::core::option::Option::Some(::core::convert::Into::into( src )); self } } diff --git a/module/core/macro_tools/src/diag.rs b/module/core/macro_tools/src/diag.rs index 80cc16b563..10a7e9e0a5 100644 --- a/module/core/macro_tools/src/diag.rs +++ b/module/core/macro_tools/src/diag.rs @@ -229,13 +229,13 @@ pub( crate ) mod private #[ macro_export ] macro_rules! tree_print { - ( $src:expr ) => + ( $src :expr ) => {{ let result = $crate::tree_diagnostics_str!( $src ); println!( "{}", result ); result }}; - ( $( $src:expr ),+ $(,)? ) => + ( $( $src :expr ),+ $(,)? ) => {{ $( $crate::tree_print!( $src ) );+ }}; @@ -257,13 +257,13 @@ pub( crate ) mod private #[ macro_export ] macro_rules! code_print { - ( $src:expr ) => + ( $src :expr ) => {{ let result = $crate::code_diagnostics_str!( $src ); println!( "{}", result ); result }}; - ( $( $src:expr ),+ $(,)? ) => + ( $( $src :expr ),+ $(,)? ) => {{ $( $crate::code_print!( $src ) );+ }}; @@ -276,7 +276,7 @@ pub( crate ) mod private #[ macro_export ] macro_rules! tree_diagnostics_str { - ( $src:expr ) => + ( $src :expr ) => {{ let src2 = &$src; format!( "{} : {} :\n{:#?}", stringify!( $src ), $crate::qt!{ #src2 }, $src ) @@ -290,7 +290,7 @@ pub( crate ) mod private #[ macro_export ] macro_rules! code_diagnostics_str { - ( $src:expr ) => + ( $src :expr ) => {{ let src2 = &$src; format!( "{} : {}", stringify!( $src ), $crate::qt!{ #src2 } ) @@ -304,7 +304,7 @@ pub( crate ) mod private #[ macro_export ] macro_rules! code_to_str { - ( $src:expr ) => + ( $src :expr ) => {{ let src2 = &$src; format!( "{}", $crate::qt!{ #src2 } )