Move CxxVector mut indexing to Pin<&mut Self>
Fixes #625.
diff --git a/src/cxx_vector.rs b/src/cxx_vector.rs
index 4bfb7a2..593c73a 100644
--- a/src/cxx_vector.rs
+++ b/src/cxx_vector.rs
@@ -64,9 +64,9 @@
/// Returns a pinned mutable reference to an element at the given position,
/// or `None` if out of bounds.
- pub fn get_mut(&mut self, pos: usize) -> Option<Pin<&mut T>> {
+ pub fn get_mut(self: Pin<&mut Self>, pos: usize) -> Option<Pin<&mut T>> {
if pos < self.len() {
- Some(unsafe { self.get_unchecked_mut(pos) })
+ Some(unsafe { CxxVector::get_unchecked_mut(self, pos) })
} else {
None
}
@@ -99,8 +99,8 @@
/// [std::vector\<T\>::operator\[\]][operator_at].
///
/// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
- pub unsafe fn get_unchecked_mut(&mut self, pos: usize) -> Pin<&mut T> {
- let ptr = T::__get_unchecked(self, pos);
+ pub unsafe fn get_unchecked_mut(self: Pin<&mut Self>, pos: usize) -> Pin<&mut T> {
+ let ptr = T::__get_unchecked(Pin::get_unchecked_mut(self), pos);
Pin::new_unchecked(&mut *ptr)
}
@@ -131,7 +131,7 @@
}
/// Returns an iterator over elements of type `Pin<&mut T>`.
- pub fn iter_mut(&mut self) -> IterMut<T> {
+ pub fn iter_mut(self: Pin<&mut Self>) -> IterMut<T> {
IterMut { v: self, index: 0 }
}
}
@@ -189,11 +189,11 @@
///
/// The iterator element type is `Pin<&'a mut T>`.
pub struct IterMut<'a, T> {
- v: &'a mut CxxVector<T>,
+ v: Pin<&'a mut CxxVector<T>>,
index: usize,
}
-impl<'a, T> IntoIterator for &'a mut CxxVector<T>
+impl<'a, T> IntoIterator for Pin<&'a mut CxxVector<T>>
where
T: VectorElement,
{
@@ -212,7 +212,7 @@
type Item = Pin<&'a mut T>;
fn next(&mut self) -> Option<Self::Item> {
- let next = self.v.get_mut(self.index)?;
+ let next = CxxVector::get_mut(self.v.as_mut(), self.index)?;
self.index += 1;
// Extend lifetime to allow simultaneous holding of nonoverlapping
// elements, analogous to slice::split_first_mut.