|
62 | 62 | self.len() == 0
|
63 | 63 | }
|
64 | 64 |
|
| 65 | + /// Increase the capacity of the vector. |
| 66 | + /// |
| 67 | + /// Matches the behavior of C++ [std::vector\<T\>::reserve][reserve]. |
| 68 | + /// |
| 69 | + /// [reserve]: https://en.cppreference.com/w/cpp/container/vector/reserve |
| 70 | + pub fn reserve(self: Pin<&mut Self>, capacity: usize) { |
| 71 | + unsafe { T::__vector_reserve(self.get_unchecked_mut(), capacity) } |
| 72 | + } |
| 73 | + |
| 74 | + /// Returns the number of elements that the vector has currently allocated space for. |
| 75 | + /// |
| 76 | + /// Matches the behavior of C++ [std::vector\<T\>::capacity][capacity]. |
| 77 | + /// |
| 78 | + /// [capacity]: https://en.cppreference.com/w/cpp/container/vector/capacity |
| 79 | + pub fn capacity(&self) -> usize { |
| 80 | + T::__vector_capacity(self) |
| 81 | + } |
| 82 | + |
65 | 83 | /// Returns a reference to an element at the given position, or `None` if
|
66 | 84 | /// out of bounds.
|
67 | 85 | pub fn get(&self, pos: usize) -> Option<&T> {
|
@@ -346,6 +364,10 @@ pub unsafe trait VectorElement: Sized {
|
346 | 364 | #[doc(hidden)]
|
347 | 365 | fn __vector_size(v: &CxxVector<Self>) -> usize;
|
348 | 366 | #[doc(hidden)]
|
| 367 | + unsafe fn __vector_reserve(v: *mut CxxVector<Self>, capacity: usize); |
| 368 | + #[doc(hidden)] |
| 369 | + fn __vector_capacity(v: &CxxVector<Self>) -> usize; |
| 370 | + #[doc(hidden)] |
349 | 371 | unsafe fn __get_unchecked(v: *mut CxxVector<Self>, pos: usize) -> *mut Self;
|
350 | 372 | #[doc(hidden)]
|
351 | 373 | unsafe fn __push_back(v: Pin<&mut CxxVector<Self>>, value: &mut ManuallyDrop<Self>) {
|
@@ -418,6 +440,20 @@ macro_rules! impl_vector_element {
|
418 | 440 | }
|
419 | 441 | unsafe { __vector_size(v) }
|
420 | 442 | }
|
| 443 | + unsafe fn __vector_reserve(v: *mut CxxVector<$ty>, capacity: usize) { |
| 444 | + extern "C" { |
| 445 | + #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$reserve")] |
| 446 | + fn __vector_reserve(_: *mut CxxVector<$ty>, _: usize); |
| 447 | + } |
| 448 | + unsafe { __vector_reserve(v, capacity) } |
| 449 | + } |
| 450 | + fn __vector_capacity(v: &CxxVector<$ty>) -> usize { |
| 451 | + extern "C" { |
| 452 | + #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$capacity")] |
| 453 | + fn __vector_capacity(_: &CxxVector<$ty>) -> usize; |
| 454 | + } |
| 455 | + unsafe { __vector_capacity(v) } |
| 456 | + } |
421 | 457 | unsafe fn __get_unchecked(v: *mut CxxVector<$ty>, pos: usize) -> *mut $ty {
|
422 | 458 | extern "C" {
|
423 | 459 | #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$get_unchecked")]
|
|
0 commit comments