11use core:: marker:: PhantomData ;
2- use core:: ops:: Deref ;
32
43use cgp_component:: UseContext ;
54
@@ -59,19 +58,6 @@ pub trait FieldGetter<Context, Tag> {
5958 fn get_field ( context : & Context , _tag : PhantomData < Tag > ) -> & Self :: Value ;
6059}
6160
62- #[ diagnostic:: do_not_recommend]
63- impl < Context , Tag , Target , Value > HasField < Tag > for Context
64- where
65- Context : DerefMap < Target = Target > ,
66- Target : HasField < Tag , Value = Value > ,
67- {
68- type Value = Value ;
69-
70- fn get_field ( & self , tag : PhantomData < Tag > ) -> & Self :: Value {
71- self . map_deref ( |context| context. get_field ( tag) )
72- }
73- }
74-
7561impl < Context , Tag , Field > FieldGetter < Context , Tag > for UseContext
7662where
7763 Context : HasField < Tag , Value = Field > ,
@@ -83,20 +69,24 @@ where
8369 }
8470}
8571
86- /**
87- A helper trait to help organize the lifetime inference in Rust.
88- Without this, `Self::Target` would need to be `'static`, as Rust couldn't
89- infer the correct lifetime when calling `context.deref().get_field()`.
90- */
91- trait DerefMap : Deref {
92- fn map_deref < T > ( & self , mapper : impl for < ' a > FnOnce ( & ' a Self :: Target ) -> & ' a T ) -> & T ;
72+ impl < ' a , Context , Tag , Value > HasField < Tag > for & ' a Context
73+ where
74+ Context : HasField < Tag , Value = Value > ,
75+ {
76+ type Value = Value ;
77+
78+ fn get_field ( & self , tag : PhantomData < Tag > ) -> & Self :: Value {
79+ Context :: get_field ( self , tag)
80+ }
9381}
9482
95- impl < Context > DerefMap for Context
83+ impl < ' a , Context , Tag , Value > HasField < Tag > for & ' a mut Context
9684where
97- Context : Deref ,
85+ Context : HasField < Tag , Value = Value > ,
9886{
99- fn map_deref < T > ( & self , mapper : impl for < ' a > FnOnce ( & ' a Self :: Target ) -> & ' a T ) -> & T {
100- mapper ( self . deref ( ) )
87+ type Value = Value ;
88+
89+ fn get_field ( & self , tag : PhantomData < Tag > ) -> & Self :: Value {
90+ Context :: get_field ( self , tag)
10191 }
10292}
0 commit comments