add support for generating unsafe functions
MODIFY
gen_rust.py
gen_rust.py
@@ -518,6 +518,13 @@ func_ignore_list = (
"cv.BOWImgDescriptorExtractor.getVocabulary",
)
+func_unsafe_list = {
+ # allocates uninitialized memory
+ "cv_Mat_Mat_int_int_int",
+ "cv_Mat_Mat_Size_int",
+ "cv_Mat_Mat_VectorOfint_int",
+}
+
# regular expressions to ignore matching constant names
const_ignore_list = (
"CV_EXPORTS_W", "CV_MAKE_TYPE",
@@ -839,8 +846,8 @@ class FuncInfo(GeneralInfo):
"cpp_doc_arg": template("""// Arg ${repr}${ptr} ${type} = ${defval}${ignored}\n"""),
"rust_safe": template("""
- ${doc_comment}${visibility}fn ${r_name}${generic_decl}(${args}) -> Result<$rv_rust_full> {${pre_call_args}
- unsafe { sys::${identifier}(${call_args}) }.into_result()${rv}
+ ${doc_comment}${visibility}${unsafety_decl}fn ${r_name}${generic_decl}(${args}) -> Result<$rv_rust_full> {${pre_call_args}
+ ${unsafety_call}{ sys::${identifier}(${call_args}) }.into_result()${rv}
}
"""),
@@ -938,6 +945,7 @@ class FuncInfo(GeneralInfo):
self.struct_attrname = decl[6] if self.fake_attrgetter else None
self.cname = self.cppname = self.name
+ self.is_safe = self.identifier not in func_unsafe_list
if self.name.startswith("~"):
logging.info("ignore destructor %s %s in %s"%(self.kind, self.name, self.ci))
@@ -1150,6 +1158,8 @@ class FuncInfo(GeneralInfo):
template_vars = combine_dicts(self.__dict__, {
"doc_comment": doc_comment,
"rv_rust_full": self.rv_type().rust_full,
+ "unsafety_decl": "" if self.is_safe else "unsafe ",
+ "unsafety_call": "unsafe " if self.is_safe else "",
"visibility": pub,
"generic_decl": "<{}>".format(", ".join(generic_decls)) if len(generic_decls) >= 1 else "",
"args": ", ".join(args),

MODIFY
src/hub/manual/core/mat.rs
src/hub/manual/core/mat.rs
@@ -143,7 +143,7 @@ impl Mat {
} else {
0
};
- let mut out = Mat::new_rows_cols(row_count, col_count, T::typ())?;
+ let mut out = unsafe { Mat::new_rows_cols(row_count, col_count, T::typ()) }?;
for (row_n, row) in s.into_iter().enumerate() {
let trg = out.at_row_mut(row_n as _)?;
let src = row.as_ref();

MODIFY
tests/mat.rs
tests/mat.rs
@@ -4,8 +4,9 @@ use opencv::types::VectorOfMat;
#[test]
fn mat_for_rows_and_cols() {
let typ = core::CV_8UC3;
- let mat = Mat::new_rows_cols(400, 300, typ).unwrap();
+ let mat = unsafe { Mat::new_rows_cols(400, 300, typ) }.unwrap();
let size = mat.size().unwrap();
+ assert_eq!(mat.typ().unwrap(), typ);
assert_eq!(
core::Size {
width: 300,
@@ -19,7 +20,7 @@ fn mat_for_rows_and_cols() {
#[test]
fn mat_at() {
- let mut mat = Mat::new_rows_cols(100, 100, core::CV_32F).unwrap();
+ let mut mat = Mat::new_rows_cols_with_default(100, 100, core::CV_32F, Scalar::all(0.)).unwrap();
assert_eq!(*mat.at_2d::<f32>(0, 0).unwrap(), 0.);
*mat.at_2d_mut::<f32>(0, 0).unwrap() = 1.;
assert_eq!(*mat.at_2d::<f32>(0, 0).unwrap(), 1.);

MODIFY
tests/types.rs
tests/types.rs
@@ -19,7 +19,7 @@ fn vec() {
let mut m = Mat::new_rows_cols_with_default(10, 10, core::CV_8UC3, core::Scalar::default()).unwrap();
let mut ps = types::VectorOfMat::new();
assert_eq!(ps.len(), 0);
- let mut p1 = Mat::new_rows_cols(3, 2, core::CV_32S).unwrap();
+ let mut p1 = unsafe { Mat::new_rows_cols(3, 2, core::CV_32S) }.unwrap();
p1.at_row_mut::<i32>(0).unwrap().copy_from_slice(&[0, 0]);
p1.at_row_mut::<i32>(1).unwrap().copy_from_slice(&[0, 9]);
p1.at_row_mut::<i32>(2).unwrap().copy_from_slice(&[9, 9]);

add support for generating unsafe functions