@@ -61,7 +61,13 @@ pub fn gen_simple_class(c: &Class, opencv_version: &str) -> String {
61
61
( "fields" , & fields) ,
62
62
(
63
63
"impl" ,
64
- & gen_impl ( c, const_methods. iter ( ) . chain ( mut_methods. iter ( ) ) , & rust_local, opencv_version) ,
64
+ & gen_impl (
65
+ c,
66
+ const_methods. iter ( ) . chain ( mut_methods. iter ( ) ) ,
67
+ & rust_local,
68
+ "" ,
69
+ opencv_version,
70
+ ) ,
65
71
) ,
66
72
( "impls" , & impls) ,
67
73
] ) )
@@ -89,10 +95,22 @@ pub fn gen_boxed_class(c: &Class, opencv_version: &str) -> String {
89
95
const_methods. extend ( more_const_methods) ;
90
96
mut_methods. extend ( more_mut_methods) ;
91
97
98
+ let ( rust_decl_lt, rust_decl_for_lt, rust_elided_lt, rust_phantom_ref) = if let Some ( lt) = c. rust_lifetime ( ) {
99
+ let lt = lt. to_explicit ( ) ;
100
+ (
101
+ format ! ( "<{lt}>" ) ,
102
+ format ! ( ": for <{lt}>" ) ,
103
+ "<'_>" . to_string ( ) ,
104
+ format ! ( "_d: PhantomData<&{lt} mut ()>," ) ,
105
+ )
106
+ } else {
107
+ ( "" . to_string ( ) , "" . to_string ( ) , "" . to_string ( ) , "" . to_string ( ) )
108
+ } ;
109
+
92
110
let type_ref = c. type_ref ( ) ;
93
111
let bases = c. bases ( ) ;
94
112
95
- let traits = gen_traits ( c, & type_ref, & bases, & const_methods, & mut_methods, opencv_version) ;
113
+ let traits = gen_traits ( c, type_ref. clone ( ) , & bases, & const_methods, & mut_methods, opencv_version) ;
96
114
97
115
let rust_local = c. rust_name ( NameStyle :: decl ( ) ) ;
98
116
@@ -116,7 +134,7 @@ pub fn gen_boxed_class(c: &Class, opencv_version: &str) -> String {
116
134
117
135
let mut bases = String :: with_capacity ( all_bases. len ( ) * ( BASE_TPL_SRC . len ( ) + 64 ) ) ;
118
136
all_bases. iter ( ) . chain ( iter:: once ( c) ) . for_each ( |base| {
119
- let base_type_ref = base. type_ref ( ) ;
137
+ let ( base_type_ref_const , base_type_ref_mut ) = make_const_mut ( base. type_ref ( ) ) ;
120
138
BASE_TPL . interpolate_into (
121
139
& mut bases,
122
140
& HashMap :: from ( [
@@ -128,22 +146,12 @@ pub fn gen_boxed_class(c: &Class, opencv_version: &str) -> String {
128
146
"base_rust_full_const" ,
129
147
& base. rust_trait_name ( NameStyle :: ref_ ( ) , Constness :: Const ) ,
130
148
) ,
131
- ( "rust_local" , & type_ref. rust_name ( NameStyle :: decl ( ) ) ) ,
149
+ ( "rust_local" , & rust_local) ,
150
+ ( "rust_elided_lt" , & rust_elided_lt) ,
132
151
( "rust_as_raw_const" , & base. rust_as_raw_name ( Constness :: Const ) ) ,
133
152
( "rust_as_raw_mut" , & base. rust_as_raw_name ( Constness :: Mut ) ) ,
134
- (
135
- "base_rust_extern_const" ,
136
- & base_type_ref
137
- . clone ( )
138
- . with_inherent_constness ( Constness :: Const )
139
- . rust_extern ( ExternDir :: ToCpp ) ,
140
- ) ,
141
- (
142
- "base_rust_extern_mut" ,
143
- & base_type_ref
144
- . with_inherent_constness ( Constness :: Mut )
145
- . rust_extern ( ExternDir :: ToCpp ) ,
146
- ) ,
153
+ ( "base_rust_extern_const" , & base_type_ref_const. rust_extern ( ExternDir :: ToCpp ) ) ,
154
+ ( "base_rust_extern_mut" , & base_type_ref_mut. rust_extern ( ExternDir :: ToCpp ) ) ,
147
155
] ) ,
148
156
)
149
157
} ) ;
@@ -160,26 +168,22 @@ pub fn gen_boxed_class(c: &Class, opencv_version: &str) -> String {
160
168
kind. as_static_method ( ) . is_some ( ) || kind. as_constructor ( ) . is_some ( )
161
169
} ) ;
162
170
171
+ let ( type_ref_const, type_ref_mut) = make_const_mut ( type_ref) ;
163
172
BOXED_TPL . interpolate ( & HashMap :: from ( [
164
173
( "doc_comment" , c. rendered_doc_comment ( "///" , opencv_version) . as_str ( ) ) ,
165
174
( "debug" , & c. get_debug ( ) ) ,
166
175
( "rust_local" , & rust_local) ,
176
+ ( "rust_decl_lt" , & rust_decl_lt) ,
177
+ ( "rust_decl_for_lt" , & rust_decl_for_lt) ,
178
+ ( "rust_elided_lt" , & rust_elided_lt) ,
179
+ ( "rust_phantom_ref" , & rust_phantom_ref) ,
167
180
( "rust_full" , & c. rust_name ( NameStyle :: ref_ ( ) ) ) ,
168
- (
169
- "rust_extern_const" ,
170
- & type_ref
171
- . clone ( )
172
- . with_inherent_constness ( Constness :: Const )
173
- . rust_extern ( ExternDir :: ToCpp ) ,
174
- ) ,
175
- (
176
- "rust_extern_mut" ,
177
- & type_ref. with_inherent_constness ( Constness :: Mut ) . rust_extern ( ExternDir :: ToCpp ) ,
178
- ) ,
181
+ ( "rust_extern_const" , & type_ref_const. rust_extern ( ExternDir :: ToCpp ) ) ,
182
+ ( "rust_extern_mut" , & type_ref_mut. rust_extern ( ExternDir :: ToCpp ) ) ,
179
183
( "traits" , & traits) ,
180
184
( "extern_delete" , & extern_delete) ,
181
185
( "bases" , & bases) ,
182
- ( "impl" , & gen_impl ( c, methods, & rust_local, opencv_version) ) ,
186
+ ( "impl" , & gen_impl ( c, methods, & rust_local, & rust_decl_lt , opencv_version) ) ,
183
187
( "impls" , & impls) ,
184
188
] ) )
185
189
}
@@ -293,7 +297,13 @@ fn all_methods_const_mut<'tu, 'ge>(c: &Class<'tu, 'ge>) -> (Vec<Func<'tu, 'ge>>,
293
297
( const_methods, mut_methods)
294
298
}
295
299
296
- fn gen_impl < ' f > ( c : & Class , methods : impl Iterator < Item = & ' f Func < ' f , ' f > > , rust_local : & str , opencv_version : & str ) -> String {
300
+ fn gen_impl < ' f > (
301
+ c : & Class ,
302
+ methods : impl Iterator < Item = & ' f Func < ' f , ' f > > ,
303
+ rust_local : & str ,
304
+ rust_decl_lt : & str ,
305
+ opencv_version : & str ,
306
+ ) -> String {
297
307
static IMPL_TPL : Lazy < CompiledInterpolation > = Lazy :: new ( || include_str ! ( "../tpl/class/impl.tpl.rs" ) . compile_interpolation ( ) ) ;
298
308
299
309
let consts = c. consts ( ) . iter ( ) . map ( |c| c. gen_rust ( opencv_version) ) . join ( "" ) ;
@@ -307,6 +317,7 @@ fn gen_impl<'f>(c: &Class, methods: impl Iterator<Item = &'f Func<'f, 'f>>, rust
307
317
} else {
308
318
IMPL_TPL . interpolate ( & HashMap :: from ( [
309
319
( "rust_local" , rust_local) ,
320
+ ( "rust_decl_lt" , rust_decl_lt) ,
310
321
( "consts" , & consts) ,
311
322
( "inherent_methods" , & inherent_methods) ,
312
323
] ) )
@@ -315,7 +326,7 @@ fn gen_impl<'f>(c: &Class, methods: impl Iterator<Item = &'f Func<'f, 'f>>, rust
315
326
316
327
fn gen_traits (
317
328
c : & Class ,
318
- type_ref : & TypeRef ,
329
+ type_ref : TypeRef ,
319
330
bases : & [ Class ] ,
320
331
const_methods : & [ Func ] ,
321
332
mut_methods : & [ Func ] ,
@@ -358,6 +369,7 @@ fn gen_traits(
358
369
opencv_version,
359
370
) ;
360
371
372
+ let ( type_ref_const, type_ref_mut) = make_const_mut ( type_ref) ;
361
373
TRAIT_TPL . interpolate ( & HashMap :: from ( [
362
374
( "debug" , c. get_debug ( ) . as_str ( ) ) ,
363
375
( "rust_trait_local_mut" , & c. rust_trait_name ( NameStyle :: decl ( ) , Constness :: Mut ) ) ,
@@ -368,20 +380,8 @@ fn gen_traits(
368
380
( "rust_as_raw_const" , & c. rust_as_raw_name ( Constness :: Const ) ) ,
369
381
( "rust_as_raw_mut" , & c. rust_as_raw_name ( Constness :: Mut ) ) ,
370
382
( "rust_name_ref" , & c. rust_name ( NameStyle :: ref_ ( ) ) ) ,
371
- (
372
- "rust_extern_const" ,
373
- & type_ref
374
- . clone ( )
375
- . with_inherent_constness ( Constness :: Const )
376
- . rust_extern ( ExternDir :: ToCpp ) ,
377
- ) ,
378
- (
379
- "rust_extern_mut" ,
380
- & type_ref
381
- . clone ( )
382
- . with_inherent_constness ( Constness :: Mut )
383
- . rust_extern ( ExternDir :: ToCpp ) ,
384
- ) ,
383
+ ( "rust_extern_const" , & type_ref_const. rust_extern ( ExternDir :: ToCpp ) ) ,
384
+ ( "rust_extern_mut" , & type_ref_mut. rust_extern ( ExternDir :: ToCpp ) ) ,
385
385
( "trait_bases_const" , & trait_bases_const) ,
386
386
( "trait_bases_mut" , & trait_bases_mut) ,
387
387
( "trait_const_methods" , & trait_const_methods) ,
@@ -486,6 +486,16 @@ impl Impls for String {
486
486
}
487
487
}
488
488
489
+ fn make_const_mut < ' tu , ' ge > ( type_ref : TypeRef < ' tu , ' ge > ) -> ( TypeRef < ' tu , ' ge > , TypeRef < ' tu , ' ge > ) {
490
+ if type_ref. inherent_constness ( ) . is_const ( ) {
491
+ let type_ref_mut = type_ref. clone ( ) . with_inherent_constness ( Constness :: Mut ) ;
492
+ ( type_ref, type_ref_mut)
493
+ } else {
494
+ let type_ref_const = type_ref. clone ( ) . with_inherent_constness ( Constness :: Const ) ;
495
+ ( type_ref_const, type_ref)
496
+ }
497
+ }
498
+
489
499
mod method {
490
500
use crate :: func:: { FuncCppBody , FuncDesc , FuncKind , ReturnKind } ;
491
501
use crate :: type_ref:: TypeRef ;
0 commit comments