diff --git a/module/core/derive_tools/tests/inc/deref/bounds_inlined.rs b/module/core/derive_tools/tests/inc/deref/bounds_inlined.rs new file mode 100644 index 0000000000..8d31b2d56d --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/bounds_inlined.rs @@ -0,0 +1,10 @@ +use core::fmt::Debug; + +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct BoundsInlined< T : ToString, U : Debug >( T, U ); + +include!( "./only_tests/bounds_inlined.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/bounds_inlined_manual.rs b/module/core/derive_tools/tests/inc/deref/bounds_inlined_manual.rs new file mode 100644 index 0000000000..a6e4306512 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/bounds_inlined_manual.rs @@ -0,0 +1,17 @@ +use core::fmt::Debug; + +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct BoundsInlined< T : ToString, U : Debug >( T, U ); + +impl< T : ToString, U : Debug > Deref for BoundsInlined< T, U > +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/bounds_inlined.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/bounds_mixed.rs b/module/core/derive_tools/tests/inc/deref/bounds_mixed.rs new file mode 100644 index 0000000000..b539d8a862 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/bounds_mixed.rs @@ -0,0 +1,12 @@ +use core::fmt::Debug; + +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct BoundsMixed< T : ToString, U >( T, U ) +where + U : Debug; + +include!( "./only_tests/bounds_mixed.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/bounds_mixed_manual.rs b/module/core/derive_tools/tests/inc/deref/bounds_mixed_manual.rs new file mode 100644 index 0000000000..fbd3ae91e5 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/bounds_mixed_manual.rs @@ -0,0 +1,21 @@ +use core::fmt::Debug; + +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct BoundsMixed< T : ToString, U >( T, U ) +where + U : Debug; + +impl< T : ToString, U > Deref for BoundsMixed< T, U > +where + U : Debug, +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/bounds_mixed.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/bounds_where.rs b/module/core/derive_tools/tests/inc/deref/bounds_where.rs new file mode 100644 index 0000000000..9fb1a3f08a --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/bounds_where.rs @@ -0,0 +1,14 @@ +trait Trait<'a> {} +impl<'a> Trait<'a> for i32 {} + +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct BoundsWhere< T, U >( T, U ) +where + T : ToString, + for< 'a > U : Trait< 'a >; + +include!( "./only_tests/bounds_where.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/bounds_where_manual.rs b/module/core/derive_tools/tests/inc/deref/bounds_where_manual.rs new file mode 100644 index 0000000000..0b440a2c94 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/bounds_where_manual.rs @@ -0,0 +1,24 @@ +trait Trait<'a> {} +impl<'a> Trait<'a> for i32 {} + +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct BoundsWhere< T, U >( T, U ) +where + T : ToString, + for< 'a > U : Trait< 'a >; + +impl< T, U > Deref for BoundsWhere< T, U > +where + T : ToString, + for< 'a > U : Trait< 'a > +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/bounds_where.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_named.rs b/module/core/derive_tools/tests/inc/deref/enum_named.rs new file mode 100644 index 0000000000..98f87a8797 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_named.rs @@ -0,0 +1,12 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code) ] +#[ derive( Deref ) ] +enum EnumNamed +{ + A { a : String, b : i32 }, + B { a : String, b : i32 }, +} + +include!( "./only_tests/enum_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_named_empty.rs b/module/core/derive_tools/tests/inc/deref/enum_named_empty.rs new file mode 100644 index 0000000000..22a7c2f8ad --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_named_empty.rs @@ -0,0 +1,12 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code) ] +#[ derive( Deref ) ] +enum EnumNamedEmpty +{ + A {}, + B {}, +} + +include!( "./only_tests/enum_named_empty.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_named_empty_manual.rs b/module/core/derive_tools/tests/inc/deref/enum_named_empty_manual.rs new file mode 100644 index 0000000000..533dc78b0e --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_named_empty_manual.rs @@ -0,0 +1,19 @@ +use core::ops::Deref; + +#[ allow( dead_code) ] +enum EnumNamedEmpty +{ + A {}, + B {}, +} + +impl Deref for EnumNamedEmpty +{ + type Target = (); + fn deref( &self ) -> &Self::Target + { + &() + } +} + +include!( "./only_tests/enum_named_empty.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_named_manual.rs b/module/core/derive_tools/tests/inc/deref/enum_named_manual.rs new file mode 100644 index 0000000000..238192d0b3 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_named_manual.rs @@ -0,0 +1,22 @@ +use core::ops::Deref; + +#[ allow( dead_code) ] +enum EnumNamed +{ + A { a : String, b : i32 }, + B { a : String, b : i32 }, +} + +impl Deref for EnumNamed +{ + type Target = String; + fn deref( &self ) -> &Self::Target + { + match self + { + Self::A { a : v, ..} | Self::B { a : v, .. } => v + } + } +} + +include!( "./only_tests/enum_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_tuple.rs b/module/core/derive_tools/tests/inc/deref/enum_tuple.rs new file mode 100644 index 0000000000..2c6701331b --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_tuple.rs @@ -0,0 +1,12 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code) ] +#[ derive( Deref ) ] +enum EnumTuple +{ + A( String, i32 ), + B( String, i32 ), +} + +include!( "./only_tests/enum_tuple.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_tuple_empty.rs b/module/core/derive_tools/tests/inc/deref/enum_tuple_empty.rs new file mode 100644 index 0000000000..df423d9893 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_tuple_empty.rs @@ -0,0 +1,12 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code) ] +#[ derive( Deref ) ] +enum EnumTupleEmpty +{ + A(), + B(), +} + +include!( "./only_tests/enum_tuple_empty.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_tuple_empty_manual.rs b/module/core/derive_tools/tests/inc/deref/enum_tuple_empty_manual.rs new file mode 100644 index 0000000000..df54d3101e --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_tuple_empty_manual.rs @@ -0,0 +1,19 @@ +use core::ops::Deref; + +#[ allow( dead_code) ] +enum EnumTupleEmpty +{ + A(), + B(), +} + +impl Deref for EnumTupleEmpty +{ + type Target = (); + fn deref( &self ) -> &Self::Target + { + &() + } +} + +include!( "./only_tests/enum_tuple_empty.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_tuple_manual.rs b/module/core/derive_tools/tests/inc/deref/enum_tuple_manual.rs new file mode 100644 index 0000000000..efd16f833a --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_tuple_manual.rs @@ -0,0 +1,22 @@ +use core::ops::Deref; + +#[ allow( dead_code) ] +enum EnumTuple +{ + A( String, i32 ), + B( String, i32 ), +} + +impl Deref for EnumTuple +{ + type Target = String; + fn deref( &self ) -> &Self::Target + { + match self + { + Self::A( v, .. ) | Self::B( v, .. ) => v + } + } +} + +include!( "./only_tests/enum_tuple.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_unit.rs b/module/core/derive_tools/tests/inc/deref/enum_unit.rs new file mode 100644 index 0000000000..3ef2b204ce --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_unit.rs @@ -0,0 +1,12 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code) ] +#[ derive( Deref ) ] +enum EnumUnit +{ + A, + B, +} + +include!( "./only_tests/enum_unit.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/enum_unit_manual.rs b/module/core/derive_tools/tests/inc/deref/enum_unit_manual.rs new file mode 100644 index 0000000000..1cc6fdc94b --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/enum_unit_manual.rs @@ -0,0 +1,19 @@ +use core::ops::Deref; + +#[ allow( dead_code) ] +enum EnumUnit +{ + A, + B, +} + +impl Deref for EnumUnit +{ + type Target = (); + fn deref( &self ) -> &Self::Target + { + &() + } +} + +include!( "./only_tests/enum_unit.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_constants.rs b/module/core/derive_tools/tests/inc/deref/generics_constants.rs new file mode 100644 index 0000000000..f4198931f3 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_constants.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct GenericsConstants< const N : usize >( i32 ); + +include!( "./only_tests/generics_constants.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_constants_default.rs b/module/core/derive_tools/tests/inc/deref/generics_constants_default.rs new file mode 100644 index 0000000000..b5b03688dc --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_constants_default.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct GenericsConstantsDefault< const N : usize = 0 >( i32 ); + +include!( "./only_tests/generics_constants_default.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_constants_default_manual.rs b/module/core/derive_tools/tests/inc/deref/generics_constants_default_manual.rs new file mode 100644 index 0000000000..b56469fe34 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_constants_default_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct GenericsConstantsDefault< const N : usize = 0 >( i32 ); + +impl< const N : usize > Deref for GenericsConstantsDefault< N > +{ + type Target = i32; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/generics_constants_default.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_constants_manual.rs b/module/core/derive_tools/tests/inc/deref/generics_constants_manual.rs new file mode 100644 index 0000000000..fcc6b6da76 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_constants_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct GenericsConstants< const N : usize >( i32 ); + +impl< const N : usize > Deref for GenericsConstants< N > +{ + type Target = i32; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/generics_constants.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_lifetimes.rs b/module/core/derive_tools/tests/inc/deref/generics_lifetimes.rs new file mode 100644 index 0000000000..a308db8fee --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_lifetimes.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct GenericsLifetimes< 'a >( &'a i32 ); + +include!( "./only_tests/generics_lifetimes.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_lifetimes_manual.rs b/module/core/derive_tools/tests/inc/deref/generics_lifetimes_manual.rs new file mode 100644 index 0000000000..57f647c9ff --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_lifetimes_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct GenericsLifetimes< 'a >( &'a i32 ); + +impl< 'a > Deref for GenericsLifetimes< 'a > +{ + type Target = &'a i32; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/generics_lifetimes.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_types.rs b/module/core/derive_tools/tests/inc/deref/generics_types.rs new file mode 100644 index 0000000000..d6acb22702 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_types.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct GenericsTypes< T >( T ); + +include!( "./only_tests/generics_types.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_types_default.rs b/module/core/derive_tools/tests/inc/deref/generics_types_default.rs new file mode 100644 index 0000000000..f89f5d9430 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_types_default.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive ( Deref ) ] +struct GenericsTypesDefault< T = i32 >( T ); + +include!( "./only_tests/generics_types_default.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_types_default_manual.rs b/module/core/derive_tools/tests/inc/deref/generics_types_default_manual.rs new file mode 100644 index 0000000000..773061023c --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_types_default_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct GenericsTypesDefault< T = i32 >( T ); + +impl< T > Deref for GenericsTypesDefault< T > +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/generics_types_default.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/generics_types_manual.rs b/module/core/derive_tools/tests/inc/deref/generics_types_manual.rs new file mode 100644 index 0000000000..8b9ab74909 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/generics_types_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct GenericsTypes< T >( T ); + +impl< T > Deref for GenericsTypes< T > +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/generics_types.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/name_collisions.rs b/module/core/derive_tools/tests/inc/deref/name_collisions.rs new file mode 100644 index 0000000000..3a0fe2a74f --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/name_collisions.rs @@ -0,0 +1,23 @@ +#![ allow( non_snake_case ) ] +#![ allow( unused_imports ) ] + +use ::core::ops::Deref; +use derive_tools::Deref; + +pub mod core {} +pub mod std {} +pub mod marker {} + +pub mod FromString {} +pub mod FromPair {} +pub mod FromBin {} + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct NameCollisions +{ + a : i32, + b : String, +} + +include!( "./only_tests/name_collisions.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/bounds_inlined.rs b/module/core/derive_tools/tests/inc/deref/only_tests/bounds_inlined.rs new file mode 100644 index 0000000000..5fa47b683b --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/bounds_inlined.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = BoundsInlined::< String, i32 >( "boo".into(), 3 ); + let exp = "boo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/bounds_mixed.rs b/module/core/derive_tools/tests/inc/deref/only_tests/bounds_mixed.rs new file mode 100644 index 0000000000..198ddd7019 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/bounds_mixed.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = BoundsMixed::< String, i32 >( "boo".into(), 3 ); + let exp = "boo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/bounds_where.rs b/module/core/derive_tools/tests/inc/deref/only_tests/bounds_where.rs new file mode 100644 index 0000000000..a7733a9b5b --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/bounds_where.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = BoundsWhere::< String, i32 >( "boo".into(), 3 ); + let exp = "boo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/enum_named.rs b/module/core/derive_tools/tests/inc/deref/only_tests/enum_named.rs new file mode 100644 index 0000000000..fc6072f1cd --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/enum_named.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = EnumNamed::A { a : "boo".into(), b : 3 }; + let exp = "boo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/enum_named_empty.rs b/module/core/derive_tools/tests/inc/deref/only_tests/enum_named_empty.rs new file mode 100644 index 0000000000..d0062ce381 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/enum_named_empty.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = EnumNamedEmpty::A {}; + let exp = &(); + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/enum_tuple.rs b/module/core/derive_tools/tests/inc/deref/only_tests/enum_tuple.rs new file mode 100644 index 0000000000..b5a71cc2e1 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/enum_tuple.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = EnumTuple::A( "boo".into(), 3 ); + let exp = "boo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/enum_tuple_empty.rs b/module/core/derive_tools/tests/inc/deref/only_tests/enum_tuple_empty.rs new file mode 100644 index 0000000000..897c714aff --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/enum_tuple_empty.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = EnumTupleEmpty::A(); + let exp = &(); + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/enum_unit.rs b/module/core/derive_tools/tests/inc/deref/only_tests/enum_unit.rs new file mode 100644 index 0000000000..c6af5da907 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/enum_unit.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = EnumUnit::A; + let exp = &(); + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/generics_constants.rs b/module/core/derive_tools/tests/inc/deref/only_tests/generics_constants.rs new file mode 100644 index 0000000000..5a8c8f023e --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/generics_constants.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = GenericsConstants::< 0 >( 5 ); + let exp = &5; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/generics_constants_default.rs b/module/core/derive_tools/tests/inc/deref/only_tests/generics_constants_default.rs new file mode 100644 index 0000000000..c0cee0bd67 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/generics_constants_default.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = GenericsConstantsDefault::< 0 >( 5 ); + let exp = &5; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/generics_lifetimes.rs b/module/core/derive_tools/tests/inc/deref/only_tests/generics_lifetimes.rs new file mode 100644 index 0000000000..cdb4089835 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/generics_lifetimes.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = GenericsLifetimes( &3 ); + let exp = &&3; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/generics_types.rs b/module/core/derive_tools/tests/inc/deref/only_tests/generics_types.rs new file mode 100644 index 0000000000..da3b2c39f6 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/generics_types.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = GenericsTypes::< &str >( "boo" ); + let got = &"boo"; + let exp = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/generics_types_default.rs b/module/core/derive_tools/tests/inc/deref/only_tests/generics_types_default.rs new file mode 100644 index 0000000000..07e25da195 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/generics_types_default.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = GenericsTypesDefault( 2 ); + let got = &2; + let exp = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/name_collisions.rs b/module/core/derive_tools/tests/inc/deref/only_tests/name_collisions.rs new file mode 100644 index 0000000000..862e034763 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/name_collisions.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = NameCollisions { a : 5, b : "boo".into() }; + let exp = &5; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/struct_named.rs b/module/core/derive_tools/tests/inc/deref/only_tests/struct_named.rs new file mode 100644 index 0000000000..48675ce5f0 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/struct_named.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = StructNamed{ a : "boo".into(), b : 3 }; + let exp = "boo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/struct_named_empty.rs b/module/core/derive_tools/tests/inc/deref/only_tests/struct_named_empty.rs new file mode 100644 index 0000000000..9692ed2716 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/struct_named_empty.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = StructNamedEmpty{}; + let exp = &(); + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/struct_tuple.rs b/module/core/derive_tools/tests/inc/deref/only_tests/struct_tuple.rs new file mode 100644 index 0000000000..60bb72d77e --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/struct_tuple.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = StructTuple( "boo".into(), 3 ); + let exp = "boo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/struct_tuple_empty.rs b/module/core/derive_tools/tests/inc/deref/only_tests/struct_tuple_empty.rs new file mode 100644 index 0000000000..e2cc30db26 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/struct_tuple_empty.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = StructTupleEmpty(); + let exp = &(); + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/only_tests/struct_unit.rs b/module/core/derive_tools/tests/inc/deref/only_tests/struct_unit.rs new file mode 100644 index 0000000000..fdb39247cc --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/only_tests/struct_unit.rs @@ -0,0 +1,8 @@ +#[ test ] +fn deref() +{ + let a = StructUnit; + let exp = &(); + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref/struct_named.rs b/module/core/derive_tools/tests/inc/deref/struct_named.rs new file mode 100644 index 0000000000..470670a206 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_named.rs @@ -0,0 +1,12 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref) ] +struct StructNamed +{ + a : String, + b : i32, +} + +include!( "./only_tests/struct_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_named_empty.rs b/module/core/derive_tools/tests/inc/deref/struct_named_empty.rs new file mode 100644 index 0000000000..ebe217835d --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_named_empty.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive( Deref ) ] +struct StructNamedEmpty{} + +include!( "./only_tests/struct_named_empty.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_named_empty_manual.rs b/module/core/derive_tools/tests/inc/deref/struct_named_empty_manual.rs new file mode 100644 index 0000000000..efd92be335 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_named_empty_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct StructNamedEmpty{} + +impl Deref for StructNamedEmpty +{ + type Target = (); + fn deref( &self ) -> &Self::Target + { + &() + } +} + +include!( "./only_tests/struct_named_empty.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_named_manual.rs b/module/core/derive_tools/tests/inc/deref/struct_named_manual.rs new file mode 100644 index 0000000000..88e31246e6 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_named_manual.rs @@ -0,0 +1,19 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct StructNamed +{ + a : String, + b : i32, +} + +impl Deref for StructNamed +{ + type Target = String; + fn deref( &self ) -> &Self::Target + { + &self.a + } +} + +include!( "./only_tests/struct_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_tuple.rs b/module/core/derive_tools/tests/inc/deref/struct_tuple.rs new file mode 100644 index 0000000000..29c4b3c4ff --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_tuple.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive ( Deref ) ] +struct StructTuple( String, i32 ); + +include!( "./only_tests/struct_tuple.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_tuple_empty.rs b/module/core/derive_tools/tests/inc/deref/struct_tuple_empty.rs new file mode 100644 index 0000000000..57e2ea7257 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_tuple_empty.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive ( Deref ) ] +struct StructTupleEmpty(); + +include!( "./only_tests/struct_tuple_empty.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_tuple_empty_manual.rs b/module/core/derive_tools/tests/inc/deref/struct_tuple_empty_manual.rs new file mode 100644 index 0000000000..b239eccd44 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_tuple_empty_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct StructTupleEmpty(); + +impl Deref for StructTupleEmpty +{ + type Target = (); + fn deref( &self ) -> &Self::Target + { + &() + } +} + +include!( "./only_tests/struct_tuple_empty.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_tuple_manual.rs b/module/core/derive_tools/tests/inc/deref/struct_tuple_manual.rs new file mode 100644 index 0000000000..7a97225d3b --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_tuple_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct StructTuple( String, i32 ); + +impl Deref for StructTuple +{ + type Target = String; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +include!( "./only_tests/struct_tuple.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_unit.rs b/module/core/derive_tools/tests/inc/deref/struct_unit.rs new file mode 100644 index 0000000000..30c742a68e --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_unit.rs @@ -0,0 +1,8 @@ +use core::ops::Deref; +use derive_tools::Deref; + +#[ allow( dead_code ) ] +#[ derive ( Deref ) ] +struct StructUnit; + +include!( "./only_tests/struct_unit.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref/struct_unit_manual.rs b/module/core/derive_tools/tests/inc/deref/struct_unit_manual.rs new file mode 100644 index 0000000000..c43a711d50 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref/struct_unit_manual.rs @@ -0,0 +1,15 @@ +use core::ops::Deref; + +#[ allow( dead_code ) ] +struct StructUnit; + +impl Deref for StructUnit +{ + type Target = (); + fn deref( &self ) -> &Self::Target + { + &() + } +} + +include!( "./only_tests/struct_unit.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_manual_test.rs b/module/core/derive_tools/tests/inc/deref_manual_test.rs index 57c3994eea..3ad2c5c1b4 100644 --- a/module/core/derive_tools/tests/inc/deref_manual_test.rs +++ b/module/core/derive_tools/tests/inc/deref_manual_test.rs @@ -3,12 +3,27 @@ use super::*; // use diagnostics_tools::prelude::*; // use derives::*; -#[ derive( Debug, Clone, Copy, PartialEq ) ] -pub struct IsTransparent( bool ); +#[ derive( Debug, Clone, Copy, PartialEq, ) ] +pub struct IsTransparentSimple( bool ); -impl core::ops::Deref for IsTransparent +impl core::ops::Deref for IsTransparentSimple { type Target = bool; + #[ inline ( always) ] + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +#[ derive( Debug, Clone, Copy, PartialEq ) ] +pub struct IsTransparentComplex< 'a, 'b : 'a, T, U : ToString + ?Sized, const N : usize >( &'a T, core::marker::PhantomData< &'b U > ) +where 'a : 'b, T : AsRef< U >; + +impl< 'a, 'b : 'a, T, U : ToString + ?Sized, const N : usize > core::ops::Deref for IsTransparentComplex< 'a, 'b, T, U, N > +where 'a : 'b, T : AsRef< U > +{ + type Target = &'a T; #[ inline( always ) ] fn deref( &self ) -> &Self::Target { diff --git a/module/core/derive_tools/tests/inc/deref_mut/bounds_inlined.rs b/module/core/derive_tools/tests/inc/deref_mut/bounds_inlined.rs new file mode 100644 index 0000000000..ebb9fe65b6 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/bounds_inlined.rs @@ -0,0 +1,10 @@ +use core::fmt::Debug; + +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct BoundsInlined< T : ToString, U : Debug >( T, U ); + +include!( "./only_tests/bounds_inlined.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/bounds_inlined_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/bounds_inlined_manual.rs new file mode 100644 index 0000000000..84051196bf --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/bounds_inlined_manual.rs @@ -0,0 +1,24 @@ +use core::fmt::Debug; + +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct BoundsInlined< T : ToString, U : Debug >( T, U ); + +impl< T : ToString, U : Debug > Deref for BoundsInlined< T, U > +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl< T : ToString, U : Debug > DerefMut for BoundsInlined< T, U > +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +include!( "./only_tests/bounds_inlined.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/bounds_mixed.rs b/module/core/derive_tools/tests/inc/deref_mut/bounds_mixed.rs new file mode 100644 index 0000000000..5b8d13196f --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/bounds_mixed.rs @@ -0,0 +1,12 @@ +use core::fmt::Debug; + +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct BoundsMixed< T : ToString, U >( T, U ) +where + U : Debug; + +include!( "./only_tests/bounds_mixed.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/bounds_mixed_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/bounds_mixed_manual.rs new file mode 100644 index 0000000000..4bbfa33c21 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/bounds_mixed_manual.rs @@ -0,0 +1,31 @@ +use core::fmt::Debug; + +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct BoundsMixed< T : ToString, U >( T, U ) +where + U : Debug; + +impl< T : ToString, U > Deref for BoundsMixed< T, U > +where + U : Debug, +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl< T : ToString, U > DerefMut for BoundsMixed< T, U > +where + U : Debug, +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + + +include!( "./only_tests/bounds_mixed.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/bounds_where.rs b/module/core/derive_tools/tests/inc/deref_mut/bounds_where.rs new file mode 100644 index 0000000000..25a61c35d1 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/bounds_where.rs @@ -0,0 +1,14 @@ +trait Trait<'a> {} +impl<'a> Trait<'a> for i32 {} + +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct BoundsWhere< T, U >( T, U ) +where + T : ToString, + for< 'a > U : Trait< 'a >; + +include!( "./only_tests/bounds_where.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/bounds_where_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/bounds_where_manual.rs new file mode 100644 index 0000000000..3a61604a87 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/bounds_where_manual.rs @@ -0,0 +1,34 @@ +trait Trait<'a> {} +impl<'a> Trait<'a> for i32 {} + +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct BoundsWhere< T, U >( T, U ) +where + T : ToString, + for< 'a > U : Trait< 'a >; + +impl< T, U > Deref for BoundsWhere< T, U > +where + T : ToString, + for< 'a > U : Trait< 'a > +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl< T, U > DerefMut for BoundsWhere< T, U > +where + T : ToString, + for< 'a > U : Trait< 'a > +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +include!( "./only_tests/bounds_where.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/enum_named.rs b/module/core/derive_tools/tests/inc/deref_mut/enum_named.rs new file mode 100644 index 0000000000..f6ced02179 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/enum_named.rs @@ -0,0 +1,12 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code) ] +#[ derive( Deref, DerefMut ) ] +enum EnumNamed +{ + A { a : String, b : i32 }, + B { a : String, b : i32 }, +} + +include!( "./only_tests/enum_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/enum_named_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/enum_named_manual.rs new file mode 100644 index 0000000000..096c52cf20 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/enum_named_manual.rs @@ -0,0 +1,32 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code) ] +enum EnumNamed +{ + A { a : String, b : i32 }, + B { a : String, b : i32 }, +} + +impl Deref for EnumNamed +{ + type Target = String; + fn deref( &self ) -> &Self::Target + { + match self + { + Self::A { a : v, ..} | Self::B { a : v, .. } => v + } + } +} +impl DerefMut for EnumNamed +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + match self + { + Self::A { a : v, ..} | Self::B { a : v, .. } => v + } + } +} + +include!( "./only_tests/enum_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/enum_tuple.rs b/module/core/derive_tools/tests/inc/deref_mut/enum_tuple.rs new file mode 100644 index 0000000000..8957e47e25 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/enum_tuple.rs @@ -0,0 +1,12 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code) ] +#[ derive( Deref, DerefMut ) ] +enum EnumTuple +{ + A( String, i32 ), + B( String, i32 ), +} + +include!( "./only_tests/enum_tuple.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/enum_tuple_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/enum_tuple_manual.rs new file mode 100644 index 0000000000..c04408ef45 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/enum_tuple_manual.rs @@ -0,0 +1,32 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code) ] +enum EnumTuple +{ + A( String, i32 ), + B( String, i32 ), +} + +impl Deref for EnumTuple +{ + type Target = String; + fn deref( &self ) -> &Self::Target + { + match self + { + Self::A( v, .. ) | Self::B( v, .. ) => v + } + } +} +impl DerefMut for EnumTuple +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + match self + { + Self::A( v, .. ) | Self::B( v, .. ) => v + } + } +} + +include!( "./only_tests/enum_tuple.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_constants.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_constants.rs new file mode 100644 index 0000000000..33ff0ff89f --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_constants.rs @@ -0,0 +1,8 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct GenericsConstants< const N : usize >( i32 ); + +include!( "./only_tests/generics_constants.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_constants_default.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_constants_default.rs new file mode 100644 index 0000000000..a5591f7569 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_constants_default.rs @@ -0,0 +1,8 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct GenericsConstantsDefault< const N : usize = 0 >( i32 ); + +include!( "./only_tests/generics_constants_default.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_constants_default_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_constants_default_manual.rs new file mode 100644 index 0000000000..6348347253 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_constants_default_manual.rs @@ -0,0 +1,22 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct GenericsConstantsDefault< const N : usize = 0 >( i32 ); + +impl< const N : usize > Deref for GenericsConstantsDefault< N > +{ + type Target = i32; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl< const N : usize > DerefMut for GenericsConstantsDefault< N > +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +include!( "./only_tests/generics_constants_default.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_constants_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_constants_manual.rs new file mode 100644 index 0000000000..efe320d565 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_constants_manual.rs @@ -0,0 +1,22 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct GenericsConstants< const N : usize >( i32 ); + +impl< const N : usize > Deref for GenericsConstants< N > +{ + type Target = i32; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl< const N : usize > DerefMut for GenericsConstants< N > +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +include!( "./only_tests/generics_constants.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_lifetimes.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_lifetimes.rs new file mode 100644 index 0000000000..3a3ff17bdd --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_lifetimes.rs @@ -0,0 +1,8 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct GenericsLifetimes< 'a >( &'a i32 ); + +include!( "./only_tests/generics_lifetimes.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_lifetimes_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_lifetimes_manual.rs new file mode 100644 index 0000000000..7d16d699c9 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_lifetimes_manual.rs @@ -0,0 +1,22 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct GenericsLifetimes< 'a >( &'a i32 ); + +impl< 'a > Deref for GenericsLifetimes< 'a > +{ + type Target = &'a i32; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl< 'a > DerefMut for GenericsLifetimes< 'a > +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +include!( "./only_tests/generics_lifetimes.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_types.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_types.rs new file mode 100644 index 0000000000..dc64f4ec43 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_types.rs @@ -0,0 +1,8 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct GenericsTypes< T >( T ); + +include!( "./only_tests/generics_types.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_types_default.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_types_default.rs new file mode 100644 index 0000000000..8680954981 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_types_default.rs @@ -0,0 +1,8 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive ( Deref, DerefMut ) ] +struct GenericsTypesDefault< T = i32 >( T ); + +include!( "./only_tests/generics_types_default.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_types_default_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_types_default_manual.rs new file mode 100644 index 0000000000..00b41e2ece --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_types_default_manual.rs @@ -0,0 +1,22 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct GenericsTypesDefault< T = i32 >( T ); + +impl< T > Deref for GenericsTypesDefault< T > +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl< T > DerefMut for GenericsTypesDefault< T > +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +include!( "./only_tests/generics_types_default.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/generics_types_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/generics_types_manual.rs new file mode 100644 index 0000000000..2bdeb3183f --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/generics_types_manual.rs @@ -0,0 +1,22 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct GenericsTypes< T >( T ); + +impl< T > Deref for GenericsTypes< T > +{ + type Target = T; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl< T > DerefMut for GenericsTypes< T > +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +include!( "./only_tests/generics_types.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/name_collisions.rs b/module/core/derive_tools/tests/inc/deref_mut/name_collisions.rs new file mode 100644 index 0000000000..66f82b0fb1 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/name_collisions.rs @@ -0,0 +1,23 @@ +#![ allow( non_snake_case ) ] +#![ allow( unused_imports ) ] + +use ::core::ops::Deref; +use derive_tools::{ Deref, DerefMut }; + +pub mod core {} +pub mod std {} +pub mod marker {} + +pub mod FromString {} +pub mod FromPair {} +pub mod FromBin {} + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct NameCollisions +{ + a : i32, + b : String, +} + +include!( "./only_tests/name_collisions.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_inlined.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_inlined.rs new file mode 100644 index 0000000000..c70dca1ce0 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_inlined.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = BoundsInlined::< String, i32 >( "boo".into(), 3 ); + *a = "foo".into(); + let exp = "foo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_mixed.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_mixed.rs new file mode 100644 index 0000000000..d511bc31cf --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_mixed.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = BoundsMixed::< String, i32 >( "boo".into(), 3 ); + *a = "foo".into(); + let exp = "foo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_where.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_where.rs new file mode 100644 index 0000000000..4606689250 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/bounds_where.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = BoundsWhere::< String, i32 >( "boo".into(), 3 ); + *a = "foo".into(); + let exp = "foo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/enum_named.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/enum_named.rs new file mode 100644 index 0000000000..a659c7aaeb --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/enum_named.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = EnumNamed::A { a : "boo".into(), b : 3 }; + *a = "foo".into(); + let exp = "foo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/enum_tuple.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/enum_tuple.rs new file mode 100644 index 0000000000..5228288f0a --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/enum_tuple.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = EnumTuple::A( "boo".into(), 3 ); + *a = "foo".into(); + let exp = "foo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_constants.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_constants.rs new file mode 100644 index 0000000000..54b9f97fd0 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_constants.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = GenericsConstants::< 0 >( 5 ); + *a = -5; + let exp = &-5; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_constants_default.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_constants_default.rs new file mode 100644 index 0000000000..3430c4e252 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_constants_default.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = GenericsConstantsDefault::< 0 >( 5 ); + *a = -5; + let exp = &-5; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_lifetimes.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_lifetimes.rs new file mode 100644 index 0000000000..9428644f06 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_lifetimes.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = GenericsLifetimes( &3 ); + *a = &-3; + let exp = &&-3; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_types.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_types.rs new file mode 100644 index 0000000000..336a59148a --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_types.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = GenericsTypes::< &str >( "boo" ); + *a = "foo"; + let got = &"foo"; + let exp = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_types_default.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_types_default.rs new file mode 100644 index 0000000000..efc09bda56 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/generics_types_default.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = GenericsTypesDefault( 2 ); + *a = -2; + let got = &-2; + let exp = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/name_collisions.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/name_collisions.rs new file mode 100644 index 0000000000..f2a24e90a7 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/name_collisions.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = NameCollisions { a : 5, b : "boo".into() }; + *a = -5; + let exp = &-5; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/struct_named.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/struct_named.rs new file mode 100644 index 0000000000..edcacf11f1 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/struct_named.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = StructNamed{ a : "boo".into(), b : 3 }; + *a = "foo".into(); + let exp = "foo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/only_tests/struct_tuple.rs b/module/core/derive_tools/tests/inc/deref_mut/only_tests/struct_tuple.rs new file mode 100644 index 0000000000..a36de0ea6f --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/only_tests/struct_tuple.rs @@ -0,0 +1,9 @@ +#[ test ] +fn deref_mut() +{ + let mut a = StructTuple( "boo".into(), 3 ); + *a = "foo".into(); + let exp = "foo"; + let got = a.deref(); + assert_eq!(got, exp); +} diff --git a/module/core/derive_tools/tests/inc/deref_mut/struct_named.rs b/module/core/derive_tools/tests/inc/deref_mut/struct_named.rs new file mode 100644 index 0000000000..4cf35f59df --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/struct_named.rs @@ -0,0 +1,12 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive( Deref, DerefMut ) ] +struct StructNamed +{ + a : String, + b : i32, +} + +include!( "./only_tests/struct_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/struct_named_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/struct_named_manual.rs new file mode 100644 index 0000000000..24cff7f14a --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/struct_named_manual.rs @@ -0,0 +1,26 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct StructNamed +{ + a : String, + b : i32, +} + +impl Deref for StructNamed +{ + type Target = String; + fn deref( &self ) -> &Self::Target + { + &self.a + } +} +impl DerefMut for StructNamed +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.a + } +} + +include!( "./only_tests/struct_named.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/struct_tuple.rs b/module/core/derive_tools/tests/inc/deref_mut/struct_tuple.rs new file mode 100644 index 0000000000..5d5d439859 --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/struct_tuple.rs @@ -0,0 +1,8 @@ +use core::ops::{ Deref }; +use derive_tools::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +#[ derive ( Deref, DerefMut ) ] +struct StructTuple( String, i32 ); + +include!( "./only_tests/struct_tuple.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut/struct_tuple_manual.rs b/module/core/derive_tools/tests/inc/deref_mut/struct_tuple_manual.rs new file mode 100644 index 0000000000..de82ac329c --- /dev/null +++ b/module/core/derive_tools/tests/inc/deref_mut/struct_tuple_manual.rs @@ -0,0 +1,22 @@ +use core::ops::{ Deref, DerefMut }; + +#[ allow( dead_code ) ] +struct StructTuple( String, i32 ); + +impl Deref for StructTuple +{ + type Target = String; + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} +impl DerefMut for StructTuple +{ + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +include!( "./only_tests/struct_tuple.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_mut_manual_test.rs b/module/core/derive_tools/tests/inc/deref_mut_manual_test.rs index c559375af5..a5541fbc52 100644 --- a/module/core/derive_tools/tests/inc/deref_mut_manual_test.rs +++ b/module/core/derive_tools/tests/inc/deref_mut_manual_test.rs @@ -3,12 +3,36 @@ use super::*; // use diagnostics_tools::prelude::*; // use derives::*; -#[ derive( Debug, Clone, Copy, PartialEq ) ] -pub struct IsTransparent( bool ); +#[ derive( Debug, Clone, Copy, PartialEq, ) ] +pub struct IsTransparentSimple( bool ); -impl core::ops::Deref for IsTransparent +impl core::ops::Deref for IsTransparentSimple { type Target = bool; + #[ inline ( always) ] + fn deref( &self ) -> &Self::Target + { + &self.0 + } +} + +impl core::ops::DerefMut for IsTransparentSimple +{ + #[ inline( always ) ] + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } +} + +#[ derive( Debug, Clone, Copy, PartialEq ) ] +pub struct IsTransparentComplex< 'a, 'b : 'a, T, U : ToString + ?Sized, const N : usize >( &'a T, core::marker::PhantomData< &'b U > ) +where 'a : 'b, T : AsRef< U >; + +impl< 'a, 'b : 'a, T, U : ToString + ?Sized, const N : usize > core::ops::Deref for IsTransparentComplex< 'a, 'b, T, U, N > +where 'a : 'b, T : AsRef< U > +{ + type Target = &'a T; #[ inline( always ) ] fn deref( &self ) -> &Self::Target { @@ -16,7 +40,8 @@ impl core::ops::Deref for IsTransparent } } -impl core::ops::DerefMut for IsTransparent +impl< 'a, 'b : 'a, T, U : ToString + ?Sized, const N : usize > core::ops::DerefMut for IsTransparentComplex< 'a, 'b, T, U, N > +where 'a : 'b, T : AsRef< U > { #[ inline( always ) ] fn deref_mut( &mut self ) -> &mut Self::Target diff --git a/module/core/derive_tools/tests/inc/deref_mut_test.rs b/module/core/derive_tools/tests/inc/deref_mut_test.rs index 8624d1d43c..aae9db0775 100644 --- a/module/core/derive_tools/tests/inc/deref_mut_test.rs +++ b/module/core/derive_tools/tests/inc/deref_mut_test.rs @@ -3,21 +3,13 @@ use super::*; // use diagnostics_tools::prelude::*; // use derives::*; -#[ derive( Debug, Clone, Copy, PartialEq ) ] -pub struct IsTransparent( bool ); - -impl std::ops::Deref for IsTransparent { - type Target = bool; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl std::ops::DerefMut for IsTransparent { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} +#[ derive( Debug, Clone, Copy, PartialEq, the_module::Deref, the_module::DerefMut ) ] +pub struct IsTransparentSimple( bool ); + +#[ derive( Debug, Clone, Copy, PartialEq, the_module::Deref, the_module::DerefMut ) ] +pub struct IsTransparentComplex< 'a, 'b : 'a, T, U : ToString + ?Sized, const N : usize >( &'a T, core::marker::PhantomData< &'b U > ) +where + 'a : 'b, + T : AsRef< U >; include!( "./only_test/deref_mut.rs" ); diff --git a/module/core/derive_tools/tests/inc/deref_test.rs b/module/core/derive_tools/tests/inc/deref_test.rs index e7e9fc2772..df445ac9b4 100644 --- a/module/core/derive_tools/tests/inc/deref_test.rs +++ b/module/core/derive_tools/tests/inc/deref_test.rs @@ -4,6 +4,12 @@ use super::*; // use derives::*; #[ derive( Debug, Clone, Copy, PartialEq, the_module::Deref ) ] -pub struct IsTransparent( bool ); +pub struct IsTransparentSimple( bool ); + +#[ derive( Debug, Clone, Copy, PartialEq, the_module::Deref ) ] +pub struct IsTransparentComplex< 'a, 'b : 'a, T, U : ToString + ?Sized, const N : usize >( &'a T, core::marker::PhantomData< &'b U > ) +where + 'a : 'b, + T : AsRef< U >; include!( "./only_test/deref.rs" ); diff --git a/module/core/derive_tools/tests/inc/mod.rs b/module/core/derive_tools/tests/inc/mod.rs index f100189dbf..518d8757d4 100644 --- a/module/core/derive_tools/tests/inc/mod.rs +++ b/module/core/derive_tools/tests/inc/mod.rs @@ -39,17 +39,117 @@ mod as_ref_manual_test; #[ cfg( feature = "derive_as_ref" ) ] mod as_ref_test; -mod deref_manual_test; #[ cfg( feature = "derive_deref" ) ] -mod deref_test; +#[ path = "deref" ] +mod deref_tests +{ + #[ allow( unused_imports ) ] + use super::*; + + // + + mod struct_unit; + mod struct_unit_manual; + mod struct_tuple; + mod struct_tuple_manual; + mod struct_tuple_empty; + mod struct_tuple_empty_manual; + mod struct_named; + mod struct_named_manual; + mod struct_named_empty; + mod struct_named_empty_manual; + + mod enum_unit; + mod enum_unit_manual; + mod enum_tuple; + mod enum_tuple_manual; + mod enum_tuple_empty; + mod enum_tuple_empty_manual; + mod enum_named; + mod enum_named_manual; + mod enum_named_empty; + mod enum_named_empty_manual; + + // + + mod generics_lifetimes; + mod generics_lifetimes_manual; + + mod generics_types; + mod generics_types_manual; + mod generics_types_default; + mod generics_types_default_manual; + + mod generics_constants; + mod generics_constants_manual; + mod generics_constants_default; + mod generics_constants_default_manual; + + // + + mod bounds_inlined; + mod bounds_inlined_manual; + mod bounds_where; + mod bounds_where_manual; + mod bounds_mixed; + mod bounds_mixed_manual; + + // + + mod name_collisions; +} -mod deref_mut_manual_test; #[ cfg( feature = "derive_deref_mut" ) ] -mod deref_mut_test; +#[ path = "deref_mut" ] +mod deref_mut_tests +{ + #[ allow( unused_imports ) ] + use super::*; + + // + + mod struct_tuple; + mod struct_tuple_manual; + mod struct_named; + mod struct_named_manual; + + mod enum_tuple; + mod enum_tuple_manual; + mod enum_named; + mod enum_named_manual; + + // + + mod generics_lifetimes; + mod generics_lifetimes_manual; + + mod generics_types; + mod generics_types_manual; + mod generics_types_default; + mod generics_types_default_manual; + + mod generics_constants; + mod generics_constants_manual; + mod generics_constants_default; + mod generics_constants_default_manual; + + // + + mod bounds_inlined; + mod bounds_inlined_manual; + mod bounds_where; + mod bounds_where_manual; + mod bounds_mixed; + mod bounds_mixed_manual; + + // + + mod name_collisions; +} #[ cfg( feature = "derive_from" ) ] #[ path = "from" ] -mod tests +mod from { #[ allow( unused_imports ) ] use super::*; diff --git a/module/core/derive_tools/tests/inc/only_test/deref.rs b/module/core/derive_tools/tests/inc/only_test/deref.rs index 1586fa7430..2c5447dc84 100644 --- a/module/core/derive_tools/tests/inc/only_test/deref.rs +++ b/module/core/derive_tools/tests/inc/only_test/deref.rs @@ -1,12 +1,26 @@ #[ test ] -fn deref_test() +fn simple() { // Deref - let got = IsTransparent( true ); + let got = IsTransparentSimple( true ); let exp = true; a_id!( *got, exp ); } + +#[ test ] +fn complex() +{ + + // Deref + + let got_tmp = "start".to_string(); + let got = IsTransparentComplex::< '_, '_, String, str, 0 >( &got_tmp, core::marker::PhantomData ); + let exp_tmp = "start".to_string(); + let exp = &exp_tmp; + assert_eq!( *got, exp ); + +} diff --git a/module/core/derive_tools/tests/inc/only_test/deref_mut.rs b/module/core/derive_tools/tests/inc/only_test/deref_mut.rs index 357c28a108..ab36833c71 100644 --- a/module/core/derive_tools/tests/inc/only_test/deref_mut.rs +++ b/module/core/derive_tools/tests/inc/only_test/deref_mut.rs @@ -1,19 +1,43 @@ #[ test ] -fn deref_mut_test() +fn simple() { // Deref - let got = IsTransparent( true ); + let got = IsTransparentSimple( true ); let exp = true; a_id!( *got, exp ); // DerefMut - let mut got = IsTransparent( true ); + let mut got = IsTransparentSimple( true ); *got = false; let exp = false; a_id!( *got, exp ); } + +#[ test ] +fn complex() +{ + + // Deref + + let got_tmp = "start".to_string(); + let got = IsTransparentComplex::< '_, '_, String, str, 0 >( &got_tmp, core::marker::PhantomData ); + let exp_tmp = "start".to_string(); + let exp = &exp_tmp; + assert_eq!( *got, exp ); + + // DerefMut + + let got_tmp = "start".to_string(); + let mut got = IsTransparentComplex::< '_, '_, String, str, 0 >( &got_tmp, core::marker::PhantomData ); + let got_tmp = "end".to_string(); + *got = &got_tmp; + let exp_tmp = "end".to_string(); + let exp = &exp_tmp; + assert_eq!( *got, exp ); + +} diff --git a/module/core/derive_tools_meta/src/derive/deref.rs b/module/core/derive_tools_meta/src/derive/deref.rs index 3d0640bf9f..ac2217c1c8 100644 --- a/module/core/derive_tools_meta/src/derive/deref.rs +++ b/module/core/derive_tools_meta/src/derive/deref.rs @@ -1,28 +1,53 @@ - use super::*; -use macro_tools::{ attr, diag, item_struct, Result }; +use macro_tools::{ attr, diag, generic_params, Result, struct_like::StructLike }; + +// pub fn deref( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStream > { let original_input = input.clone(); - let parsed = syn::parse::< syn::ItemStruct >( input )?; - let has_debug = attr::has_debug( parsed.attrs.iter() )?; + let parsed = syn::parse::< StructLike >( input )?; + let has_debug = attr::has_debug( parsed.attrs().iter() )?; + let item_name = &parsed.ident(); - let item_name = &parsed.ident; - let field_type = item_struct::first_field_type( &parsed )?; + let ( _generics_with_defaults, generics_impl, generics_ty, generics_where ) + = generic_params::decompose( &parsed.generics() ); - let result = qt! + let result = match parsed { - impl core::ops::Deref for #item_name + StructLike::Unit( _ ) => { - type Target = #field_type; - #[ inline( always ) ] - fn deref( &self ) -> &Self::Target - { - &self.0 - } + generate_unit + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + ) } - }; + StructLike::Struct( ref item ) => + { + generate_struct + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &item.fields, + ) + } + StructLike::Enum( ref item ) => + { + generate_enum + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &item.variants, + ) + } + }?; if has_debug { @@ -32,3 +57,490 @@ pub fn deref( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStr Ok( result ) } + +/// Generates `Deref` implementation for unit structs and enums +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::Deref; +/// #[ derive( Deref ) ] +/// pub struct Struct; +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct; +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for Struct +/// { +/// type Target = (); +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// &() +/// } +/// } +/// ``` +/// +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 >, +) +-> Result< proc_macro2::TokenStream > +{ + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::Deref for #item_name< #generics_ty > + where + #generics_where + { + type Target = (); + #[ inline( always ) ] + fn deref( &self ) -> &Self::Target + { + &() + } + } + } + ) +} + +/// An aggregator function to generate `Deref` implementation for unit, tuple structs and the ones with named fields +fn generate_struct +( + 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 >, + fields : &syn::Fields, +) +-> Result< proc_macro2::TokenStream > +{ + match fields + { + + syn::Fields::Unit => + generate_unit + ( + item_name, + generics_impl, + generics_ty, + generics_where, + ), + + syn::Fields::Unnamed( fields ) => + generate_struct_tuple_fields + ( + item_name, + generics_impl, + generics_ty, + generics_where, + fields, + ), + + syn::Fields::Named( fields ) => + generate_struct_named_fields + ( + item_name, + generics_impl, + generics_ty, + generics_where, + fields, + ), + + } +} + +/// Generates `Deref` implementation for structs with tuple fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::Deref; +/// #[ derive( Deref ) ] +/// pub struct Struct( i32, Vec< String > ); +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct( i32, Vec< String > ); +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for Struct +/// { +/// type Target = i32; +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// &self.0 +/// } +/// } +/// ``` +/// +fn generate_struct_tuple_fields +( + 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 >, + fields : &syn::FieldsUnnamed, +) +-> Result< proc_macro2::TokenStream > +{ + let fields = &fields.unnamed; + let field_type = match fields.first() + { + Some( field ) => &field.ty, + None => return generate_unit + ( + item_name, + generics_impl, + generics_ty, + generics_where, + ), + }; + + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::Deref for #item_name< #generics_ty > + where + #generics_where + { + type Target = #field_type; + #[ inline( always ) ] + fn deref( &self ) -> &Self::Target + { + &self.0 + } + } + } + ) +} + +/// Generates `Deref` implementation for structs with named fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::Deref; +/// #[ derive( Deref ) ] +/// pub struct Struct +/// { +/// a : i32, +/// b : Vec< String >, +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct +/// { +/// a : i32, +/// b : Vec< String >, +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for Struct +/// { +/// type Target = i32; +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// &self.a +/// } +/// } +/// ``` +/// +fn generate_struct_named_fields +( + 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 >, + fields : &syn::FieldsNamed, +) +-> Result< proc_macro2::TokenStream > +{ + let fields = &fields.named; + let ( field_name, field_type ) = match fields.first() + { + Some( field ) => ( field.ident.as_ref().unwrap(), &field.ty ), + None => return generate_unit + ( + item_name, + generics_impl, + generics_ty, + generics_where, + ), + }; + + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::Deref for #item_name< #generics_ty > + where + #generics_where + { + type Target = #field_type; + #[ inline( always ) ] + fn deref( &self ) -> &Self::Target + { + &self.#field_name + } + } + } + ) +} + +/// An aggregator function to generate `Deref` implementation for unit, tuple enums and the ones with named fields +fn generate_enum +( + 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 >, + variants : &syn::punctuated::Punctuated, +) +-> Result< proc_macro2::TokenStream > +{ + let fields = match variants.first() + { + Some( variant ) => &variant.fields, + None => return generate_unit + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + ), + }; + + // error if fields have different types + if !variants.iter().skip(1).all(|v| &v.fields == fields) + { + return Err( syn::Error::new( variants.span(), "Variants must have the same type" ) ); + } + + let idents = variants.iter().map( | v | v.ident.clone() ).collect::< Vec< _ > >(); + + match fields + { + + syn::Fields::Unit => + generate_unit + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + ), + + syn::Fields::Unnamed( ref item ) => + generate_enum_tuple_variants + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &idents, + item, + ), + + syn::Fields::Named( ref item ) => + generate_enum_named_variants + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &idents, + item, + ), + + } +} + +/// Generates `Deref` implementation for enums with tuple fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::Deref; +/// #[ derive( Deref ) ] +/// pub enum E +/// { +/// A ( i32, Vec< String > ), +/// B ( i32, Vec< String > ), +/// C ( i32, Vec< String > ), +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub enum E +/// { +/// A ( i32, Vec< String > ), +/// B ( i32, Vec< String > ), +/// C ( i32, Vec< String > ), +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for E +/// { +/// type Target = i32; +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// match self +/// { +/// E::A( v, .. ) | E::B( v, .. ) | E::C( v, .. ) => v, +/// } +/// } +/// } +/// ``` +/// +fn generate_enum_tuple_variants +( + 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 >, + variant_idents : &[ syn::Ident ], + fields : &syn::FieldsUnnamed, +) +-> Result< proc_macro2::TokenStream > +{ + let fields = &fields.unnamed; + let field_ty = match fields.first() + { + Some( field ) => &field.ty, + None => return generate_unit + ( + item_name, + generics_impl, + generics_ty, + generics_where, + ), + }; + + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::Deref for #item_name< #generics_ty > + where + #generics_where + { + type Target = #field_ty; + #[ inline( always ) ] + fn deref( &self ) -> &Self::Target + { + match self + { + #( #item_name::#variant_idents( v, .. ) )|* => v + } + } + } + } + ) +} + +/// Generates `Deref` implementation for enums with named fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::Deref; +/// #[ derive( Deref ) ] +/// pub enum E +/// { +/// A { a : i32, b : Vec< String > }, +/// B { a : i32, b : Vec< String > }, +/// C { a : i32, b : Vec< String > }, +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub enum E +/// { +/// A { a : i32, b : Vec< String > }, +/// B { a : i32, b : Vec< String > }, +/// C { a : i32, b : Vec< String > }, +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for E +/// { +/// type Target = i32; +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// match self +/// { +/// E::A { a : v, .. } | E::B { a : v, .. } | E::C { a : v, .. } => v, +/// } +/// } +/// } +/// ``` +/// +fn generate_enum_named_variants +( + 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 >, + variant_idents : &[ syn::Ident ], + fields : &syn::FieldsNamed, +) +-> Result< proc_macro2::TokenStream > +{ + let fields = &fields.named; + let ( field_name, field_ty ) = match fields.first() + { + Some( field ) => ( field.ident.as_ref().unwrap(), &field.ty ), + None => return generate_unit + ( + item_name, + generics_impl, + generics_ty, + generics_where, + ), + }; + + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::Deref for #item_name< #generics_ty > + where + #generics_where + { + type Target = #field_ty; + #[ inline( always ) ] + fn deref( &self ) -> &Self::Target + { + match self + { + #( #item_name::#variant_idents{ #field_name : v, ..} )|* => v + } + } + } + } + ) +} diff --git a/module/core/derive_tools_meta/src/derive/deref_mut.rs b/module/core/derive_tools_meta/src/derive/deref_mut.rs index 20b778e448..46d7a273f6 100644 --- a/module/core/derive_tools_meta/src/derive/deref_mut.rs +++ b/module/core/derive_tools_meta/src/derive/deref_mut.rs @@ -1,33 +1,458 @@ - use super::*; -use macro_tools::{ attr, diag, Result }; +use macro_tools::{ attr, diag, generic_params, Result, struct_like::StructLike }; // pub fn deref_mut( input : proc_macro::TokenStream ) -> Result< proc_macro2::TokenStream > { let original_input = input.clone(); - let parsed = syn::parse::< syn::ItemStruct >( input )?; - let has_debug = attr::has_debug( parsed.attrs.iter() )?; - let item_name = &parsed.ident; + let parsed = syn::parse::< StructLike >( input )?; + let has_debug = attr::has_debug( 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( _ ) => generate_unit(), + + StructLike::Struct( ref item ) => + generate_struct + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &item.fields, + ), + + StructLike::Enum( ref item ) => + generate_enum + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &item.variants, + ), + + }?; + + if has_debug + { + let about = format!( "derive : DerefMut\nstructure : {item_name}" ); + diag::report_print( about, &original_input, &result ); + } + + Ok( result ) +} - let result = qt! +/// Placeholder for unit structs and enums. Does not generate any `DerefMut` implementation +fn generate_unit() -> Result< proc_macro2::TokenStream > +{ + Ok( qt!{} ) +} + +/// An aggregator function to generate `DerefMut` implementation for unit, tuple structs and the ones with named fields +fn generate_struct +( + 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 >, + fields : &syn::Fields, +) +-> Result< proc_macro2::TokenStream > +{ + match fields { - impl core::ops::DerefMut for #item_name + + syn::Fields::Unit => generate_unit(), + + syn::Fields::Unnamed( _ ) => + generate_struct_tuple_fields + ( + item_name, + generics_impl, + generics_ty, + generics_where, + ), + + syn::Fields::Named( fields ) => + generate_struct_named_fields + ( + item_name, + generics_impl, + generics_ty, + generics_where, + fields, + ), + + } +} + +/// Generates `DerefMut` implementation for structs with tuple fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::{ Deref, DerefMut }; +/// #[ derive( Deref, DerefMut ) ] +/// pub struct Struct( i32, Vec< String > ); +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct( i32, Vec< String > ); +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for Struct +/// { +/// type Target = i32; +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// &self.0 +/// } +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::DerefMut for Struct +/// { +/// #[ inline( always ) ] +/// fn deref_mut( &mut self ) -> &mut Self::Target +/// { +/// &mut self.0 +/// } +/// } +/// ``` +/// +fn generate_struct_tuple_fields +( + 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 >, +) +-> Result< proc_macro2::TokenStream > +{ + Ok + ( + qt! { - #[ inline( always ) ] - fn deref_mut( &mut self ) -> &mut Self::Target + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::DerefMut for #item_name< #generics_ty > + where + #generics_where { - &mut self.0 + #[ inline( always ) ] + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.0 + } } } + ) +} + +/// Generates `DerefMut` implementation for structs with named fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::{ Deref, DerefMut }; +/// #[ derive( Deref, DerefMut ) ] +/// pub struct Struct +/// { +/// a : i32, +/// b : Vec< String >, +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct +/// { +/// a : i32, +/// b : Vec< String >, +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for Struct +/// { +/// type Target = i32; +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// &self.a +/// } +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::DerefMut for Struct +/// { +/// #[ inline( always ) ] +/// fn deref_mut( &mut self ) -> &mut Self::Target +/// { +/// &mut self.a +/// } +/// } +/// ``` +/// +fn generate_struct_named_fields +( + 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 >, + fields : &syn::FieldsNamed, +) +-> Result< proc_macro2::TokenStream > +{ + let fields = &fields.named; + let field_name = match fields.first() + { + Some( field ) => field.ident.as_ref().unwrap(), + None => return generate_unit(), }; - if has_debug + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::DerefMut for #item_name< #generics_ty > + where + #generics_where + { + #[ inline( always ) ] + fn deref_mut( &mut self ) -> &mut Self::Target + { + &mut self.#field_name + } + } + } + ) +} + +/// An aggregator function to generate `DerefMut` implementation for unit, tuple enums and the ones with named fields +fn generate_enum +( + 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 >, + variants : &syn::punctuated::Punctuated, +) +-> Result< proc_macro2::TokenStream > +{ + let fields = match variants.first() { - let about = format!( "derive : DerefMut\nstructure : {item_name}" ); - diag::report_print( about, &original_input, &result ); + Some( variant ) => &variant.fields, + None => return generate_unit(), + }; + + let idents = variants.iter().map( | v | v.ident.clone() ).collect::< Vec< _ > >(); + + match fields + { + + syn::Fields::Unit => generate_unit(), + + syn::Fields::Unnamed( _ ) => + generate_enum_tuple_variants + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &idents, + ), + + syn::Fields::Named( ref item ) => + generate_enum_named_variants + ( + item_name, + &generics_impl, + &generics_ty, + &generics_where, + &idents, + item, + ), + } +} - Ok( result ) +/// Generates `DerefMut` implementation for enums with tuple fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::{ Deref, DerefMut }; +/// #[ derive( Deref, DerefMut ) ] +/// pub enum E +/// { +/// A ( i32, Vec< String > ), +/// B ( i32, Vec< String > ), +/// C ( i32, Vec< String > ), +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub enum E +/// { +/// A ( i32, Vec< String > ), +/// B ( i32, Vec< String > ), +/// C ( i32, Vec< String > ), +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for E +/// { +/// type Target = i32; +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// match self +/// { +/// E::A( v, .. ) | E::B( v, .. ) | E::C( v, .. ) => v, +/// } +/// } +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::DerefMut for E +/// { +/// #[ inline( always ) ] +/// fn deref_mut( &mut self ) -> &mut Self::Target +/// { +/// match self +/// { +/// E::A( v, .. ) | E::B( v, .. ) | E::C( v, .. ) => v, +/// } +/// } +/// } +/// ``` +/// +fn generate_enum_tuple_variants +( + 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 >, + variant_idents : &[ syn::Ident ], +) +-> Result< proc_macro2::TokenStream > +{ + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::DerefMut for #item_name< #generics_ty > + where + #generics_where + { + #[ inline( always ) ] + fn deref_mut( &mut self ) -> &mut Self::Target + { + match self + { + #( #item_name::#variant_idents( v, .. ) )|* => v + } + } + } + } + ) +} + +/// Generates `DerefMut` implementation for enums with named fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::{ Deref, DerefMut }; +/// #[ derive( Deref, DerefMut ) ] +/// pub enum E +/// { +/// A { a : i32, b : Vec< String > }, +/// B { a : i32, b : Vec< String > }, +/// C { a : i32, b : Vec< String > }, +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub enum E +/// { +/// A { a : i32, b : Vec< String > }, +/// B { a : i32, b : Vec< String > }, +/// C { a : i32, b : Vec< String > }, +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::Deref for E +/// { +/// type Target = i32; +/// #[ inline( always ) ] +/// fn deref( &self ) -> &Self::Target +/// { +/// match self +/// { +/// E::A { a : v, .. } | E::B { a : v, .. } | E::C { a : v, .. } => v, +/// } +/// } +/// } +/// #[ automatically_derived ] +/// impl ::core::ops::DerefMut for E +/// { +/// #[ inline( always ) ] +/// fn deref_mut( &mut self ) -> &mut Self::Target +/// { +/// match self +/// { +/// E::A { a : v, .. } | E::B { a : v, .. } | E::C { a : v, .. } => v, +/// } +/// } +/// } +/// ``` +/// +fn generate_enum_named_variants +( + 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 >, + variant_idents : &[ syn::Ident ], + fields : &syn::FieldsNamed, +) +-> Result< proc_macro2::TokenStream > +{ + let fields = &fields.named; + let field_name = match fields.first() + { + Some( field ) => field.ident.as_ref().unwrap(), + None => return generate_unit(), + }; + + Ok + ( + qt! + { + #[ automatically_derived ] + impl< #generics_impl > ::core::ops::DerefMut for #item_name< #generics_ty > + where + #generics_where + { + #[ inline( always ) ] + fn deref_mut( &mut self ) -> &mut Self::Target + { + match self + { + #( #item_name::#variant_idents{ #field_name : v, ..} )|* => v + } + } + } + } + ) } diff --git a/module/core/derive_tools_meta/src/derive/from.rs b/module/core/derive_tools_meta/src/derive/from.rs index 88783c21e6..65334d453d 100644 --- a/module/core/derive_tools_meta/src/derive/from.rs +++ b/module/core/derive_tools_meta/src/derive/from.rs @@ -141,7 +141,31 @@ 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 -- done +/// Generates `From` implementation for unit structs +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::From; +/// #[ derive( From ) ] +/// pub struct IsTransparent; +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct IsTransparent; +/// impl From< () > for IsTransparent +/// { +/// #[ inline( always ) ] +/// fn from( src : () ) -> Self +/// { +/// Self +/// } +/// } +/// ``` +/// fn generate_unit ( item_name : &syn::Ident, @@ -167,7 +191,38 @@ fn generate_unit } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code -- done +/// Generates `From` implementation for tuple structs with a single field +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::From; +/// #[ derive( From ) ] +/// pub struct IsTransparent +/// { +/// value : bool, +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct IsTransparent +/// { +/// value : bool, +/// } +/// #[ automatically_derived ] +/// impl From< bool > for IsTransparent +/// { +/// #[ inline( always ) ] +/// fn from( src : bool ) -> Self +/// { +/// Self { value : src } +/// } +/// } +/// ``` +/// fn generate_single_field_named ( item_name : &syn::Ident, @@ -182,7 +237,6 @@ fn generate_single_field_named qt! { #[ automatically_derived ] - // impl From < i32 > for MyStruct impl< #generics_impl > From< #field_type > for #item_name< #generics_ty > where #generics_where @@ -191,14 +245,38 @@ fn generate_single_field_named // fn from( src : i32 ) -> Self fn from( src : #field_type ) -> Self { - // Self { a: src } - Self { #field_name: src } + Self { #field_name : src } } } } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code -- done +/// Generates `From`` implementation for structs with a single named field +/// +/// # Example of generated code +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::From; +/// #[ derive( From ) ] +/// pub struct IsTransparent( bool ); +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct IsTransparent( bool ); +/// #[ automatically_derived ] +/// impl From< bool > for IsTransparent +/// { +/// #[ inline( always ) ] +/// fn from( src : bool ) -> Self +/// { +/// Self( src ) +/// } +/// } +/// ``` +/// fn generate_single_field ( item_name : &syn::Ident, @@ -213,7 +291,6 @@ fn generate_single_field qt! { #[automatically_derived] - // impl From< bool > for IsTransparent impl< #generics_impl > From< #field_type > for #item_name< #generics_ty > where #generics_where @@ -229,7 +306,42 @@ fn generate_single_field } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code -- done +/// Generates `From` implementation for structs with multiple named fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::From; +/// #[ derive( From ) ] +/// pub struct Struct +/// { +/// value1 : bool, +/// value2 : i32, +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct +/// { +/// value1 : bool, +/// value2 : i32, +/// } +/// impl From< ( bool, i32 ) > for Struct +/// { +/// #[ inline( always ) ] +/// fn from( src : ( bool, i32 ) ) -> Self +/// { +/// Struct +/// { +/// value1 : src.0, +/// value2 : src.1, +/// } +/// } +/// } +/// ``` fn generate_multiple_fields_named< 'a > ( item_name : &syn::Ident, @@ -254,7 +366,6 @@ fn generate_multiple_fields_named< 'a > let field_types : Vec< _ > = field_types.collect(); qt! { - // impl From< (i32, bool) > for StructNamedFields impl< #generics_impl > From< (# ( #field_types ),* ) > for #item_name< #generics_ty > where #generics_where @@ -263,7 +374,6 @@ fn generate_multiple_fields_named< 'a > // fn from( src : (i32, bool) ) -> Self fn from( src : ( #( #field_types ),* ) ) -> Self { - // StructNamedFields{ a: src.0, b: src.1 } #item_name { #(#params),* } } } @@ -271,7 +381,31 @@ fn generate_multiple_fields_named< 'a > } -// qqq : document, add example of generated code +// qqq : document, add example of generated code -- done +/// Generates `From` implementation for tuple structs with multiple fields +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::From; +/// #[ derive( From ) ] +/// pub struct Struct( bool, i32 ); +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct( bool, i32 ); +/// impl From< ( bool, i32 ) > for Struct +/// { +/// #[ inline( always ) ] +/// fn from( src : ( bool, i32 ) ) -> Self +/// { +/// Struct( src.0, src.1 ) +/// } +/// } +/// ``` +/// fn generate_multiple_fields< 'a > ( item_name : &syn::Ident, @@ -295,7 +429,6 @@ fn generate_multiple_fields< 'a > qt! { - // impl From< (i32, bool) > for StructWithManyFields impl< #generics_impl > From< (# ( #field_types ),* ) > for #item_name< #generics_ty > where #generics_where @@ -304,7 +437,6 @@ fn generate_multiple_fields< 'a > // fn from( src : (i32, bool) ) -> Self fn from( src : ( #( #field_types ),* ) ) -> Self { - // StructWithManyFields( src.0, src.1 ) #item_name( #( #params ),* ) } } @@ -409,4 +541,4 @@ field : {variant_name}"#, } ) -} +} \ No newline at end of file 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 2226df43d8..427f447087 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,40 @@ 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 +/// Generates `From` implementation for the inner type regarding bounded type +/// Works with structs with a single named field +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::InnerFrom; +/// #[ derive( InnerFrom ) ] +/// pub struct Struct +/// { +/// value : bool, +/// } +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct +/// { +/// value : bool, +/// } +/// #[ allow( non_local_definitions ) ] +/// #[ automatically_derived ] +/// impl From< Struct > for bool +/// { +/// #[ inline( always ) ] +/// fn from( src : Struct ) -> Self +/// { +/// src.value +/// } +/// } +/// ``` +/// fn from_impl_named ( item_name : &syn::Ident, @@ -69,21 +102,45 @@ fn from_impl_named { #[ allow( non_local_definitions ) ] #[ automatically_derived ] - // impl From< MyStruct > for i32 impl From< #item_name > for #field_type { #[ inline( always ) ] // fm from( src : MyStruct ) -> Self fn from( src : #item_name ) -> Self { - // src.a src.#field_name } } } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code -- done +/// Generates `From` implementation for the only contained type regarding the bounded type +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::InnerFrom; +/// #[ derive( InnerFrom ) ] +/// pub struct Struct( bool ); +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct( bool ); +/// #[ allow( non_local_definitions ) ] +/// #[ automatically_derived ] +/// impl From< Struct > for bool +/// { +/// #[ inline( always ) ] +/// fn from( src : Struct ) -> Self +/// { +/// src.0 +/// } +/// } +/// ``` +/// fn from_impl ( item_name : &syn::Ident, @@ -94,7 +151,6 @@ fn from_impl { #[ allow( non_local_definitions ) ] #[ automatically_derived ] - // impl From< IsTransparent> for bool impl From< #item_name > for #field_type { #[ inline( always ) ] @@ -107,7 +163,34 @@ fn from_impl } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code -- done +/// Generates `From` implementation for the tuple type containing all the inner types regarding the bounded type +/// Can generate implementations both for structs with named fields and tuple structs. +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::InnerFrom; +/// #[ derive( InnerFrom ) ] +/// pub struct Struct( bool, i32 ); +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct( bool, i32 ); +/// #[ allow( non_local_definitions ) ] +/// #[ automatically_derived ] +/// impl From< Struct > for ( bool, i32 ) +/// { +/// #[ inline( always ) ] +/// fn from( src : Struct ) -> Self +/// { +/// ( src.0, src.1 ) +/// } +/// } +/// ``` +/// fn from_impl_multiple_fields< 'a > ( item_name : &syn::Ident, @@ -119,28 +202,53 @@ fn from_impl_multiple_fields< 'a > { #[ allow( non_local_definitions ) ] #[ automatically_derived ] - // impl From< StructWithManyFields > for ( i32, bool ) - impl From< #item_name > for ( #(#field_types), *) + impl From< #item_name > for ( #( #field_types ), *) { #[ inline( always ) ] // fn from( src : StructWithManyFields ) -> Self fn from( src : #item_name ) -> Self { - //( src.0, src.1 ) - (#(#params), *) + ( #( #params ), * ) } } } } -// qqq : document, add example of generated code +// qqq : document, add example of generated code -- done +/// Generates `From` implementation for the unit type regarding the bound type +/// +/// # Example +/// +/// ## Input +/// ```rust +/// # use derive_tools_meta::InnerFrom; +/// #[ derive( InnerFrom ) ] +/// pub struct Struct; +/// ``` +/// +/// ## Output +/// ```rust +/// pub struct Struct; +/// #[ allow( non_local_definitions ) ] +/// #[ allow( clippy::unused_imports ) ] +/// #[ automatically_derived] +/// impl From< Struct > for () +/// { +/// #[ inline( always ) ] +/// fn from( src : Struct ) -> () +/// { +/// () +/// } +/// } +/// ``` +/// fn unit( item_name : &syn::Ident ) -> proc_macro2::TokenStream { qt! { #[ allow( non_local_definitions ) ] + #[ allow( clippy::unused_imports ) ] #[ automatically_derived ] - // impl From< UnitStruct > for () impl From< #item_name > for () { #[ inline( always ) ] diff --git a/module/core/derive_tools_meta/src/lib.rs b/module/core/derive_tools_meta/src/lib.rs index 1a034690bc..b2ea174a1f 100644 --- a/module/core/derive_tools_meta/src/lib.rs +++ b/module/core/derive_tools_meta/src/lib.rs @@ -281,7 +281,7 @@ pub fn deref( input : proc_macro::TokenStream ) -> proc_macro::TokenStream /// /// Write this /// -/// ```rust ignore +/// ```rust /// # use derive_tools_meta::*; /// #[ derive( Deref, DerefMut ) ] /// pub struct IsTransparent( bool ); diff --git a/module/core/macro_tools/src/generic_params.rs b/module/core/macro_tools/src/generic_params.rs index 5af6bdc366..09f4445e5b 100644 --- a/module/core/macro_tools/src/generic_params.rs +++ b/module/core/macro_tools/src/generic_params.rs @@ -462,10 +462,18 @@ pub( crate ) mod private }, syn::GenericParam::Lifetime( lifetime_param ) => { - // Lifetimes are added as-is to both generics_for_impl and generics_for_ty + // Lifetimes are added as-is to generics_for_impl and without bounds to generics_for_ty generics_for_impl.push_value( syn::GenericParam::Lifetime( lifetime_param.clone() ) ); generics_for_impl.push_punct( syn::token::Comma::default() ); - generics_for_ty.push_value( syn::GenericParam::Lifetime( lifetime_param.clone() ) ); + + let ty_param = syn::GenericParam::Lifetime( syn::LifetimeParam + { + attrs : vec![], + lifetime : lifetime_param.lifetime.clone(), + colon_token : None, + bounds : syn::punctuated::Punctuated::new(), + }); + generics_for_ty.push_value( ty_param ); generics_for_ty.push_punct( syn::token::Comma::default() ); } }