Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: teach BufferMut into_iter #1983

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vortex-buffer/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl Buf for ByteBuffer {
}
}

/// Owned iterator over a `Buffer<T>`.
/// Owned iterator over a [`Buffer`].
pub struct BufferIterator<T> {
buffer: Buffer<T>,
index: usize,
Expand Down
50 changes: 50 additions & 0 deletions vortex-buffer/src/buffer_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,56 @@ impl<T> FromIterator<T> for BufferMut<T> {
}
}

/// Owned iterator over a [`BufferMut`].
///
/// Examples
/// --------
///
/// ```
/// use vortex_buffer::buffer_mut;
///
/// let mut a = buffer_mut![1u16, 2, 3];
/// let b = buffer_mut![4u16, 5, 6];
/// a.extend(b);
/// assert_eq!(a.len(), 6);
/// assert_eq!(a[3..6], [4, 5, 6]);
/// ```
pub struct BufferMutIterator<T> {
buffer: BufferMut<T>,
index: usize,
}

impl<T: Copy> Iterator for BufferMutIterator<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
if self.index == self.buffer.len() {
None
} else {
let value = self.buffer[self.index];
self.index += 1;
Some(value)
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.buffer.len() - self.index;
(remaining, Some(remaining))
}
}

impl<T: Copy> IntoIterator for BufferMut<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the into_iterator to actually consume the buffer instead of copying values

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise you can make &T as a return value and the caller can copy

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this, doesn't deref and derefmut give you &T and &mut T iterators already?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're correct that it would give you that but I didn't realize deref impls exist before making this comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want this to compile:

    let buf: BufferMut<T> = values.into_buffer_mut::<T>();
    let mut new_buf: BufferMut<T> = BufferMut::<T>::with_capacity(buf.len() + 1);
    new_buf.push(T::zero());
    new_buf.extend(buf);

But it doesn't because buf does not implement IntoIterator<Item = T>. I somehow want to convince Rust to just memcpy the old buffer into the new buffer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check out how this is handled for Vec - spoiler alert, you want to be able to introspect passed iterator and extract the buffer back from it.

Copy link
Member Author

@danking danking Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can punt on all of this and use a for loop because I need to increment each code by one anyway. Erm getting confused between codes and values.

EDIT: Let me look at the Vec implementation and get back to y'all.

type Item = T;
type IntoIter = BufferMutIterator<T>;

fn into_iter(self) -> Self::IntoIter {
BufferMutIterator {
buffer: self,
index: 0,
}
}
}

impl Buf for ByteBufferMut {
fn remaining(&self) -> usize {
self.len()
Expand Down
Loading