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

[Feature request] Add [T]::windows_mut #23783

Closed
SimonSapin opened this issue Mar 27, 2015 · 7 comments
Closed

[Feature request] Add [T]::windows_mut #23783

SimonSapin opened this issue Mar 27, 2015 · 7 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@SimonSapin
Copy link
Contributor

When borrows are involved, it is a common pattern to have a foo_mut variation of foo method. For example, [T] slices have a chunks method that takes &self and returns an iterator of &[T], and a chunks_mut method that is identical except that it takes &mut self and returns an iterator of &mut [T].

The windows method similarly takes &self and return an iterator of &[T]. However, the corresponding windows_mut does not exist, although it could sometimes be useful.

@alexcrichton
Copy link
Member

I think that this would actually be unsound to implement. Each window is overlapping, and I believe that there's currently no guarantee that you can't do something like:

let mut parts = slice.windows_mut(4);
let part1 = parts.next().unwrap();
let part2 = parts.next().unwrap();
// part1/part2 overlap

I believe this is why iterators like IterMut fundamentally require unsafe code, but they're safe at an API level because the borrows they return are always disjoint (hence chunks_mut). I'm not 100% sure that this would be the case though, so I'd want to try it out first.

@SimonSapin
Copy link
Contributor Author

Is it possible to write in a way that the result of next borrows the iterator (not just the slice) so that next can not be called again while the previous item still lives?

@alexcrichton
Copy link
Member

I believe that due to the signature of Iterator, fn next(&mut self) -> Option<T>, there's no possibility to connect T to the lifetime of self.

@steveklabnik steveklabnik added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Mar 27, 2015
@huonw
Copy link
Member

huonw commented Mar 28, 2015

(This is an example of something that could be tackled via "streaming iterators" e.g. https://github.com/emk/rust-streaming .)

@abonander
Copy link
Contributor

@huonw is right but his link is broken 😄

@huonw
Copy link
Member

huonw commented Mar 31, 2015

Ah, github incorrectly included the trailing . in the autolinkification.

@steveklabnik
Copy link
Member

Small API enhancements aren't tracked these days; we instead just accept PRs. Apparently this is blocked on streaming iterators, so a PR can't be sent in right now, but we still don't track random stuff like this. Closing!

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 17, 2023
Mention the lack of `windows_mut` in `windows`

This is a common request, going back to at least 2015 (rust-lang#23783), so mention in the docs that it can't be done and offer a workaround using <https://doc.rust-lang.org/std/cell/struct.Cell.html#method.as_slice_of_cells>.

(See also URLO threads like <https://internals.rust-lang.org/t/a-windows-mut-method-on-slice/16941/10?u=scottmcm>.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants