Skip to content

Commit

Permalink
READY: IndexMut macros (#1409)
Browse files Browse the repository at this point in the history
derive_tools: IndexMut macros
  • Loading branch information
Inkvisto authored Jul 15, 2024
1 parent 117b34c commit 5a55299
Show file tree
Hide file tree
Showing 30 changed files with 824 additions and 4 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 @@ -152,7 +152,7 @@ derive_error = [ "derive_more", "derive_more/std", "derive_more/error" ]
# derive_reflect = [ "derive_tools_meta/derive_reflect" ]

# derive_index = [ "derive_more", "derive_more/std", "derive_more/index" ]
derive_index_mut = [ "derive_more", "derive_more/std", "derive_more/index_mut" ]
# 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" ]
# derive_iterator = [ "derive_more", "derive_more/iterator" ]
Expand Down Expand Up @@ -181,6 +181,7 @@ derive_clone_dyn = [ "clone_dyn/enabled" ]

derive_from = [ "derive_tools_meta/derive_from" ]
derive_index = [ "derive_tools_meta/derive_index" ]
derive_index_mut = [ "derive_tools_meta/derive_index_mut" ]
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 @@ -22,6 +22,7 @@ fn main()
feature = "derive_deref_mut",
feature = "derive_from",
feature = "derive_index",
feature = "derive_index_mut",
feature = "derive_inner_from",
feature = "derive_variadic_from",
feature = "derive_reflect",
Expand All @@ -38,6 +39,7 @@ fn main()
feature = "derive_deref_mut",
feature = "derive_from",
feature = "derive_index",
feature = "derive_index_mut",
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 @@ -43,8 +43,6 @@ mod derive_more
pub use ::derive_more::Constructor;
#[ cfg( feature = "derive_error" ) ]
pub use ::derive_more::Error;
#[ cfg( feature = "derive_index_mut" ) ]
pub use ::derive_more::IndexMut;
#[ 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_mut/compiletime/enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use derive_tools::IndexMut;

#[ derive( IndexMut ) ]
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_mut/compiletime/enum.rs:3:12
|
3 | #[ derive( IndexMut ) ]
| ^^^^^^^^
|
= help: message: not implemented: IndexMut not implemented for Enum
14 changes: 14 additions & 0 deletions module/core/derive_tools/tests/inc/index_mut/compiletime/struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use derive_tools::IndexMut;

#[ derive( IndexMut ) ]
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_mut/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::IndexMut;

#[ derive( IndexMut ) ]
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_mut/compiletime/struct_named_empty.rs:3:12
|
3 | #[ derive( IndexMut ) ]
| ^^^^^^^^
|
= help: message: not implemented: IndexMut not implemented for Unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use derive_tools::IndexMut;

#[ derive( IndexMut ) ]
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_mut/compiletime/struct_unit.rs:3:12
|
3 | #[ derive( IndexMut ) ]
| ^^^^^^^^
|
= help: message: not implemented: IndexMut not implemented for Unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[ test ]
fn index_mut()
{
let mut x = StructMultipleNamed
{
a : vec![ 4, 17 ],
b : vec![ 33, 55 ]
};

x[ 0 ] = 5;
x[ 1 ] = 18;
let v = vec![ 5, 18 ];

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,14 @@
#[ test ]
fn index_mut()
{
let mut x = StructMultipleTuple( false, vec![ 2, 44, 81 ] );

x[ 0 ] = 18;
x[ 1 ] = 99;

let exp = ( 18, 99 );
let got = ( x[ 0 ], x[ 1 ] );

assert_eq!( got, exp );
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[ test ]
fn index_mut()
{
let mut x = StructNamed
{
a : vec![ 4, 17 ]
};

x[ 0 ] = 5;
x[ 1 ] = 18;
let v = vec![ 5, 18 ];

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,14 @@
#[ test ]
fn index_mut()
{
let mut x = StructTuple( vec![ 2, 44, 81 ] );

x[ 0 ] = 18;
x[ 1 ] = 99;

let exp = ( 18, 99 );
let got = ( x[ 0 ], x[ 1 ] );

assert_eq!( got, exp );
}

22 changes: 22 additions & 0 deletions module/core/derive_tools/tests/inc/index_mut/struct_collisions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#![ allow( non_snake_case ) ]
#![ allow( unused_imports ) ]
use super::*;

pub mod core {}
pub mod std {}
pub mod marker {}

pub mod a {}
pub mod b {}

#[ derive( the_module::IndexMut ) ]
#[ allow( dead_code ) ]
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,14 @@
#![ allow( dead_code ) ]
#[ allow( unused_imports ) ]
use super::*;

#[ derive( the_module::IndexMut ) ]
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,15 @@
#![ allow( dead_code ) ]
#[ allow( unused_imports ) ]
use super::*;

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

include!( "./only_test/struct_multiple_named.rs" );


Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use core::ops::{ Index, IndexMut };

#[ 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 ]
}
}

impl< T > IndexMut< usize > for StructMultipleNamed< T >
{
fn index_mut( &mut self, index : usize ) -> &mut Self::Output
{
&mut self.b[ index ]
}
}


include!( "./only_test/struct_multiple_named.rs" );

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![ allow( dead_code ) ]
#[ allow( unused_imports ) ]
use super::*;


#[ derive( the_module::IndexMut ) ]
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,27 @@
use core::ops::{ Index, IndexMut };

#[ 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 ]
}
}

impl< T > IndexMut< usize > for StructMultipleTuple< T >
{
fn index_mut( &mut self, index : usize ) -> &mut Self::Output
{
&mut self.1[ index ]
}
}


include!( "./only_test/struct_multiple_tuple.rs" );


12 changes: 12 additions & 0 deletions module/core/derive_tools/tests/inc/index_mut/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::IndexMut ) ]
struct StructNamed< T >
{
#[ index ]
a : Vec< T >,
}

include!( "./only_test/struct_named.rs" );
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::ops::{ Index, IndexMut };

#[ 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 ]
}
}

impl< T > IndexMut< usize > for StructNamed< T >
{
fn index_mut( &mut self, index : usize ) -> &mut Self::Output
{
&mut self.a[ index ]
}
}


include!( "./only_test/struct_named.rs" );
12 changes: 12 additions & 0 deletions module/core/derive_tools/tests/inc/index_mut/struct_tuple.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::IndexMut ) ]
struct StructTuple< T >
(
#[ index ]
Vec< T >
);

include!( "./only_test/struct_tuple.rs" );
Loading

0 comments on commit 5a55299

Please sign in to comment.