Skip to content

Commit

Permalink
NOT READY: Index macros (#1398)
Browse files Browse the repository at this point in the history
* fut: basic struct tests

* fut: named struct draft impl

* fut: wnamed struct Generics realization

* fix: formatting fix

* fut: enum and struct macros implementation

* refactor: #[ index ] attribute implementation and general macros, tests refactor

* ref: basic implementation

* ref: comments fix

* fut: struct implementation

* fut: add try_build tests, ref: delete not used attrs

* ref: formatting

* fix: formatting and added attributes
  • Loading branch information
Inkvisto authored Jul 8, 2024
1 parent aa944d8 commit e667a7e
Show file tree
Hide file tree
Showing 30 changed files with 736 additions and 3 deletions.
3 changes: 2 additions & 1 deletion module/core/derive_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ derive_error = [ "derive_more", "derive_more/std", "derive_more/error" ]
# derive_from = [ "derive_tools_meta/derive_from" ]
# derive_reflect = [ "derive_tools_meta/derive_reflect" ]

derive_index = [ "derive_more", "derive_more/std", "derive_more/index" ]
# derive_index = [ "derive_more", "derive_more/std", "derive_more/index" ]
derive_index_mut = [ "derive_more", "derive_more/std", "derive_more/index_mut" ]
# derive_inner_from = [ "derive_more", "derive_more/into" ]
derive_into_iterator = [ "derive_more", "derive_more/std", "derive_more/into_iterator" ]
Expand Down Expand Up @@ -180,6 +180,7 @@ derive_clone_dyn = [ "clone_dyn/enabled" ]
# derive_clone_dyn_use_alloc = [ "derive_clone_dyn", "clone_dyn/use_alloc" ]

derive_from = [ "derive_tools_meta/derive_from" ]
derive_index = [ "derive_tools_meta/derive_index" ]
derive_inner_from = [ "derive_tools_meta/derive_inner_from" ]
derive_new = [ "derive_tools_meta/derive_new" ]

Expand Down
2 changes: 2 additions & 0 deletions module/core/derive_tools/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn main()
feature = "derive_deref",
feature = "derive_deref_mut",
feature = "derive_from",
feature = "derive_index",
feature = "derive_inner_from",
feature = "derive_variadic_from",
feature = "derive_reflect",
Expand All @@ -36,6 +37,7 @@ fn main()
feature = "derive_deref",
feature = "derive_deref_mut",
feature = "derive_from",
feature = "derive_index",
feature = "derive_inner_from",
feature = "derive_variadic_from",
feature = "derive_reflect",
Expand Down
2 changes: 0 additions & 2 deletions module/core/derive_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ mod derive_more
pub use ::derive_more::Error;
#[ cfg( feature = "derive_index_mut" ) ]
pub use ::derive_more::IndexMut;
#[ cfg( feature = "derive_index" ) ]
pub use ::derive_more::Index;
#[ cfg( feature = "derive_into" ) ]
pub use ::derive_more::Into;
// #[ cfg( feature = "derive_iterator" ) ]
Expand Down
13 changes: 13 additions & 0 deletions module/core/derive_tools/tests/inc/index/compiletime/enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use derive_tools::Index;

#[ derive( Index ) ]
enum Enum< T >
{
Nothing,
#[ index ]
IndexVector( Vec< T > )
}

fn main()
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> tests/inc/index/compiletime/enum.rs:3:12
|
3 | #[ derive( Index ) ]
| ^^^^^
|
= help: message: not implemented: Index not implemented for Enum
14 changes: 14 additions & 0 deletions module/core/derive_tools/tests/inc/index/compiletime/struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use derive_tools::Index;

#[ derive( Index ) ]
struct StructMultipleNamed< T >
{
#[ index ]
a: Vec< T >,
#[ index ]
b : Vec< T >,
}

fn main()
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: Only one field can include #[index] derive macro
--> tests/inc/index/compiletime/struct.rs:6:3
|
6 | / #[ index ]
7 | | a: Vec< T >,
8 | | #[ index ]
9 | | b : Vec< T >,
| |_______________^
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use derive_tools::Index;

#[ derive( Index ) ]
struct EmptyStruct
{
}

fn main()
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> tests/inc/index/compiletime/struct_named_empty.rs:3:12
|
3 | #[ derive( Index ) ]
| ^^^^^
|
= help: message: not implemented: Index not implemented for Unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use derive_tools::Index;

#[ derive( Index ) ]
struct StructUnit;

fn main()
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> tests/inc/index/compiletime/struct_unit.rs:3:12
|
3 | #[ derive( Index ) ]
| ^^^^^
|
= help: message: not implemented: Index not implemented for Unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[ test ]
fn index()
{
let x = StructMultipleNamed
{
a: vec![ 12, 22 ],
b: vec![ 33, 55 ]
};
let v = vec![ 33, 55 ];
let exp = ( v[ 0 ], v[ 1 ] );
let got = ( x[ 0 ], x[ 1 ] );

assert_eq!( got, exp );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[ test ]
fn index()
{
let x = StructMultipleTuple( false, vec![ 2, 44, 81 ] );
let exp = ( 2, 44 );
let got = ( x[ 0 ], x[ 1 ] );
assert_eq!( got, exp );
}
13 changes: 13 additions & 0 deletions module/core/derive_tools/tests/inc/index/only_test/struct_named.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[ test ]
fn index()
{
let x = StructNamed
{
a: vec![ false, true ]
};
let v = vec![ false, true ];
let exp = ( v[ 0 ], v[ 1 ] );
let got = ( x[ 0 ], x[ 1 ] );

assert_eq!( got, exp );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[ test ]
fn index()
{
let x = StructTuple( vec![ 2, 44, 81 ] );
let exp = ( 2, 44 );
let got = ( x[ 0 ], x[ 1 ] );

assert_eq!( got, exp );
}
13 changes: 13 additions & 0 deletions module/core/derive_tools/tests/inc/index/struct_multiple_named.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![ allow( dead_code ) ]
#[ allow( unused_imports ) ]
use super::*;

#[ derive( the_module::Index ) ]
struct StructMultipleNamed< T >
{
a : Vec< T >,
#[ index ]
b : Vec< T >,
}

include!( "./only_test/struct_multiple_named.rs" );
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use core::ops::Index;

#[ allow( dead_code ) ]
struct StructMultipleNamed< T >
{
a: Vec< T >,
b: Vec< T >,
}

impl< T > Index< usize > for StructMultipleNamed< T >
{
type Output = T;

fn index( &self, index : usize ) -> &Self::Output
{
&self.b[ index ]
}
}

include!( "./only_test/struct_multiple_named.rs" );
13 changes: 13 additions & 0 deletions module/core/derive_tools/tests/inc/index/struct_multiple_tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![ allow( dead_code ) ]
#[ allow( unused_imports ) ]
use super::*;

#[ derive( the_module::Index ) ]
struct StructMultipleTuple< T >
(
bool,
#[ index ]
Vec< T >,
);

include!( "./only_test/struct_multiple_tuple.rs" );
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use core::ops::Index;

#[ allow( dead_code) ]
struct StructMultipleTuple< T >( bool, Vec< T > );

impl< T > Index< usize > for StructMultipleTuple< T >
{
type Output = T;

fn index( &self, index : usize ) -> &Self::Output
{
&self.1[ index ]
}
}

include!( "./only_test/struct_multiple_tuple.rs" );
12 changes: 12 additions & 0 deletions module/core/derive_tools/tests/inc/index/struct_named.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![ allow( dead_code ) ]
#[ allow( unused_imports ) ]
use super::*;

#[ derive( the_module::Index ) ]
struct StructNamed< T >
{
#[ index ]
a : Vec< T >,
}

include!( "./only_test/struct_named.rs" );
19 changes: 19 additions & 0 deletions module/core/derive_tools/tests/inc/index/struct_named_manual.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use core::ops::Index;

#[ allow( dead_code ) ]
struct StructNamed< T >
{
a: Vec< T >
}

impl< T > Index< usize > for StructNamed< T >
{
type Output = T;

fn index( &self, index : usize ) -> &Self::Output
{
&self.a[ index ]
}
}

include!( "./only_test/struct_named.rs" );
11 changes: 11 additions & 0 deletions module/core/derive_tools/tests/inc/index/struct_tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::*;

#[ allow( dead_code ) ]
#[ derive( the_module::Index ) ]
struct StructTuple< T >
(
#[ index ]
Vec< T >
);

include!( "./only_test/struct_tuple.rs" );
16 changes: 16 additions & 0 deletions module/core/derive_tools/tests/inc/index/struct_tuple_manual.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use core::ops::Index;

#[ allow( dead_code) ]
struct StructTuple< T >( Vec< T > );

impl< T > Index< usize > for StructTuple< T >
{
type Output = T;

fn index( &self, index : usize ) -> &Self::Output
{
&self.0[ index ]
}
}

include!( "./only_test/struct_tuple.rs" );
36 changes: 36 additions & 0 deletions module/core/derive_tools/tests/inc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod all_manual_test;
feature = "derive_deref",
feature = "derive_deref_mut",
feature = "derive_from",
feature = "derive_index",
feature = "derive_inner_from",
feature = "derive_phantom"
)
Expand Down Expand Up @@ -294,3 +295,38 @@ mod phantom_tests
}
}
}


#[ cfg( feature = "derive_index" ) ]
#[ path = "index" ]
mod index_tests
{
#[ allow( unused_imports ) ]
use super::*;

mod struct_named;
mod struct_multiple_named;
mod struct_named_manual;
mod struct_multiple_named_manual;
mod struct_tuple;
mod struct_multiple_tuple;
mod struct_tuple_manual;
mod struct_multiple_tuple_manual;

only_for_terminal_module!
{
#[ test_tools::nightly ]
#[ test ]
fn index_trybuild()
{

println!( "current_dir : {:?}", std::env::current_dir().unwrap() );
let t = test_tools::compiletime::TestCases::new();

t.compile_fail( "tests/inc/index/compiletime/struct.rs" );
t.compile_fail( "tests/inc/index/compiletime/struct_unit.rs" );
t.compile_fail( "tests/inc/index/compiletime/struct_named_empty.rs" );
t.compile_fail( "tests/inc/index/compiletime/enum.rs" );
}
}
}
3 changes: 3 additions & 0 deletions module/core/derive_tools_meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ default = [
"derive_deref",
"derive_from",
"derive_new",
"derive_index",
"derive_inner_from",
"derive_as_ref",
"derive_as_mut",
Expand All @@ -46,6 +47,7 @@ full = [
"derive_deref",
"derive_from",
"derive_new",
"derive_index",
"derive_inner_from",
"derive_as_ref",
"derive_as_mut",
Expand All @@ -60,6 +62,7 @@ derive_deref = []
derive_deref_mut = []
derive_from = []
derive_new = []
derive_index = []
derive_inner_from = []
derive_variadic_from = [ "iter_tools/iter_ext" ]
derive_phantom = []
Expand Down
2 changes: 2 additions & 0 deletions module/core/derive_tools_meta/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub mod deref;
pub mod deref_mut;
#[ cfg( feature = "derive_from" ) ]
pub mod from;
#[ cfg( feature = "derive_index" ) ]
pub mod index;
#[ cfg( feature = "derive_inner_from" ) ]
pub mod inner_from;
#[ cfg( feature = "derive_new" ) ]
Expand Down
Loading

0 comments on commit e667a7e

Please sign in to comment.