Skip to content

Commit 8b136a6

Browse files
committed
Show Mat data dump in Debug implementation
1 parent 7aef8c0 commit 8b136a6

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

binding-generator/src/class/desc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::rc::Rc;
22

3-
use crate::element::ExcludeKind;
4-
53
use super::{Class, ClassKind, TemplateKind};
4+
use crate::element::ExcludeKind;
65

76
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
87
pub struct ClassDesc<'tu, 'ge> {
@@ -114,6 +113,11 @@ impl<'tu, 'ge> ClassDesc<'tu, 'ge> {
114113
Class::new_desc(Self::system("cv::String", "core"))
115114
}
116115

116+
/// `std::String`
117+
pub fn std_string() -> Class<'tu, 'ge> {
118+
Class::new_desc(Self::system("std::string", "core"))
119+
}
120+
117121
/// `cv::MatConstIterator`
118122
pub fn cv_matconstiterator() -> Class<'tu, 'ge> {
119123
Class::new_desc(Self::boxed("cv::MatConstIterator", "core"))

binding-generator/src/settings/func_inject.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ pub fn func_inject_factory(module: &str) -> Vec<FuncFactory> {
3838
TypeRefDesc::cv_size(),
3939
))
4040
},
41+
|| {
42+
Func::new_desc(
43+
FuncDesc::new(
44+
InstanceMethod(ClassDesc::cv_mat()),
45+
Const,
46+
Fallible,
47+
"getDataDump",
48+
"core",
49+
[],
50+
TypeRefDesc::std_string(),
51+
)
52+
.cpp_body(FuncCppBody::ManualCall(
53+
"std::string();\nstd::ostringstream oss;\noss << *instance;\nret = oss.str()".into(),
54+
))
55+
.doc_comment("Return the dump of the Mat's data"),
56+
)
57+
},
4158
|| {
4259
Func::new_desc(FuncDesc::new(
4360
InstanceMethod(ClassDesc::cv_umat()),

binding-generator/src/type_ref/desc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ impl<'tu, 'ge> TypeRefDesc<'tu, 'ge> {
211211
TypeRef::new_class(ClassDesc::cv_string())
212212
}
213213

214+
/// `std::string`
215+
pub fn std_string() -> TypeRef<'tu, 'ge> {
216+
TypeRef::new_class(ClassDesc::std_string())
217+
}
218+
214219
/// `std::vector<std::vector<double>>`
215220
pub fn vector_of_vector_of_double() -> TypeRef<'tu, 'ge> {
216221
TypeRef::new_vector(Vector::new_desc(VectorDesc::new(TypeRef::new_vector(Vector::new_desc(

src/manual/core/mat.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,25 @@ impl fmt::Debug for Mat {
788788
.field("total", &self.total())
789789
.field("is_continuous", &self.is_continuous())
790790
.field("is_submatrix", &self.is_submatrix())
791+
.field("data", &MatDataDumper(self))
791792
.finish()
792793
}
793794
}
794795

796+
struct MatDataDumper<'r>(&'r Mat);
797+
798+
impl fmt::Debug for MatDataDumper<'_> {
799+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
800+
const MAX_DUMP_SIZE: usize = 1000;
801+
802+
if self.0.total() <= MAX_DUMP_SIZE {
803+
f.write_str(&self.0.get_data_dump().map_err(|_| fmt::Error)?)
804+
} else {
805+
f.write_fmt(format_args!("<element count is higher than threshold: {MAX_DUMP_SIZE}>"))
806+
}
807+
}
808+
}
809+
795810
input_output_array! { UMat, from_umat, from_umat_mut }
796811
input_output_array_vector! { UMat, from_umat_vec, from_umat_vec_mut }
797812

0 commit comments

Comments
 (0)