Improve ConstnessOverride usability
MODIFY
binding-generator/src/type_ref.rs
binding-generator/src/type_ref.rs
@@ -396,7 +396,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
fn get_const_hint(&self, type_ref: &TypeRef) -> ConstnessOverride {
let constness = self.constness();
match constness {
- Constness::Const if type_ref.clang_constness().is_mut() => ConstnessOverride::Yes(constness),
+ Constness::Const if type_ref.clang_constness().is_mut() => ConstnessOverride::Const,
_ => ConstnessOverride::No,
}
}
@@ -850,7 +850,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
} else if self.as_string().is_some() {
out += "c_char";
} else {
- out += inner.rust_extern(ConstnessOverride::Yes(constness)).as_ref()
+ out += inner.rust_extern(ConstnessOverride::force(constness)).as_ref()
}
break 'typ out.into();
}
@@ -858,7 +858,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
break 'typ format!(
"*{cnst}[{typ}; {len}]",
cnst = self.constness().rust_qual(true),
- typ = elem.rust_extern(ConstnessOverride::Yes(constness)),
+ typ = elem.rust_extern(ConstnessOverride::force(constness)),
len = len,
)
.into();
@@ -869,7 +869,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
// argv is treated as array of output arguments and it doesn't seem to be meant this way
format!("*{cnst}c_char", cnst = elem.clang_constness().rust_qual(true)).into()
} else {
- elem.rust_extern(ConstnessOverride::Yes(constness))
+ elem.rust_extern(ConstnessOverride::force(constness))
};
break 'typ format!("*{cnst}{typ}", cnst = self.constness().rust_qual(true), typ = typ,).into();
}
@@ -1061,7 +1061,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
self.rust_arg_func_call(
"self",
if method_constness.is_const() {
- ConstnessOverride::Yes(Constness::Const)
+ ConstnessOverride::Const
} else {
ConstnessOverride::No
},
@@ -1178,7 +1178,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
}
pub fn rust_extern_self_func_decl(&self, method_constness: Constness) -> String {
- self.rust_extern_arg_func_decl("instance", ConstnessOverride::Yes(method_constness))
+ self.rust_extern_arg_func_decl("instance", ConstnessOverride::force(method_constness))
}
pub fn rust_extern_arg_func_decl(&self, name: &str, constness: ConstnessOverride) -> String {
@@ -1193,7 +1193,7 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
if self.as_string().is_some() {
"*mut c_void".into()
} else {
- self.rust_extern(ConstnessOverride::Yes(Constness::Mut))
+ self.rust_extern(ConstnessOverride::Mut)
}
}

MODIFY
binding-generator/src/type_ref/renderer.rs
binding-generator/src/type_ref/renderer.rs
@@ -175,14 +175,23 @@ impl Constness {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConstnessOverride {
No,
- Yes(Constness),
+ Const,
+ Mut,
}
impl ConstnessOverride {
+ pub fn force(constness: Constness) -> Self {
+ match constness {
+ Constness::Const => Self::Const,
+ Constness::Mut => Self::Mut,
+ }
+ }
+
pub fn with(self, constness: Constness) -> Constness {
match self {
ConstnessOverride::No => constness,
- ConstnessOverride::Yes(cnst) => cnst,
+ ConstnessOverride::Const => Constness::Const,
+ ConstnessOverride::Mut => Constness::Mut,
}
}
}

MODIFY
binding-generator/src/writer/rust_native/abstract_ref_wrapper.rs
binding-generator/src/writer/rust_native/abstract_ref_wrapper.rs
@@ -26,8 +26,8 @@ impl RustNativeGeneratedElement for AbstractRefWrapper<'_, '_> {
"rust_full" => cls.rust_trait_name(NameStyle::ref_(), Constness::Mut),
"rust_const_full" => cls.rust_trait_name(NameStyle::ref_(), Constness::Const),
"rust_local" => type_ref.rust_name(NameStyle::decl()),
- "rust_extern_mut" => type_ref.rust_extern(ConstnessOverride::Yes(Constness::Mut)),
- "rust_extern_const" => type_ref.rust_extern(ConstnessOverride::Yes(Constness::Const)),
+ "rust_extern_mut" => type_ref.rust_extern(ConstnessOverride::Mut),
+ "rust_extern_const" => type_ref.rust_extern(ConstnessOverride::Const),
})
}
}

MODIFY
binding-generator/src/writer/rust_native/class.rs
binding-generator/src/writer/rust_native/class.rs
@@ -125,8 +125,8 @@ fn gen_rust_class(c: &Class, opencv_version: &str) -> String {
"rust_trait_local" => c.rust_trait_name(NameStyle::decl(), Constness::Mut),
"rust_trait_local_const" => c.rust_trait_name(NameStyle::decl(), Constness::Const),
"rust_local" => type_ref.rust_name(NameStyle::decl()),
- "rust_extern_const" => type_ref.rust_extern(ConstnessOverride::Yes(Constness::Const)),
- "rust_extern_mut" => type_ref.rust_extern(ConstnessOverride::Yes(Constness::Mut)),
+ "rust_extern_const" => type_ref.rust_extern(ConstnessOverride::Const),
+ "rust_extern_mut" => type_ref.rust_extern(ConstnessOverride::Mut),
"trait_bases_const" => trait_bases_const.into(),
"trait_bases_mut" => trait_bases_mut.into(),
"trait_const_methods" => trait_const_methods.into(),
@@ -202,8 +202,8 @@ fn gen_rust_class(c: &Class, opencv_version: &str) -> String {
"base_const_rust_full" => base.rust_trait_name(NameStyle::ref_(), Constness::Const),
"rust_local" => type_ref.rust_name(NameStyle::decl()),
"base_rust_local" => base_type_ref.rust_name(NameStyle::decl()),
- "base_rust_extern_const" => base_type_ref.rust_extern(ConstnessOverride::Yes(Constness::Const)),
- "base_rust_extern_mut" => base_type_ref.rust_extern(ConstnessOverride::Yes(Constness::Mut)),
+ "base_rust_extern_const" => base_type_ref.rust_extern(ConstnessOverride::Const),
+ "base_rust_extern_mut" => base_type_ref.rust_extern(ConstnessOverride::Mut),
})
})
.collect::<Vec<_>>();
@@ -290,8 +290,8 @@ fn gen_rust_class(c: &Class, opencv_version: &str) -> String {
"debug" => get_debug(c).into(),
"rust_local" => rust_local.clone(),
"rust_full" => c.rust_name(NameStyle::ref_()),
- "rust_extern_const" => type_ref.rust_extern(ConstnessOverride::Yes(Constness::Const)),
- "rust_extern_mut" => type_ref.rust_extern(ConstnessOverride::Yes(Constness::Mut)),
+ "rust_extern_const" => type_ref.rust_extern(ConstnessOverride::Const),
+ "rust_extern_mut" => type_ref.rust_extern(ConstnessOverride::Mut),
"fields" => fields.join("").into(),
"bases" => bases.join("").into(),
"impl" => IMPL_TPL.interpolate(&hashmap! {

MODIFY
binding-generator/src/writer/rust_native/func.rs
binding-generator/src/writer/rust_native/func.rs
@@ -4,7 +4,7 @@ use maplit::hashmap;
use once_cell::sync::Lazy;
use crate::func::Kind;
-use crate::type_ref::{Constness, ConstnessOverride, CppNameStyle, Dir, FishStyle, NameStyle, StrEnc, StrType};
+use crate::type_ref::{ConstnessOverride, CppNameStyle, Dir, FishStyle, NameStyle, StrEnc, StrType};
use crate::{
get_debug, settings, Class, CompiledInterpolation, DefaultElement, Element, Field, Func, FunctionTypeHint, IteratorExt,
StrExt, StringExt, TypeRef,
@@ -390,9 +390,9 @@ impl RustNativeGeneratedElement for Func<'_, '_> {
return_type.cpp_extern_return_wrapper_full(ConstnessOverride::No)
};
let mut_ret_wrapper_full = if is_infallible {
- return_type.cpp_extern_return(ConstnessOverride::Yes(Constness::Mut))
+ return_type.cpp_extern_return(ConstnessOverride::Mut)
} else {
- return_type.cpp_extern_return_wrapper_full(ConstnessOverride::Yes(Constness::Mut))
+ return_type.cpp_extern_return_wrapper_full(ConstnessOverride::Mut)
};
if !naked_return {
decl_args.push(format!("{}* ocvrs_return", mut_ret_wrapper_full));

MODIFY
binding-generator/src/writer/rust_native/smart_ptr.rs
binding-generator/src/writer/rust_native/smart_ptr.rs
@@ -29,8 +29,8 @@ impl RustNativeGeneratedElement for SmartPtr<'_, '_> {
let mut inter_vars = hashmap! {
"rust_localalias" => self.rust_localalias(),
"rust_full" => self.rust_name(NameStyle::ref_()),
- "rust_extern_const" => type_ref.rust_extern(ConstnessOverride::Yes(Constness::Const)),
- "rust_extern_mut" => type_ref.rust_extern(ConstnessOverride::Yes(Constness::Mut)),
+ "rust_extern_const" => type_ref.rust_extern(ConstnessOverride::Const),
+ "rust_extern_mut" => type_ref.rust_extern(ConstnessOverride::Mut),
"inner_rust_full" => pointee_type.rust_name(NameStyle::ref_()),
};
@@ -94,14 +94,14 @@ impl RustNativeGeneratedElement for SmartPtr<'_, '_> {
let pointee_type = self.pointee();
let mut inner_cpp_extern = pointee_type.cpp_extern_return(ConstnessOverride::No);
- let mut inner_cpp_extern_const = pointee_type.cpp_extern_return(ConstnessOverride::Yes(Constness::Const));
+ let mut inner_cpp_extern_const = pointee_type.cpp_extern_return(ConstnessOverride::Const);
if !pointee_type.is_by_ptr() {
inner_cpp_extern.to_mut().push('*');
inner_cpp_extern_const.to_mut().push('*');
}
let mut const_renderer = type_ref::CppRenderer::new(CppNameStyle::Reference, "", true);
- const_renderer.constness_override = ConstnessOverride::Yes(Constness::Const);
+ const_renderer.constness_override = ConstnessOverride::Const;
let cpp_ref_const = type_ref.render(const_renderer);
let mut inter_vars = hashmap! {

MODIFY
binding-generator/src/writer/rust_native/vector.rs
binding-generator/src/writer/rust_native/vector.rs
@@ -3,7 +3,7 @@ use std::borrow::Cow;
use maplit::hashmap;
use once_cell::sync::Lazy;
-use crate::type_ref::{Constness, ConstnessOverride, CppNameStyle, NameStyle};
+use crate::type_ref::{ConstnessOverride, CppNameStyle, NameStyle};
use crate::{settings, CompiledInterpolation, Element, StrExt, Vector};
use super::RustNativeGeneratedElement;
@@ -38,8 +38,8 @@ impl RustNativeGeneratedElement for Vector<'_, '_> {
let mut inter_vars = hashmap! {
"rust_localalias" => self.rust_localalias(),
"rust_full" => self.rust_name(NameStyle::ref_()),
- "rust_extern_const" => vec_type.rust_extern(ConstnessOverride::Yes(Constness::Const)),
- "rust_extern_mut" => vec_type.rust_extern(ConstnessOverride::Yes(Constness::Mut)),
+ "rust_extern_const" => vec_type.rust_extern(ConstnessOverride::Const),
+ "rust_extern_mut" => vec_type.rust_extern(ConstnessOverride::Mut),
"inner_rust_full" => element_type.rust_name(NameStyle::ref_()),
"inner_rust_extern_return" => element_type.rust_extern_return(),
};

Improve ConstnessOverride usability