Fix non-portable swap for Vector<bool>
MODIFY
binding-generator/src/writer/rust_native/tpl/vector/cpp.tpl.cpp
binding-generator/src/writer/rust_native/tpl/vector/cpp.tpl.cpp
@@ -32,7 +32,7 @@ extern "C" {
}
void cv_{{rust_localalias}}_swap({{cpp_full}}* instance, size_t index1, size_t index2) {
- std::swap((*instance)[index1], (*instance)[index2]);
+ {{swap_func}}((*instance)[index1], (*instance)[index2]);
}
void cv_{{rust_localalias}}_clear({{cpp_full}}* instance) {

MODIFY
binding-generator/src/writer/rust_native/vector.rs
binding-generator/src/writer/rust_native/vector.rs
@@ -96,6 +96,13 @@ impl RustNativeGeneratedElement for Vector<'_> {
let vec_type = self.type_ref();
let element_type = self.element_type();
+ let element_is_bool = element_type.is_bool();
+ // https://stackoverflow.com/questions/58660207/why-doesnt-stdswap-work-on-vectorbool-elements-under-clang-win
+ let swap_func = if element_is_bool {
+ "instance->swap"
+ } else {
+ "std::swap"
+ };
let mut inter_vars = hashmap! {
"rust_localalias" => self.rust_localalias(),
"cpp_full" => vec_type.cpp_full(),
@@ -105,6 +112,7 @@ impl RustNativeGeneratedElement for Vector<'_> {
"inner_cpp_func_call" => element_type.cpp_arg_func_call("val"),
"inner_cpp_extern_return" => element_type.cpp_extern_return(),
"inner_cpp_extern_return_wrapper" => element_type.cpp_extern_return_wrapper_full(),
+ "swap_func" => swap_func.into(),
};
let mut prefix = Cow::Borrowed("");
@@ -119,7 +127,7 @@ impl RustNativeGeneratedElement for Vector<'_> {
inter_vars.insert("prefix", prefix);
inter_vars.insert("suffix", suffix);
let mut exports = String::new();
- if element_type.is_copy() && !element_type.is_bool() {
+ if element_type.is_copy() && !element_is_bool {
exports += &METHODS_COPY_NON_BOOL_TPL.interpolate(&inter_vars);
}
if self.is_data_type(&element_type) {

MODIFY
tests/vector.rs
tests/vector.rs
@@ -305,6 +305,18 @@ fn swap() -> Result<()> {
assert_eq!("123", vec.get(2)?);
}
+ {
+ let mut vec = VectorOfbool::new();
+ vec.push(true);
+ vec.push(false);
+ vec.push(true);
+ vec.swap(0, 1)?;
+ assert_eq!(false, vec.get(0)?);
+ assert_eq!(true, vec.get(1)?);
+ vec.swap(1, 2)?;
+ assert_eq!(true, vec.get(2)?);
+ }
+
Ok(())
}

Fix non-portable swap for Vector<bool>