Add Vector::as_mut_slice()
MODIFY
binding-generator/src/writer/rust_native/tpl/vector/cpp_methods_copy_non_bool.tpl.cpp
binding-generator/src/writer/rust_native/tpl/vector/cpp_methods_copy_non_bool.tpl.cpp
-const {{inner_cpp_extern_return}}* cv_{{rust_localalias}}_data({{cpp_full}}* instance) {
+const {{inner_cpp_extern_return}}* cv_{{rust_localalias}}_data(const {{cpp_full}}* instance) {
+ return instance->data();
+}
+
+{{inner_cpp_extern_return}}* cv_{{rust_localalias}}_data_mut({{cpp_full}}* instance) {
return instance->data();
}

MODIFY
binding-generator/src/writer/rust_native/tpl/vector/rust_copy_non_bool.tpl.rs
binding-generator/src/writer/rust_native/tpl/vector/rust_copy_non_bool.tpl.rs
-vector_copy_non_bool! { {{inner_rust_full}}, {{rust_extern_const}},
- cv_{{rust_localalias}}_data
+vector_copy_non_bool! { {{inner_rust_full}}, {{rust_extern_const}}, {{rust_extern_mut}},
+ cv_{{rust_localalias}}_data, cv_{{rust_localalias}}_data_mut,
}

MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
@@ -5,6 +5,7 @@ use std::{
iter::FromIterator,
marker::PhantomData,
mem::ManuallyDrop,
+ slice,
};
pub use iter::{VectorIterator, VectorRefIterator};
@@ -145,7 +146,13 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
pub fn as_slice(&self) -> &[T] where Self: VectorExternCopyNonBool<T> {
unsafe {
- ::std::slice::from_raw_parts(self.extern_data(), self.len())
+ slice::from_raw_parts(self.extern_data(), self.len())
+ }
+ }
+
+ pub fn as_mut_slice(&mut self) -> &mut [T] where Self: VectorExternCopyNonBool<T> {
+ unsafe {
+ slice::from_raw_parts_mut(self.extern_data_mut(), self.len())
}
}

MODIFY
src/manual/core/vector/vector_extern.rs
src/manual/core/vector/vector_extern.rs
@@ -158,7 +158,9 @@ macro_rules! vector_copy_non_bool {
(
$type: ty,
$vector_extern_const: ty,
- $extern_data: ident
+ $vector_extern_mut: ty,
+ $extern_data_const: ident,
+ $extern_data_mut: ident $(,)?
) => {
impl $crate::manual::core::VectorElement for $type where $crate::manual::core::Vector<$type>: $crate::manual::core::VectorExtern<$type> {
#[inline(always)]
@@ -170,8 +172,14 @@ macro_rules! vector_copy_non_bool {
impl $crate::manual::core::VectorExternCopyNonBool<$type> for $crate::manual::core::Vector<$type> {
#[inline(always)]
unsafe fn extern_data(&self) -> *const $type {
- extern "C" { fn $extern_data(instance: $vector_extern_const) -> *const $type; }
- $extern_data(self.as_raw())
+ extern "C" { fn $extern_data_const(instance: $vector_extern_const) -> *const $type; }
+ $extern_data_const(self.as_raw())
+ }
+
+ #[inline(always)]
+ unsafe fn extern_data_mut(&mut self) -> *mut $type {
+ extern "C" { fn $extern_data_mut(instance: $vector_extern_mut) -> *mut $type; }
+ $extern_data_mut(self.as_raw_mut())
}
}
};

MODIFY
tests/vector.rs
tests/vector.rs
@@ -411,23 +411,58 @@ fn iter() -> Result<()> {
}
#[test]
-fn to_slice() -> Result<()> {
+fn as_slice() -> Result<()> {
{
- let vec = VectorOfu8::from_iter(vec![1, 2, 3, 4, 5]);
- assert_eq!(vec.as_slice(), &[1, 2, 3, 4, 5]);
+ let src = vec![1, 2, 3, 4, 5];
+ let mut vec = VectorOfu8::from_iter(src.clone());
+ assert_eq!(vec.as_slice(), src.as_slice());
+ assert_eq!(vec.as_mut_slice(), src.as_slice());
+ vec.as_mut_slice()[2] = 10;
+ vec.as_mut_slice()[4] = 15;
+ assert_eq!(vec.as_mut_slice(), &[1, 2, 10, 4, 15]);
}
{
let mut vec = VectorOfi32::new();
vec.push(5);
vec.push(10);
assert_eq!(vec.as_slice(), &[5, 10]);
+ vec.as_mut_slice().swap(0, 1);
+ assert_eq!(vec.as_slice(), &[10, 5]);
}
{
- let vec = VectorOfPoint2d::from_iter(vec![Point2d::new(10., 20.), Point2d::new(60.5, 90.3), Point2d::new(-40.333, 89.)]);
+ let src = vec![Point2d::new(10., 20.), Point2d::new(60.5, 90.3), Point2d::new(-40.333, 89.)];
+ let mut vec = VectorOfPoint2d::from_iter(src.clone());
let slice = vec.as_slice();
assert_eq!(20., slice[0].y);
assert_eq!(60.5, slice[1].x);
assert_eq!(Point2d::new(-40.333, 89.), slice[2]);
+ let slice = vec.as_mut_slice();
+ slice[0].x = 15.;
+ slice[0].y = 90.;
+ slice[1] = Point2d::new(91.5, 92.6);
+ slice.swap(0, 1);
+ assert_eq!(&[Point2d::new(91.5, 92.6), Point2d::new(15., 90.), Point2d::new(-40.333, 89.)], vec.as_slice());
+ }
+ {
+ let default = DMatch::default()?;
+ let src = vec![DMatch::default()?, DMatch::new(1, 2, 9.)?];
+ let mut vec = VectorOfDMatch::from_iter(src.clone());
+ let slice = vec.as_slice();
+ assert_eq!(slice[0].distance, default.distance);
+ assert_eq!(slice[0].img_idx, default.img_idx);
+ assert_eq!(slice[1], DMatch::new(1, 2, 9.)?);
+ let slice = vec.as_mut_slice();
+ slice[0].distance = 15.;
+ slice[0].img_idx = 8;
+ slice[1] = DMatch::new_index(1, 2, 3, 98.)?;
+ slice.swap(0, 1);
+ assert_eq!(
+ &[
+ DMatch::new_index(1, 2, 3, 98.)?,
+ DMatch::new_index(default.query_idx, default.train_idx, 8, 15.)?,
+ ],
+ vec.as_slice()
+ );
}
Ok(())
}

Add Vector::as_mut_slice()