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

Clarify WindowsMut (Lending)Iterator #135773

Merged
merged 1 commit into from
Jan 27, 2025
Merged

Clarify WindowsMut (Lending)Iterator #135773

merged 1 commit into from
Jan 27, 2025

Conversation

hkBst
Copy link
Contributor

@hkBst hkBst commented Jan 20, 2025

fixes #133628

@rustbot
Copy link
Collaborator

rustbot commented Jan 20, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 20, 2025
@rust-log-analyzer

This comment has been minimized.

Comment on lines 1102 to 1103
/// There's no `windows_mut` Iterator-equivalent of `windows`,
/// (as `[0,1,2].windows_mut(2).collect()` would create 2 mutable references to the middle elements of that array),
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of an example, I think it may be worth giving the reasoning here. For example, "... because the Iterator trait cannot represent the required lifetimes (though this is possible with a [LendingIterator]). However, you can sometimes..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've now lead with this more abstract lifetime reasoning, though I did not cut the counter(!)example, as I think it is a simple to understand but powerful proof of non-existence of windows_mut as an Iterator. Let me know what you think!

@rust-log-analyzer

This comment has been minimized.

Comment on lines 1102 to 1106
/// Because the [Iterator] trait cannot represent the required lifetimes, there is no `windows_mut` analog to `windows`.
/// (If there was, then `[0,1,2].windows_mut(2).collect()` would create 2 mutable references
/// to the middle element of that array, which would violate [the rules of references],
/// though a [LendingIterator] analog is possible.)
/// However, you can sometimes use [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
Copy link
Contributor

Choose a reason for hiding this comment

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

I think things can be simplified a bit: complete thoughts shouldn't be in ( ... ), and I don't think we need to point link to the reference docs (we can probably assume users know that two mutable references to the same thing is bad). So suggested wording, also dropping the LendingIterator mention:

Suggested change
/// Because the [Iterator] trait cannot represent the required lifetimes, there is no `windows_mut` analog to `windows`.
/// (If there was, then `[0,1,2].windows_mut(2).collect()` would create 2 mutable references
/// to the middle element of that array, which would violate [the rules of references],
/// though a [LendingIterator] analog is possible.)
/// However, you can sometimes use [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
/// There is no `windows_mut` analog to `windows` because [`Iterator`] does not represent the
/// required lifetimes, e.g. `[0, 1, 2].windows_mut(2).collect()` would allow creating two mutable
/// references to the middle element of the array. However, you can sometimes use
/// [`Cell::as_slice_of_cells`] (crate::cell::Cell::as_slice_of_cells) in

Then add something like "This pattern can also be achieved with a [LendingIterator]." after the example (to keep the paragraph introducing the example only about std).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regarding complete thought in parens: The second site I looked up gave this example: Tonight's gala is a huge occasion. (Everyone who's somebody will be there.), so I don't quite see what is wrong with my use of parens here. I've used the parens in the sense that you can get more information by reading what's inside them, but you can also safely skip over them, and this way the reference outside of std is not in the unparenthesized text. Upon further reflection I suppose I could move the parens to only go around "though a [LendingIterator] analog is possible".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made the parenthetical more terse and adjusted punctuation. Let me know if you think it helps.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is grammatically correct, but readability tends to be better if parenthesized data is kept brief. If there are complete asides that need to be expressed, it is preferable to put it in a footnote or separate paragraph, or just fold it into the text somehow.

Reword looks fine though 👍

Copy link
Contributor Author

@hkBst hkBst Jan 27, 2025

Choose a reason for hiding this comment

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

Great! :D

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the changes! Please just rewrap the comments to 100 characters and squash.

@hkBst hkBst force-pushed the patch-10 branch 2 times, most recently from 01f49b0 to cc52066 Compare January 27, 2025 09:34
@hkBst
Copy link
Contributor Author

hkBst commented Jan 27, 2025

@tgross35 I've rewrapped text and squashed, but there is one long line with the link to the book that I don't know how to shorten, and am afraid to break up. Let me know if there is anything that can be done there, otherwise I think this is good to go.

Comment on lines 1102 to 1107
/// Because the [Iterator] trait cannot represent the required lifetimes,
/// there is no `windows_mut` analog to `windows`;
/// `[0,1,2].windows_mut(2).collect()` would violate [the rules of references]
/// (though a [LendingIterator] analog is possible).
/// You can sometimes use
/// [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
/// conjunction with `windows` instead:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Because the [Iterator] trait cannot represent the required lifetimes,
/// there is no `windows_mut` analog to `windows`;
/// `[0,1,2].windows_mut(2).collect()` would violate [the rules of references]
/// (though a [LendingIterator] analog is possible).
/// You can sometimes use
/// [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
/// conjunction with `windows` instead:
/// Because the [Iterator] trait cannot represent the required lifetimes, there is no
/// `windows_mut` analog to `windows`; `[0,1,2].windows_mut(2).collect()` would violate
/// [the rules of references] (though a [LendingIterator] analog is possible). You can sometimes
/// use [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in conjunction with
/// `windows` instead:

Looks like it may have gotten chopped up somehow, it should be something like the wrapping here (I wish rustfmt just did this...)

No worries about links, those are find to exceed 100.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did manual line breaking at logical places, which is why it looks a bit ragged.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, makes sense. We tend to avoid semantic line breaks in docs because it's a small benefit for reading diffs but makes reading the code less nice, and the code gets read significantly more than diffs do.

Anyway, thanks for all the follow up here!

@tgross35
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 27, 2025

📌 Commit f630f7f has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 27, 2025
@hkBst
Copy link
Contributor Author

hkBst commented Jan 27, 2025

@tgross35 thanks for reviewing!

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 27, 2025
…llaumeGomez

Rollup of 10 pull requests

Successful merges:

 - rust-lang#135773 (Clarify WindowsMut (Lending)Iterator)
 - rust-lang#135807 (Implement phantom variance markers)
 - rust-lang#135876 (fix doc for std::sync::mpmc)
 - rust-lang#135988 (Add a workaround for parallel rustc crashing when there are delayed bugs)
 - rust-lang#136037 (Mark all NuttX targets as tier 3 target and support the standard library)
 - rust-lang#136064 (Add a suggestion to cast target_feature fn items to fn pointers.)
 - rust-lang#136082 (Incorporate `iter_nodes` into `graph::DirectedGraph`)
 - rust-lang#136112 (Clean up all dead files inside `tests/ui/`)
 - rust-lang#136114 (Use identifiers more in diagnostics code)
 - rust-lang#136118 (Change `collect_and_partition_mono_items` tuple return type to a struct)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b7476f1 into rust-lang:master Jan 27, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 27, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 27, 2025
Rollup merge of rust-lang#135773 - hkBst:patch-10, r=tgross35

Clarify WindowsMut (Lending)Iterator

fixes rust-lang#133628
@hkBst hkBst deleted the patch-10 branch February 2, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clarify statement regarding windows_mut
5 participants