Skip to content

Commit ab1ac88

Browse files
committed
Add CxxVector::as_mut_slice
1 parent 5573e52 commit ab1ac88

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/cxx_vector.rs

+15
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ where
125125
}
126126
}
127127

128+
/// Returns a slice to the underlying contiguous array of elements by
129+
/// mutable reference.
130+
pub fn as_mut_slice(self: Pin<&mut Self>) -> &mut [T]
131+
where
132+
T: ExternType<Kind = Trivial>,
133+
{
134+
let len = self.len();
135+
if len == 0 {
136+
&mut []
137+
} else {
138+
let ptr = unsafe { T::__get_unchecked(self.get_unchecked_mut(), 0) };
139+
unsafe { slice::from_raw_parts_mut(ptr, len) }
140+
}
141+
}
142+
128143
/// Returns an iterator over elements of type `&T`.
129144
pub fn iter(&self) -> Iter<T> {
130145
Iter { v: self, index: 0 }

0 commit comments

Comments
 (0)