Skip to main content

opencv_binding_generator/
lib.rs

1// todo support converting pointer + size to slice of Mat and other similar objects
2// todo add support for arrays in dnn::DictValue
3// todo allow ergonomically combining of enum variants with |
4// todo almost everything from the manual module must be connected to the binding generator, not the main crate
5// todo check that FN_FaceDetector works at all (receiving InputArray, passing as callback)
6// todo public static properties like opencv2/core/base.hpp:384 Hamming::normType
7// todo test returning reference to array like cv_MatStep_buf
8// todo, allow extension of simple classes for e.g. Elliptic_KeyPoint
9// fixme vector<Mat*> get's interpreted as Vector<Mat> which should be wrong (e.g. Layer::forward and Layer::apply_halide_scheduler)
10// fixme MatConstIterator::m return Mat**, is it handled correctly?
11// fixme VectorOfMat::get allows mutation
12// fixme MatSize looks like just a pointer inside a Mat so it should take a lifetime
13
14// copy-pasted form python generator (may be obsolete):
15// fixme returning MatAllocator (trait) by reference is bad, check knearestneighbour
16
17#![expect(clippy::nonminimal_bool)] // pattern `!type_ref.as_vector().is_some()` used for more clarity
18
19use std::borrow::Cow;
20use std::fs::File;
21use std::io::{BufRead, Read, Seek, SeekFrom};
22use std::ops::ControlFlow;
23
24pub use abstract_ref_wrapper::AbstractRefWrapper;
25use clang::Entity;
26pub use class::Class;
27pub use constant::Const;
28pub use element::{DefaultElement, Element, EntityElement, is_opencv_path, opencv_module_from_path};
29pub use entity::EntityExt;
30#[expect(unused)]
31use entity::dbg_clang_entity;
32pub use enumeration::Enum;
33use field::Field;
34pub use func::{Func, FuncTypeHint, Pred, UsageTracker};
35pub use generator::{GeneratedType, Generator, GeneratorVisitor, OpenCvWalker};
36pub use generator_env::{ClassKindOverride, ExportConfig, GeneratorEnv};
37pub use iterator_ext::IteratorExt;
38pub use map_borrowed::CowMapBorrowedExt;
39use memoize::{MemoizeMap, MemoizeMapExt};
40use smart_ptr::SmartPtr;
41pub use string_ext::{CompiledInterpolation, StrExt, StringExt};
42pub use supported_module::SupportedModule;
43use tuple::Tuple;
44use type_ref::TypeRef;
45#[expect(unused)]
46use type_ref::dbg_clang_type;
47pub use type_ref::{Constness, CppNameStyle, NameStyle};
48pub use typedef::Typedef;
49use vector::Vector;
50pub use walker::{EntityWalkerExt, EntityWalkerVisitor};
51
52use crate::debug::NameDebug;
53
54mod abstract_ref_wrapper;
55mod class;
56pub mod comment;
57mod constant;
58mod debug;
59mod element;
60mod entity;
61mod enumeration;
62mod field;
63mod func;
64mod function;
65mod generator;
66mod generator_env;
67mod iterator_ext;
68mod map_borrowed;
69mod memoize;
70mod name_pool;
71mod renderer;
72pub mod settings;
73mod smart_ptr;
74mod string_ext;
75mod supported_module;
76#[cfg(test)]
77mod test;
78mod tuple;
79mod type_ref;
80mod typedef;
81mod vector;
82pub mod version;
83mod walker;
84pub mod writer;
85
86fn get_definition_text(entity: Entity) -> String {
87	if let Some(range) = entity.get_range() {
88		let loc = range.get_start().get_spelling_location();
89		let start = loc.offset;
90		let end = range.get_end().get_spelling_location().offset;
91		let len = usize::try_from(end - start).expect("Definition span is too large");
92		let mut def_bytes = vec![0; len];
93		let mut source = File::open(loc.file.expect("Can't get file").get_path()).expect("Can't open source file");
94		source.seek(SeekFrom::Start(u64::from(start))).expect("Cannot seek");
95		source.read_exact(&mut def_bytes).expect("Can't read definition");
96		String::from_utf8(def_bytes).expect("Can't parse definition")
97	} else {
98		unreachable!("Can't get entity range: {:#?}", entity)
99	}
100}
101
102fn reserved_rename(val: Cow<str>) -> Cow<str> {
103	settings::RESERVED_RENAME.get(val.as_ref()).map_or(val, |&v| v.into())
104}
105
106#[inline(always)]
107pub fn line_reader(mut b: impl BufRead, mut cb: impl FnMut(&str) -> ControlFlow<()>) {
108	let mut line = String::with_capacity(256);
109	while let Ok(bytes_read) = b.read_line(&mut line) {
110		if bytes_read == 0 || cb(&line).is_break() {
111			break;
112		}
113		line.clear();
114	}
115}