Skip to content

Commit

Permalink
READY: Extend phantom macros (#1381)
Browse files Browse the repository at this point in the history
get completed phantom derive
  • Loading branch information
BigglesworthCat authored Jun 22, 2024
1 parent fded466 commit d5c2499
Show file tree
Hide file tree
Showing 42 changed files with 467 additions and 17 deletions.
4 changes: 2 additions & 2 deletions module/core/derive_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ default = [
"derive_inner_from",
"derive_new",

"derive_phantom"
"derive_phantom",

# "use_std",
]
Expand Down Expand Up @@ -119,7 +119,7 @@ full = [
"derive_inner_from",
"derive_new",

"derive_phantom"
"derive_phantom",

# "use_std",
]
Expand Down
6 changes: 4 additions & 2 deletions module/core/derive_tools/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ fn main()
feature = "derive_from",
feature = "derive_inner_from",
feature = "derive_variadic_from",
feature = "derive_reflect"
feature = "derive_reflect",
feature = "derive_phantom"
)
},
any_derive :
Expand All @@ -37,7 +38,8 @@ fn main()
feature = "derive_from",
feature = "derive_inner_from",
feature = "derive_variadic_from",
feature = "derive_reflect"
feature = "derive_reflect",
feature = "derive_phantom"
)
},
}
Expand Down
32 changes: 31 additions & 1 deletion module/core/derive_tools/tests/inc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod all_manual_test;
feature = "derive_deref_mut",
feature = "derive_from",
feature = "derive_inner_from",
feature = "derive_phantom"
feature = "derive_phantom",
)
)]
mod all_test;
Expand Down Expand Up @@ -257,6 +257,36 @@ mod phantom_tests

mod struct_named;
mod struct_named_manual;
mod struct_named_empty;
mod struct_named_empty_manual;
mod struct_tuple;
mod struct_tuple_manual;
mod struct_tuple_empty;
mod struct_tuple_empty_manual;
mod struct_unit_to_tuple;
mod struct_unit_to_tuple_manual;
mod bounds_inlined;
mod bounds_inlined_manual;
mod bounds_mixed;
mod bounds_mixed_manual;
mod bounds_where;
mod bounds_where_manual;
mod name_collisions;
mod covariant_type;
mod covariant_type_manual;
mod contravariant_type;
mod contravariant_type_manual;
mod send_sync_type;
mod send_sync_type_manual;
#[ test_tools::nightly ]
#[ test ]
fn phantom_trybuild()
{

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

t.compile_fail( "tests/inc/phantom/compiletime/enum.rs" );
t.compile_fail( "tests/inc/phantom/compiletime/invariant_type.rs" );
}
}
8 changes: 8 additions & 0 deletions module/core/derive_tools/tests/inc/phantom/bounds_inlined.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::fmt::Debug;
use super::*;

#[ allow( dead_code ) ]
#[ the_module::phantom ]
struct BoundsInlined< T: ToString, U: Debug > {}

include!( "./only_test/bounds_inlined.rs" );
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::
{
fmt::Debug,
marker::PhantomData,
};

#[ allow( dead_code ) ]
struct BoundsInlined< T: ToString, U: Debug >
{
_phantom: PhantomData< ( T, U ) >,
}

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

#[ allow( dead_code ) ]
#[ the_module::phantom ]
struct BoundsMixed< T: ToString, U >
where
U: Debug,
{}

include!( "./only_test/bounds_mixed.rs" );
15 changes: 15 additions & 0 deletions module/core/derive_tools/tests/inc/phantom/bounds_mixed_manual.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::
{
fmt::Debug,
marker::PhantomData,
};

#[ allow( dead_code ) ]
struct BoundsMixed< T: ToString, U >
where
U: Debug,
{
_phantom: PhantomData< ( T, U ) >,
}

include!( "./only_test/bounds_mixed.rs" );
12 changes: 12 additions & 0 deletions module/core/derive_tools/tests/inc/phantom/bounds_where.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::fmt::Debug;
use super::*;

#[ allow( dead_code ) ]
#[ the_module::phantom ]
struct BoundsWhere< T, U >
where
T: ToString,
U: Debug,
{}

include!( "./only_test/bounds_where.rs" );
16 changes: 16 additions & 0 deletions module/core/derive_tools/tests/inc/phantom/bounds_where_manual.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::
{
fmt::Debug,
marker::PhantomData,
};

#[ allow( dead_code ) ]
struct BoundsWhere< T, U >
where
T: ToString,
U: Debug,
{
_phantom: PhantomData< ( T, U ) >
}

include!( "./only_test/bounds_where.rs" );
13 changes: 13 additions & 0 deletions module/core/derive_tools/tests/inc/phantom/compiletime/enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use derive_tools_meta::phantom;

#[ phantom ]
enum Enum< T >
{
A,
B,
C( T ),
}

fn main()
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: expected `struct`
--> tests/inc/phantom/compiletime/enum.rs:4:1
|
4 | enum Enum< T >
| ^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use derive_tools_meta::phantom;

#[ phantom ]
struct InvariantType< T >
{
a: T,
}

fn assert_invariant< 'a >( x: InvariantType< *mut &'static str > ) -> InvariantType< *mut &'a str >
{
x
}

fn main()
{
let x: InvariantType< *mut &'static str > = InvariantType { a: &mut "boo", _phantom: Default::default() };
let _: InvariantType< *mut &str > = assert_invariant( x );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: lifetime may not live long enough
--> tests/inc/phantom/compiletime/invariant_type.rs:11:3
|
9 | fn assert_invariant< 'a >( x: InvariantType< *mut &'static str > ) -> InvariantType< *mut &'a str >
| -- lifetime `'a` defined here
10 | {
11 | x
| ^ returning this value requires that `'a` must outlive `'static`
|
= note: requirement occurs because of a mutable pointer to `&str`
= note: mutable pointers are invariant over their type parameter
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
10 changes: 10 additions & 0 deletions module/core/derive_tools/tests/inc/phantom/contravariant_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::*;

#[ allow( dead_code ) ]
#[ the_module::phantom ]
struct ContravariantType< T >
{
a: T,
}

include!( "./only_test/contravariant_type.rs" );
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::marker::PhantomData;

#[ allow( dead_code ) ]
struct ContravariantType< T >
{
a: T,
_phantom: PhantomData< T >,
}

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

#[ allow( dead_code ) ]
#[ the_module::phantom ]
struct CovariantType< T >
{
a: T,
}

include!( "./only_test/covariant_type.rs" );
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::marker::PhantomData;

#[ allow( dead_code ) ]
struct CovariantType< T >
{
a: T,
_phantom: PhantomData< T >,
}

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

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

#[ allow( dead_code ) ]
#[ the_module::phantom ]
struct NameCollisions< T >
{
a : String,
b : i32,
}

include!( "./only_test/name_collisions.rs" );
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[ test ]
fn phantom()
{
let _ = BoundsInlined::< String, i32 > { _phantom: Default::default() };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[ test ]
fn phantom()
{
let _ = BoundsMixed::< String, i32 > { _phantom: Default::default() };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[ test ]
fn phantom()
{
let _ = BoundsWhere::< String, i32 > { _phantom: Default::default() };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn assert_contravariant( x: ContravariantType< &dyn Fn( &'static str ) -> String > ) -> String
{
( x.a )( "test" )
}

#[test]
fn contravariant()
{
let x_fn: &dyn for< 'a > Fn( &'a str ) -> String = &| s: &str |
{
format!( "x_fn: {s}" )
};

let x: ContravariantType< &dyn for< 'a > Fn( &'a str ) -> String > = ContravariantType { a: x_fn, _phantom: Default::default() };
let value = assert_contravariant(x);

assert_eq!( value, String::from( "x_fn: test" ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn assert_covariant< 'a >( x: CovariantType< &'static str > ) -> CovariantType< &'a str >
{
x
}

#[ test ]
fn covariant()
{
let x: CovariantType< &'static str > = CovariantType { a: "boo", _phantom: Default::default(), };
let y: CovariantType< &str > = assert_covariant( x );
assert_eq!( y.a, "boo" );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[ test ]
fn phantom()
{
let _ = NameCollisions::< bool > { a : "boo".into(), b : 3, _phantom: Default::default() };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn assert_send_sync< T: Send + Sync >( _x: SendSyncType< T > )
{}

#[ test ]
fn phantom()
{
let x: SendSyncType::< bool > = SendSyncType { a: true, _phantom: Default::default() };
assert_send_sync( x );
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[ test ]
fn phantom()
{
let _ = StructNamed::< bool >{ a : "boo".into(), b : 3, _phantom: Default::default() };
let _ = StructNamed::< bool > { a : "boo".into(), b : 3, _phantom: Default::default() };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[ test ]
fn phantom()
{
let _ = StructNamedEmpty::< bool > { _phantom: Default::default() };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[ test ]
fn phantom()
{
let _ = StructTupleEmpty::< bool >( Default::default() );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[ test ]
fn phantom()
{
let _ = StructUnit::< bool >( Default::default() );
}
Loading

0 comments on commit d5c2499

Please sign in to comment.