Skip to content

Allow an Iterator to update a &mut [T], add IterReader #14568

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

Merged
merged 1 commit into from
Jun 5, 2014

Conversation

erickt
Copy link
Contributor

@erickt erickt commented May 31, 2014

This PR adds two features to make it possible to transform an Iterator<u8> into a Reader. The first patch adds a method to mutable slices that allows it to be updated with an Iterator<T> without paying for the bounds cost. The second adds a Iterator adaptor, IterReader, to provide that Reader interface.

I had two questions. First, are these named the right things? Second, should IterReader instead wrap an Iterator<Result<u8, E>>? This would allow you to IterReader::new(rdr.bytes()), which could be useful if you want to apply some iterator transformations on a reader while still exporting the Reader interface, but I'd expect there'd be a lot of overhead annotating each byte with an error result.

@huonw
Copy link
Member

huonw commented May 31, 2014

update could theoretically work with any Iterator<&mut T>, right?

}
}
idx
}
Copy link
Member

Choose a reason for hiding this comment

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

Would something like this suffice?

fn update<I: Iterator<T>>(self, iter: I) -> uint {
    let mut i = 0;
    for (slot, element) in self.mut_iter().zip(iter) {
        *slot = element;
        i += 1;
    }
    return i;
}

If the unsafe blocks stays, could you add a comment explaining why it is unsafe, what the benefits are, and why it is necessary to not rely on safe code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, that could work. I'll remove that patch from the PR.

@alexcrichton
Copy link
Member

The first patch does seem to deviate a bit from the compositionality of iterators. This would add an update method to vectors, but not to all the other containers that we have? I think that @huonw's suggestion of working with Iterator<&mut T> and Iterator<T> would indeed maintain composing this with other types as well.

bors added a commit that referenced this pull request Jun 5, 2014
This PR adds two features to make it possible to transform an `Iterator<u8>` into a `Reader`. The first patch adds a method to mutable slices that allows it to be updated with an `Iterator<T>` without paying for the bounds cost. The second adds a Iterator adaptor, `IterReader`, to provide that `Reader` interface.

I had two questions. First, are these named the right things? Second, should `IterReader` instead wrap an `Iterator<Result<u8, E>>`? This would allow you to `IterReader::new(rdr.bytes())`, which could be useful if you want to apply some iterator transformations on a reader while still exporting the Reader interface, but I'd expect there'd be a lot of overhead annotating each byte with an error result.
@bors bors closed this Jun 5, 2014
@bors bors merged commit 30a8bcb into rust-lang:master Jun 5, 2014
bors pushed a commit to rust-lang-ci/rust that referenced this pull request Apr 22, 2025
…st-lang#14568)

Fixes rust-lang/rust-clippy#14449, introduced in rust-lang#14314

changelog: [`map_entry`]: fix a false positive where the lint would
trigger without any insert calls present
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants