add Vector::swap()
MODIFY
gen_rust.py
gen_rust.py
@@ -2020,6 +2020,21 @@ class VectorTypeInfo(TypeInfo):
Ok(())
}
+ /// Swaps values of 2 elements
+ #[inline]
+ fn swap(&mut self, index1: size_t, index2: size_t) -> crate::Result<()> {
+ let len = self.len();
+ crate::templ::Vector::<Storage=Self::Storage, Arg=Self::Arg>::index_check(index1, len)?;
+ crate::templ::Vector::<Storage=Self::Storage, Arg=Self::Arg>::index_check(index2, len)?;
+ if index1 != index2 {
+ let vec = self.as_raw_${rust_local}();
+ cpp!(unsafe [vec as "${cpptype}*", index1 as "size_t", index2 as "size_t"] {
+ swap((*vec)[index1], (*vec)[index2]);
+ });
+ }
+ Ok(())
+ }
+
#[inline]
fn clear(&mut self) {
let vec = self.as_raw_${rust_local}();

MODIFY
src/templ/vector.rs
src/templ/vector.rs
@@ -52,6 +52,9 @@ pub trait Vector<'i> {
/// Remove the element at the specified `index`
fn remove(&mut self, index: size_t) -> crate::Result<()>;
+ /// Swaps 2 elements in the Vector
+ fn swap(&mut self, index1: size_t, index2: size_t) -> crate::Result<()>;
+
/// Get element at the specified `index`
fn get(&self, index: size_t) -> crate::Result<Self::Storage>;

MODIFY
tests/vector.rs
tests/vector.rs
@@ -186,6 +186,37 @@ fn remove() -> Result<()> {
Ok(())
}
+#[test]
+fn swap() -> Result<()> {
+ {
+ let mut vec = VectorOfint::from_iter(vec![1, 2, 3]);
+ vec.swap(0, 0)?;
+ vec.swap(0, 1)?;
+ assert_eq!(2, vec.get(0)?);
+ assert_eq!(1, vec.get(1)?);
+
+ vec.swap(2, 0)?;
+ assert_eq!(3, vec.get(0)?);
+ assert_eq!(1, vec.get(1)?);
+
+ assert_matches!(vec.swap(0, 4), Err(Error { code: core::StsOutOfRange, .. }));
+ assert_matches!(vec.swap(6, 1), Err(Error { code: core::StsOutOfRange, .. }));
+ }
+
+ {
+ let mut vec = VectorOfString::new();
+ vec.push("123");
+ vec.push("456");
+ vec.push("789");
+ vec.swap(0, 2)?;
+ assert_eq!("789", vec.get(0)?);
+ assert_eq!("456", vec.get(1)?);
+ assert_eq!("123", vec.get(2)?);
+ }
+
+ Ok(())
+}
+
#[test]
fn out_of_bounds() -> Result<()> {
let mut vec = VectorOfdouble::new();

add Vector::swap()