Skip to content

Commit 55cd32b

Browse files
committed
Add support for passing the string length as argument
1 parent a0c6873 commit 55cd32b

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

binding-generator/src/settings/argument_override.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ fn core_arg_override_factory() -> ArgOverride {
169169
HashMap::from([("allocator", TypeRefTypeHint::ExplicitLifetime(Lifetime::statik()))]),
170170
)],
171171
),
172+
(
173+
"cv::ocl::convertTypeStr",
174+
vec![(
175+
pred!(mut, ["sdepth", "ddepth", "cn", "buf", "buf_size"]),
176+
HashMap::from([("buf", TypeRefTypeHint::StringWithLen("buf_size".into()))]),
177+
)],
178+
),
172179
]))
173180
}
174181

binding-generator/src/type_ref/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22
use std::fmt;
3+
use std::rc::Rc;
34
use std::sync::Arc;
45

56
use clang::Type;
@@ -20,6 +21,8 @@ pub enum TypeRefTypeHint {
2021
LenForSlice(Arc<[String]>, usize),
2122
/// Treat C++ string as a byte buffer (`Vec<u8>`) instead of an actual string, argument is optional cpp_arg_name of the argument that specifies the buffer byte length
2223
StringAsBytes(Option<Arc<str>>),
24+
/// String len is passed in an additional argument (cpp_arg_name)
25+
StringWithLen(Rc<str>),
2326
/// when C++ char needs to be represented as Rust char
2427
CharAsRustChar,
2528
/// for the cases when `char *` should not be treated as string, but as a pointer to single char
@@ -57,6 +60,7 @@ impl TypeRefTypeHint {
5760
| Self::Slice
5861
| Self::LenForSlice(_, _)
5962
| Self::StringAsBytes(_)
63+
| Self::StringWithLen(_)
6064
| Self::CharAsRustChar
6165
| Self::CharPtrSingleChar
6266
| Self::PrimitivePtrAsRaw
@@ -74,6 +78,7 @@ impl TypeRefTypeHint {
7478
| Self::Slice
7579
| Self::LenForSlice(_, _)
7680
| Self::StringAsBytes(_)
81+
| Self::StringWithLen(_)
7782
| Self::CharAsRustChar
7883
| Self::CharPtrSingleChar
7984
| Self::PrimitivePtrAsRaw

binding-generator/src/writer/rust_native/type_ref/render_lane/string.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,22 @@ impl RenderLaneTrait for OutStringRenderLane<'_, '_> {
108108
StrType::StdString(_) => format!("std::string {name}_out"),
109109
StrType::CvString(_) => format!("cv::String {name}_out"),
110110
StrType::CharPtr(str_enc) => {
111-
let len = if matches!(str_enc, StrEnc::Binary) {
112-
if let TypeRefTypeHint::StringAsBytes(Some(len_arg_name)) = self.canonical.type_hint() {
113-
len_arg_name.as_ref()
114-
} else {
115-
"1024"
116-
}
117-
} else {
118-
"1024"
111+
let len = match self.canonical.type_hint() {
112+
TypeRefTypeHint::StringAsBytes(Some(len_arg_name)) if matches!(str_enc, StrEnc::Binary) => len_arg_name.as_ref(),
113+
TypeRefTypeHint::StringWithLen(len_arg_name) => len_arg_name.as_ref(),
114+
TypeRefTypeHint::None
115+
| TypeRefTypeHint::Nullable
116+
| TypeRefTypeHint::NullableSlice
117+
| TypeRefTypeHint::Slice
118+
| TypeRefTypeHint::LenForSlice(_, _)
119+
| TypeRefTypeHint::StringAsBytes(_)
120+
| TypeRefTypeHint::CharAsRustChar
121+
| TypeRefTypeHint::CharPtrSingleChar
122+
| TypeRefTypeHint::PrimitivePtrAsRaw
123+
| TypeRefTypeHint::AddArrayLength(_)
124+
| TypeRefTypeHint::BoxedAsRef(_, _, _)
125+
| TypeRefTypeHint::TraitClassConcrete
126+
| TypeRefTypeHint::ExplicitLifetime(_) => "1024",
119127
};
120128
format!("std::unique_ptr<char[]> {name}_out = std::make_unique<char[]>({len})")
121129
}

0 commit comments

Comments
 (0)