@@ -28,21 +28,34 @@ pub(crate) trait DynTypedReduceRule: Send + Sync {
2828 ) -> VortexResult < Option < Expression > > ;
2929}
3030
31- /// Concrete wrapper that implements DynReduceRule for rules with `&dyn RewriteContext` context.
32- struct ReduceRuleAdapter < V , R >
33- where
34- V : VTable ,
35- for < ' a > R : ReduceRule < V , & ' a dyn RewriteContext > ,
36- {
31+ /// Type-erased wrapper for ParentReduceRule that allows dynamic dispatch.
32+ pub ( crate ) trait DynParentReduceRule : Send + Sync {
33+ fn reduce_parent_dyn (
34+ & self ,
35+ expr : & Expression ,
36+ parent : & Expression ,
37+ child_idx : usize ,
38+ ctx : & dyn RewriteContext ,
39+ ) -> VortexResult < Option < Expression > > ;
40+ }
41+
42+ pub ( crate ) trait DynTypedParentReduceRule : Send + Sync {
43+ fn reduce_parent_dyn_typed (
44+ & self ,
45+ expr : & Expression ,
46+ parent : & Expression ,
47+ child_idx : usize ,
48+ ctx : & dyn TypedRewriteContext ,
49+ ) -> VortexResult < Option < Expression > > ;
50+ }
51+
52+ /// Universal adapter for both ReduceRule and ParentReduceRule with any context type.
53+ struct RuleAdapter < V : VTable , R > {
3754 rule : R ,
3855 _phantom : PhantomData < V > ,
3956}
4057
41- impl < V , R > ReduceRuleAdapter < V , R >
42- where
43- V : VTable ,
44- for < ' a > R : ReduceRule < V , & ' a dyn RewriteContext > ,
45- {
58+ impl < V : VTable , R > RuleAdapter < V , R > {
4659 fn new ( rule : R ) -> Self {
4760 Self {
4861 rule,
5164 }
5265}
5366
54- impl < V , R > DynReduceRule for ReduceRuleAdapter < V , R >
67+ // Implement DynReduceRule for any ReduceRule with RewriteContext
68+ impl < V , R > DynReduceRule for RuleAdapter < V , R >
5569where
5670 V : VTable ,
5771 for < ' a > R : ReduceRule < V , & ' a dyn RewriteContext > ,
6882 }
6983}
7084
71- /// Concrete wrapper that implements DynReduceRule for rules with `&dyn TypedRewriteContext` context.
72- struct TypedReduceRuleAdapter < V , R >
73- where
74- V : VTable ,
75- for < ' a > R : ReduceRule < V , & ' a dyn TypedRewriteContext > ,
76- {
77- rule : R ,
78- _phantom : PhantomData < V > ,
79- }
80-
81- impl < V , R > TypedReduceRuleAdapter < V , R >
82- where
83- V : VTable ,
84- for < ' a > R : ReduceRule < V , & ' a dyn TypedRewriteContext > ,
85- {
86- fn new ( rule : R ) -> Self {
87- Self {
88- rule,
89- _phantom : PhantomData ,
90- }
91- }
92- }
93-
94- impl < V , R > DynTypedReduceRule for TypedReduceRuleAdapter < V , R >
85+ // Implement DynTypedReduceRule for any ReduceRule with TypedRewriteContext
86+ impl < V , R > DynTypedReduceRule for RuleAdapter < V , R >
9587where
9688 V : VTable ,
9789 for < ' a > R : ReduceRule < V , & ' a dyn TypedRewriteContext > ,
@@ -108,41 +100,8 @@ where
108100 }
109101}
110102
111- /// Type-erased wrapper for ParentReduceRule that allows dynamic dispatch.
112- pub ( crate ) trait DynParentReduceRule : Send + Sync {
113- fn reduce_parent_dyn (
114- & self ,
115- expr : & Expression ,
116- parent : & Expression ,
117- child_idx : usize ,
118- ctx : & dyn RewriteContext ,
119- ) -> VortexResult < Option < Expression > > ;
120- }
121-
122- /// Concrete wrapper that implements DynParentReduceRule for a specific VTable type.
123- struct ParentReduceRuleAdapter < V , R >
124- where
125- V : VTable ,
126- for < ' a > R : ParentReduceRule < V , & ' a dyn RewriteContext > ,
127- {
128- rule : R ,
129- _phantom : PhantomData < V > ,
130- }
131-
132- impl < V , R > ParentReduceRuleAdapter < V , R >
133- where
134- V : VTable ,
135- for < ' a > R : ParentReduceRule < V , & ' a dyn RewriteContext > ,
136- {
137- fn new ( rule : R ) -> Self {
138- Self {
139- rule,
140- _phantom : PhantomData ,
141- }
142- }
143- }
144-
145- impl < V , R > DynParentReduceRule for ParentReduceRuleAdapter < V , R >
103+ // Implement DynParentReduceRule for any ParentReduceRule with RewriteContext
104+ impl < V , R > DynParentReduceRule for RuleAdapter < V , R >
146105where
147106 V : VTable ,
148107 for < ' a > R : ParentReduceRule < V , & ' a dyn RewriteContext > ,
@@ -161,39 +120,8 @@ where
161120 }
162121}
163122
164- pub ( crate ) trait DynTypedParentReduceRule : Send + Sync {
165- fn reduce_parent_dyn_typed (
166- & self ,
167- expr : & Expression ,
168- parent : & Expression ,
169- child_idx : usize ,
170- ctx : & dyn TypedRewriteContext ,
171- ) -> VortexResult < Option < Expression > > ;
172- }
173-
174- struct TypedParentReduceRuleAdapter < V : VTable , R >
175- where
176- V : VTable ,
177- for < ' a > R : ParentReduceRule < V , & ' a dyn TypedRewriteContext > ,
178- {
179- rule : R ,
180- _phantom : PhantomData < V > ,
181- }
182-
183- impl < V , R > TypedParentReduceRuleAdapter < V , R >
184- where
185- V : VTable ,
186- for < ' a > R : ParentReduceRule < V , & ' a dyn TypedRewriteContext > ,
187- {
188- fn new ( rule : R ) -> Self {
189- Self {
190- rule,
191- _phantom : PhantomData ,
192- }
193- }
194- }
195-
196- impl < V , R > DynTypedParentReduceRule for TypedParentReduceRuleAdapter < V , R >
123+ // Implement DynTypedParentReduceRule for any ParentReduceRule with TypedRewriteContext
124+ impl < V , R > DynTypedParentReduceRule for RuleAdapter < V , R >
197125where
198126 V : VTable ,
199127 for < ' a > R : ParentReduceRule < V , & ' a dyn TypedRewriteContext > ,
@@ -252,7 +180,7 @@ impl RewriteRuleRegistry {
252180 for < ' a > R : ReduceRule < V , & ' a dyn TypedRewriteContext > ,
253181 {
254182 let id = vtable. id ( ) ;
255- let adapter = TypedReduceRuleAdapter :: new ( rule) ;
183+ let adapter = RuleAdapter :: new ( rule) ;
256184 self . typed_reduce_rules
257185 . entry ( id)
258186 . or_default ( )
@@ -268,7 +196,7 @@ impl RewriteRuleRegistry {
268196 for < ' a > R : ReduceRule < V , & ' a dyn RewriteContext > ,
269197 {
270198 let id = vtable. id ( ) ;
271- let adapter = ReduceRuleAdapter :: new ( rule) ;
199+ let adapter = RuleAdapter :: new ( rule) ;
272200 self . reduce_rules
273201 . entry ( id)
274202 . or_default ( )
@@ -282,7 +210,7 @@ impl RewriteRuleRegistry {
282210 for < ' a > R : ParentReduceRule < V , & ' a dyn RewriteContext > ,
283211 {
284212 let id = vtable. id ( ) ;
285- let adapter = ParentReduceRuleAdapter :: new ( rule) ;
213+ let adapter = RuleAdapter :: new ( rule) ;
286214 self . parent_rules
287215 . entry ( id)
288216 . or_default ( )
@@ -297,7 +225,7 @@ impl RewriteRuleRegistry {
297225 for < ' a > R : ParentReduceRule < V , & ' a dyn TypedRewriteContext > ,
298226 {
299227 let id = vtable. id ( ) ;
300- let adapter = TypedParentReduceRuleAdapter :: new ( rule) ;
228+ let adapter = RuleAdapter :: new ( rule) ;
301229 self . typed_parent_rules
302230 . entry ( id)
303231 . or_default ( )
0 commit comments