Skip to content

Commit e3a928f

Browse files
committed
Add drain_iter_from to reuse RawIter for RawDrain
1 parent d85f2bc commit e3a928f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/raw/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,23 @@ impl<T> RawTable<T> {
10071007
/// on the `RawDrain`, we have to make the `drain` method unsafe.
10081008
#[cfg_attr(feature = "inline-more", inline)]
10091009
pub unsafe fn drain(&mut self) -> RawDrain<'_, T> {
1010+
let iter = self.iter();
1011+
self.drain_iter_from(iter)
1012+
}
1013+
1014+
/// Returns an iterator which removes all elements from the table without
1015+
/// freeing the memory. It is up to the caller to ensure that the `RawTable`
1016+
/// outlives the `RawDrain`. Because we cannot make the `next` method unsafe
1017+
/// on the `RawDrain`, we have to make the `drain` method unsafe.
1018+
///
1019+
/// Iteration starts at the provided iterator's current location.
1020+
///
1021+
/// This method panics if the given iterator does not cover all items remaining in the table.
1022+
#[cfg_attr(feature = "inline-more", inline)]
1023+
pub unsafe fn drain_iter_from(&mut self, iter: RawIter<T>) -> RawDrain<'_, T> {
1024+
debug_assert_eq!(iter.len(), self.len());
10101025
RawDrain {
1011-
iter: self.iter(),
1026+
iter,
10121027
table: ManuallyDrop::new(mem::replace(self, Self::new())),
10131028
orig_table: NonNull::from(self),
10141029
marker: PhantomData,

0 commit comments

Comments
 (0)