File tree Expand file tree Collapse file tree 3 files changed +26
-17
lines changed
cgp-macro-lib/src/derive_getter
cgp-tests/src/tests/getter Expand file tree Collapse file tree 3 files changed +26
-17
lines changed Original file line number Diff line number Diff line change @@ -28,9 +28,12 @@ pub fn derive_blanket_impl(
2828 . insert ( 0 , parse2 ( context_type. to_token_stream ( ) ) ?) ;
2929
3030 let where_clause = generics. make_where_clause ( ) ;
31- where_clause. predicates . push ( parse2 ( quote ! {
32- #context_type: #supertrait_constraints
33- } ) ?) ;
31+
32+ if !supertrait_constraints. is_empty ( ) {
33+ where_clause. predicates . push ( parse2 ( quote ! {
34+ #context_type: #supertrait_constraints
35+ } ) ?) ;
36+ }
3437
3538 for field in fields {
3639 let ( receiver_type, context_arg) = match & field. receiver_mode {
@@ -62,26 +65,13 @@ pub fn derive_blanket_impl(
6265 let ( _, type_generics, _) = consumer_trait. generics . split_for_impl ( ) ;
6366 let ( impl_generics, _, where_clause) = generics. split_for_impl ( ) ;
6467
65- let mut item_impl: ItemImpl = parse2 ( quote ! {
68+ let item_impl: ItemImpl = parse2 ( quote ! {
6669 impl #impl_generics #consumer_name #type_generics for #context_type
6770 #where_clause
6871 {
6972 #methods
7073 }
7174 } ) ?;
7275
73- item_impl
74- . generics
75- . params
76- . extend ( consumer_trait. generics . params . clone ( ) ) ;
77-
78- if let Some ( consumer_where_clause) = & consumer_trait. generics . where_clause {
79- item_impl
80- . generics
81- . make_where_clause ( )
82- . predicates
83- . extend ( consumer_where_clause. predicates . clone ( ) ) ;
84- }
85-
8676 Ok ( item_impl)
8777}
Original file line number Diff line number Diff line change 1+ use cgp:: prelude:: * ;
2+
3+ #[ cgp_auto_getter]
4+ pub trait HasFoo < Foo > {
5+ fn foo ( & self , _tag : PhantomData < Foo > ) -> & Foo ;
6+ }
7+
8+ #[ derive( HasField ) ]
9+ pub struct App {
10+ pub foo : u32 ,
11+ }
12+
13+ #[ test]
14+ fn test_generic_auto_getter ( ) {
15+ let app = App { foo : 42 } ;
16+
17+ assert_eq ! ( app. foo( PhantomData :: <u32 >) , & 42 ) ;
18+ }
Original file line number Diff line number Diff line change 11pub mod abstract_type;
2+ pub mod auto_generics;
23pub mod clone;
34pub mod mref;
45pub mod non_self;
You can’t perform that action at this time.
0 commit comments