Rework error handing in infallible functions
MODIFY
binding-generator/src/type_ref.rs
binding-generator/src/type_ref.rs
@@ -1069,7 +1069,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
}
}
- pub fn rust_return_map(&self, is_safe_context: bool, is_static_func: bool, error_handling: &str) -> Cow<str> {
+ pub fn rust_return_map(&self, is_safe_context: bool, is_static_func: bool, is_infallible: bool) -> Cow<str> {
let unsafety_call = if is_safe_context { "unsafe " } else { "" };
if self.as_string().is_some() || self.is_by_ptr() {
format!(
@@ -1079,8 +1079,13 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
).into()
} else if self.as_pointer().map_or(false, |i| !i.is_void()) || self.as_fixed_array().is_some() {
let ptr_call = if self.constness().is_const() { "ref" } else { "mut" };
+ let error_handling = if is_infallible {
+ ".expect(\"Function returned null pointer\")"
+ } else {
+ ".ok_or_else(|| Error::new(core::StsNullPtr, \"Function returned null pointer\".to_string()))?"
+ };
format!(
- "let ret = {unsafety_call}{{ ret.as_{ptr_call}() }}.ok_or_else(|| Error::new(core::StsNullPtr, \"Function returned Null pointer\".to_string())){error_handling};",
+ "let ret = {unsafety_call}{{ ret.as_{ptr_call}() }}{error_handling};",
unsafety_call=unsafety_call,
ptr_call=ptr_call,
error_handling=error_handling,

MODIFY
binding-generator/src/writer/rust_native/func.rs
binding-generator/src/writer/rust_native/func.rs
@@ -114,17 +114,12 @@ fn gen_rust_with_name(f: &Func, name: &str, opencv_version: &str) -> String {
let call_args = call_args.join(", ");
let forward_args = forward_args.join(", ");
let post_call_args = post_call_args.join("\n");
- let error_handling = if is_infallible {
- format!(".expect(\"Infallible function failed: {name}\")", name=name).into()
- } else {
- Cow::Borrowed("?")
- };
let ret_convert = if is_infallible {
- Cow::Borrowed("")
+ ""
} else {
- format!(".into_result(){}", error_handling).into()
+ ".into_result()?"
};
- let ret_map = return_type.rust_return_map(is_safe, is_static_func, &error_handling);
+ let ret_map = return_type.rust_return_map(is_safe, is_static_func, is_infallible);
let mut attributes = String::new();
if let Some(attrs) = settings::FUNC_CFG_ATTR.get(identifier.as_ref()) {
attributes = format!("#[cfg({})]", attrs.0);
@@ -150,7 +145,7 @@ fn gen_rust_with_name(f: &Func, name: &str, opencv_version: &str) -> String {
"identifier" => identifier.as_ref(),
"call_args" => &call_args,
"forward_args" => &forward_args,
- "ret_convert" => &ret_convert,
+ "ret_convert" => ret_convert,
"ret_map" => ret_map.as_ref(),
"post_call_args" => &post_call_args,
})

MODIFY
src/manual/core/mat.rs
src/manual/core/mat.rs
@@ -314,7 +314,7 @@ pub trait MatTraitConstManual: MatTraitConst {
.and_then(|_| {
let data = self.data();
if data.is_null() {
- Err(Error::new(core::StsNullPtr, "Function returned Null pointer".to_string()))
+ Err(Error::new(core::StsNullPtr, "Function returned null pointer".to_string()))
} else {
Ok(unsafe { slice::from_raw_parts(data, self.total()? * self.elem_size()?) })
}
@@ -334,7 +334,7 @@ pub trait MatTraitConstManual: MatTraitConst {
unsafe fn data_typed_unchecked<T: DataType>(&self) -> Result<&[T]> {
let data = self.data();
if data.is_null() {
- Err(Error::new(core::StsNullPtr, "Function returned Null pointer".to_string()))
+ Err(Error::new(core::StsNullPtr, "Function returned null pointer".to_string()))
} else {
Ok(slice::from_raw_parts(data as *const T, self.total()?))
}

Rework error handing in infallible functions