Skip to content

Commit 7aef8c0

Browse files
committed
Improve FuncDesc API
1 parent a80a6c7 commit 7aef8c0

File tree

8 files changed

+539
-496
lines changed

8 files changed

+539
-496
lines changed

binding-generator/src/class.rs

Lines changed: 66 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use crate::debug::{DefinitionLocation, LocationName};
1313
use crate::element::ExcludeKind;
1414
use crate::entity::{ControlFlowExt, ToEntity};
1515
use crate::field::FieldDesc;
16-
use crate::func::{FuncCppBody, FuncDesc, FuncKind, FuncRustBody, FuncRustExtern, ReturnKind};
16+
use crate::func::{FuncCppBody, FuncDesc, FuncKind, ReturnKind};
1717
use crate::type_ref::{Constness, CppNameStyle, StrEnc, StrType, TypeRef, TypeRefDesc, TypeRefTypeHint};
1818
use crate::writer::rust_native::element::RustElement;
1919
use crate::{
20-
settings, ClassKindOverride, Const, DefaultElement, Element, EntityExt, Enum, Field, Func, FuncTypeHint, GeneratedType,
21-
GeneratorEnv, NameDebug, StrExt,
20+
settings, ClassKindOverride, Const, DefaultElement, Element, EntityExt, Enum, Field, Func, GeneratedType, GeneratorEnv,
21+
NameDebug, StrExt,
2222
};
2323

2424
mod desc;
@@ -419,6 +419,8 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
419419
out.extend(fields.flat_map(|fld| {
420420
iter::from_fn({
421421
let doc_comment = Rc::from(fld.doc_comment());
422+
let def_loc = fld.file_line_name().location;
423+
let rust_module = fld.rust_module();
422424
let mut fld_type_ref = fld.type_ref();
423425
let fld_type_kind = fld_type_ref.kind();
424426
if fld_type_kind
@@ -432,72 +434,64 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
432434
}
433435
let fld_type_kind = fld_type_ref.kind();
434436
let fld_type_ref_return_as_naked = fld_type_kind.return_as_naked(fld_type_ref.type_hint());
437+
let return_kind = ReturnKind::infallible(fld_type_ref_return_as_naked);
435438
let fld_const = fld.constness();
436439
let passed_by_ref = fld_type_kind.can_return_as_direct_reference();
437440
let (mut read_const_yield, mut read_mut_yield) = if passed_by_ref && fld_const.is_mut() {
438441
let read_const_func = if constness_filter.map_or(true, |c| c.is_const()) {
439-
Some(Func::new_desc(FuncDesc {
440-
type_hint: FuncTypeHint::None,
441-
kind: FuncKind::FieldAccessor(self.clone(), fld.clone()),
442-
cpp_name: fld.cpp_name(CppNameStyle::Reference).into(),
443-
rust_custom_leafname: None,
444-
rust_module: fld.rust_module().into(),
445-
constness: Constness::Const,
446-
return_kind: ReturnKind::infallible(fld_type_ref_return_as_naked),
447-
doc_comment: Rc::clone(&doc_comment),
448-
def_loc: fld.file_line_name().location,
449-
rust_generic_decls: Rc::new([]),
450-
arguments: Rc::new([]),
451-
return_type_ref: fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
452-
cpp_body: FuncCppBody::ManualCall("{{name}}".into()),
453-
rust_body: FuncRustBody::Auto,
454-
rust_extern_definition: FuncRustExtern::Auto,
455-
}))
442+
Some(Func::new_desc(
443+
FuncDesc::new(
444+
FuncKind::FieldAccessor(self.clone(), fld.clone()),
445+
Constness::Const,
446+
return_kind,
447+
fld.cpp_name(CppNameStyle::Reference),
448+
rust_module.clone(),
449+
[],
450+
fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
451+
)
452+
.def_loc(def_loc.clone())
453+
.doc_comment(Rc::clone(&doc_comment))
454+
.cpp_body(FuncCppBody::ManualCall("{{name}}".into())),
455+
))
456456
} else {
457457
None
458458
};
459459
let read_mut_func = if constness_filter.map_or(true, |c| c.is_mut()) {
460460
let cpp_name = fld.cpp_name(CppNameStyle::Declaration);
461-
Some(Func::new_desc(FuncDesc {
462-
type_hint: FuncTypeHint::None,
463-
kind: FuncKind::FieldAccessor(self.clone(), fld.clone()),
464-
cpp_name: format!("{cpp_name}Mut").into(),
465-
rust_custom_leafname: None,
466-
rust_module: fld.rust_module().into(),
467-
constness: Constness::Mut,
468-
return_kind: ReturnKind::infallible(fld_type_ref_return_as_naked),
469-
doc_comment: Rc::clone(&doc_comment),
470-
def_loc: fld.file_line_name().location,
471-
rust_generic_decls: Rc::new([]),
472-
arguments: Rc::new([]),
473-
return_type_ref: fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Mut),
474-
cpp_body: FuncCppBody::ManualCall("{{name}}".into()),
475-
rust_body: FuncRustBody::Auto,
476-
rust_extern_definition: FuncRustExtern::Auto,
477-
}))
461+
Some(Func::new_desc(
462+
FuncDesc::new(
463+
FuncKind::FieldAccessor(self.clone(), fld.clone()),
464+
Constness::Mut,
465+
return_kind,
466+
format!("{cpp_name}Mut"),
467+
rust_module.clone(),
468+
[],
469+
fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Mut),
470+
)
471+
.def_loc(def_loc.clone())
472+
.doc_comment(Rc::clone(&doc_comment))
473+
.cpp_body(FuncCppBody::ManualCall("{{name}}".into())),
474+
))
478475
} else {
479476
None
480477
};
481478
(read_const_func, read_mut_func)
482479
} else {
483480
let read_const_func = if constness_filter.map_or(true, |c| c == fld_const) {
484-
Some(Func::new_desc(FuncDesc {
485-
type_hint: FuncTypeHint::None,
486-
kind: FuncKind::FieldAccessor(self.clone(), fld.clone()),
487-
cpp_name: fld.cpp_name(CppNameStyle::Reference).into(),
488-
rust_custom_leafname: None,
489-
rust_module: fld.rust_module().into(),
490-
constness: fld_const,
491-
return_kind: ReturnKind::infallible(fld_type_ref_return_as_naked),
492-
doc_comment: Rc::clone(&doc_comment),
493-
def_loc: fld.file_line_name().location,
494-
rust_generic_decls: Rc::new([]),
495-
arguments: Rc::new([]),
496-
return_type_ref: fld_type_ref.as_ref().clone(),
497-
cpp_body: FuncCppBody::ManualCall("{{name}}".into()),
498-
rust_body: FuncRustBody::Auto,
499-
rust_extern_definition: FuncRustExtern::Auto,
500-
}))
481+
Some(Func::new_desc(
482+
FuncDesc::new(
483+
FuncKind::FieldAccessor(self.clone(), fld.clone()),
484+
fld_const,
485+
return_kind,
486+
fld.cpp_name(CppNameStyle::Reference),
487+
rust_module.clone(),
488+
[],
489+
fld_type_ref.as_ref().clone(),
490+
)
491+
.def_loc(def_loc.clone())
492+
.doc_comment(Rc::clone(&doc_comment))
493+
.cpp_body(FuncCppBody::ManualCall("{{name}}".into())),
494+
))
501495
} else {
502496
None
503497
};
@@ -509,28 +503,24 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
509503
{
510504
let cpp_name = fld.cpp_name(CppNameStyle::Declaration);
511505
let (first_letter, rest) = cpp_name.split_at(1);
512-
let write_func = Func::new_desc(FuncDesc {
513-
type_hint: FuncTypeHint::None,
514-
kind: FuncKind::FieldAccessor(self.clone(), fld.clone()),
515-
cpp_name: format!("set{}{rest}", first_letter.to_uppercase()).into(),
516-
rust_custom_leafname: None,
517-
rust_module: fld.rust_module().into(),
518-
constness: Constness::Mut,
519-
doc_comment,
520-
def_loc: fld.file_line_name().location,
521-
rust_generic_decls: Rc::new([]),
522-
arguments: Rc::new([Field::new_desc(FieldDesc {
523-
cpp_fullname: "val".into(),
524-
type_ref: fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
525-
default_value: fld.default_value().map(|v| v.into()),
526-
})]),
527-
return_kind: ReturnKind::InfallibleNaked,
528-
return_type_ref: TypeRefDesc::void(),
529-
cpp_body: FuncCppBody::ManualCall("{{name}} = {{args}}".into()),
530-
rust_body: FuncRustBody::Auto,
531-
rust_extern_definition: FuncRustExtern::Auto,
532-
});
533-
Some(write_func)
506+
Some(Func::new_desc(
507+
FuncDesc::new(
508+
FuncKind::FieldAccessor(self.clone(), fld.clone()),
509+
Constness::Mut,
510+
ReturnKind::InfallibleNaked,
511+
format!("set{}{rest}", first_letter.to_uppercase()),
512+
rust_module.clone(),
513+
[Field::new_desc(FieldDesc {
514+
cpp_fullname: "val".into(),
515+
type_ref: fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
516+
default_value: fld.default_value().map(|v| v.into()),
517+
})],
518+
TypeRefDesc::void(),
519+
)
520+
.doc_comment(doc_comment)
521+
.def_loc(def_loc)
522+
.cpp_body(FuncCppBody::ManualCall("{{name}} = {{args}}".into())),
523+
))
534524
} else {
535525
None
536526
};

binding-generator/src/func/desc.rs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::borrow::Cow;
22
use std::rc::Rc;
33

4+
use super::FuncKind;
45
use crate::debug::DefinitionLocation;
56
use crate::field::Field;
67
use crate::func::ReturnKind;
78
use crate::type_ref::{Constness, TypeRef, TypeRefDesc};
89
use crate::{Class, Func, FuncTypeHint};
910

10-
use super::FuncKind;
11-
1211
#[derive(Clone, Debug)]
1312
pub struct FuncDesc<'tu, 'ge> {
1413
pub kind: FuncKind<'tu, 'ge>,
@@ -37,11 +36,8 @@ impl<'tu, 'ge> FuncDesc<'tu, 'ge> {
3736
cpp_name: impl Into<Rc<str>>,
3837
rust_module: impl Into<Rc<str>>,
3938
arguments: impl Into<Rc<[Field<'tu, 'ge>]>>,
40-
cpp_body: FuncCppBody,
41-
rust_body: FuncRustBody,
4239
return_type_ref: TypeRef<'tu, 'ge>,
4340
) -> Self {
44-
#![allow(clippy::too_many_arguments)]
4541
Self {
4642
kind,
4743
type_hint: FuncTypeHint::None,
@@ -55,24 +51,61 @@ impl<'tu, 'ge> FuncDesc<'tu, 'ge> {
5551
rust_generic_decls: Rc::new([]),
5652
arguments: arguments.into(),
5753
return_type_ref,
58-
cpp_body,
59-
rust_body,
54+
cpp_body: FuncCppBody::Auto,
55+
rust_body: FuncRustBody::Auto,
6056
rust_extern_definition: FuncRustExtern::Auto,
6157
}
6258
}
6359

60+
#[inline]
61+
pub fn doc_comment(mut self, doc_comment: impl Into<Rc<str>>) -> Self {
62+
self.doc_comment = doc_comment.into();
63+
self
64+
}
65+
66+
#[inline]
67+
pub fn cpp_body(mut self, cpp_body: FuncCppBody) -> Self {
68+
self.cpp_body = cpp_body;
69+
self
70+
}
71+
72+
#[inline]
73+
pub fn rust_body(mut self, rust_body: FuncRustBody) -> Self {
74+
self.rust_body = rust_body;
75+
self
76+
}
77+
78+
#[inline]
79+
pub fn def_loc(mut self, def_loc: DefinitionLocation) -> Self {
80+
self.def_loc = def_loc;
81+
self
82+
}
83+
84+
#[inline]
85+
pub fn rust_extern_definition(mut self, rust_extern_definition: FuncRustExtern) -> Self {
86+
self.rust_extern_definition = rust_extern_definition;
87+
self
88+
}
89+
90+
#[inline]
91+
pub fn rust_generic_decls(mut self, rust_generic_decls: impl Into<Rc<[(String, String)]>>) -> Self {
92+
self.rust_generic_decls = rust_generic_decls.into();
93+
self
94+
}
95+
6496
pub fn method_delete(class_desc: Class<'tu, 'ge>) -> Func<'tu, 'ge> {
65-
Func::new_desc(FuncDesc::new(
66-
FuncKind::InstanceMethod(class_desc),
67-
Constness::Mut,
68-
ReturnKind::InfallibleNaked,
69-
"delete",
70-
"<unused>",
71-
vec![],
72-
FuncCppBody::ManualCall("delete instance".into()),
73-
FuncRustBody::Auto,
74-
TypeRefDesc::void(),
75-
))
97+
Func::new_desc(
98+
FuncDesc::new(
99+
FuncKind::InstanceMethod(class_desc),
100+
Constness::Mut,
101+
ReturnKind::InfallibleNaked,
102+
"delete",
103+
"<unused>",
104+
[],
105+
TypeRefDesc::void(),
106+
)
107+
.cpp_body(FuncCppBody::ManualCall("delete instance".into())),
108+
)
76109
}
77110
}
78111

binding-generator/src/settings/func_inject.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::class::ClassDesc;
22
use crate::field::{Field, FieldDesc};
3-
use crate::func::{FuncCppBody, FuncDesc, FuncKind, FuncRustBody, ReturnKind};
3+
use crate::func::FuncKind::{Constructor, InstanceMethod};
4+
use crate::func::ReturnKind::{Fallible, InfallibleNaked};
5+
use crate::func::{FuncCppBody, FuncDesc};
46
use crate::type_ref::Constness::{Const, Mut};
57
use crate::type_ref::{TypeRef, TypeRefDesc, TypeRefTypeHint};
68
use crate::writer::rust_native::type_ref::Lifetime;
@@ -12,52 +14,49 @@ pub fn func_inject_factory(module: &str) -> Vec<FuncFactory> {
1214
match module {
1315
"core" => vec![
1416
(|| {
15-
Func::new_desc(FuncDesc::new(
16-
FuncKind::InstanceMethod(ClassDesc::cv_matconstiterator()),
17-
Const,
18-
ReturnKind::InfallibleNaked,
19-
"type",
20-
"core",
21-
vec![],
22-
FuncCppBody::ManualCall("instance->m->type()".into()),
23-
FuncRustBody::Auto,
24-
TypeRefDesc::int(),
25-
))
17+
Func::new_desc(
18+
FuncDesc::new(
19+
InstanceMethod(ClassDesc::cv_matconstiterator()),
20+
Const,
21+
InfallibleNaked,
22+
"type",
23+
"core",
24+
[],
25+
TypeRefDesc::int(),
26+
)
27+
.cpp_body(FuncCppBody::ManualCall("instance->m->type()".into())),
28+
)
2629
}) as FuncFactory, // todo: remove this cast when MSRV allows
2730
|| {
2831
Func::new_desc(FuncDesc::new(
29-
FuncKind::InstanceMethod(ClassDesc::cv_mat()),
32+
InstanceMethod(ClassDesc::cv_mat()),
3033
Const,
31-
ReturnKind::Fallible,
34+
Fallible,
3235
"size",
3336
"core",
34-
vec![],
35-
FuncCppBody::Auto,
36-
FuncRustBody::Auto,
37+
[],
3738
TypeRefDesc::cv_size(),
3839
))
3940
},
4041
|| {
4142
Func::new_desc(FuncDesc::new(
42-
FuncKind::InstanceMethod(ClassDesc::cv_umat()),
43+
InstanceMethod(ClassDesc::cv_umat()),
4344
Const,
44-
ReturnKind::Fallible,
45+
Fallible,
4546
"size",
4647
"core",
47-
vec![],
48-
FuncCppBody::Auto,
49-
FuncRustBody::Auto,
48+
[],
5049
TypeRefDesc::cv_size(),
5150
))
5251
},
5352
|| {
5453
Func::new_desc(FuncDesc::new(
55-
FuncKind::Constructor(ClassDesc::cv_input_array()),
54+
Constructor(ClassDesc::cv_input_array()),
5655
Mut,
57-
ReturnKind::Fallible,
56+
Fallible,
5857
"_InputArray",
5958
"core",
60-
vec![
59+
[
6160
Field::new_desc(FieldDesc::new(
6261
"vec",
6362
TypeRef::new_array(TypeRefDesc::uchar().with_inherent_constness(Const), None),
@@ -68,8 +67,6 @@ pub fn func_inject_factory(module: &str) -> Vec<FuncFactory> {
6867
TypeRefDesc::int().with_type_hint(TypeRefTypeHint::LenForSlice(["vec".to_string()].as_slice().into(), 1)),
6968
)),
7069
],
71-
FuncCppBody::Auto,
72-
FuncRustBody::Auto,
7370
TypeRefDesc::cv_input_array()
7471
.with_inherent_constness(Const)
7572
.with_type_hint(TypeRefTypeHint::BoxedAsRef(Const, "vec", Lifetime::Elided)),

0 commit comments

Comments
 (0)