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

implement IntoIterator for Vec #337

Merged
merged 1 commit into from
Mar 17, 2022
Merged
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
6 changes: 4 additions & 2 deletions src/vec/vec-drain.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ impl<T> Drop for IntoIter<T> {
}
}

impl<T> Vec<T> {
pub fn into_iter(self) -> IntoIter<T> {
impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> IntoIter<T> {
unsafe {
let iter = RawValIter::new(&self);

Expand Down
30 changes: 17 additions & 13 deletions src/vec/vec-final.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,6 @@ impl<T> Vec<T> {
}
}

pub fn into_iter(self) -> IntoIter<T> {
unsafe {
let iter = RawValIter::new(&self);
let buf = ptr::read(&self.buf);
mem::forget(self);

IntoIter {
iter: iter,
_buf: buf,
}
}
}

pub fn drain(&mut self) -> Drain<T> {
unsafe {
let iter = RawValIter::new(&self);
Expand Down Expand Up @@ -208,6 +195,23 @@ impl<T> DerefMut for Vec<T> {
}
}

impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> IntoIter<T> {
unsafe {
let iter = RawValIter::new(&self);
let buf = ptr::read(&self.buf);
mem::forget(self);

IntoIter {
iter: iter,
_buf: buf,
}
}
}
}

struct RawValIter<T> {
start: *const T,
end: *const T,
Expand Down
6 changes: 4 additions & 2 deletions src/vec/vec-into-iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ And this is what we end up with for initialization:

<!-- ignore: simplified code -->
```rust,ignore
impl<T> Vec<T> {
pub fn into_iter(self) -> IntoIter<T> {
impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> IntoIter<T> {
// Can't destructure Vec since it's Drop
let ptr = self.ptr;
let cap = self.cap;
Expand Down
6 changes: 4 additions & 2 deletions src/vec/vec-raw.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ impl<T> Drop for IntoIter<T> {
}
}

impl<T> Vec<T> {
pub fn into_iter(self) -> IntoIter<T> {
impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> IntoIter<T> {
unsafe {
// need to use ptr::read to unsafely move the buf out since it's
// not Copy, and Vec implements Drop (so we can't destructure it).
Expand Down