-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Clarify meaning of ranges in pointer offset docs #156666
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| Subtracts an unsigned offset from a pointer. | ||
|
|
||
| This can only move the pointer backward (or not move it). If you need to move forward or | ||
| backward depending on the value, then you might want [`offset`](#method.offset) instead | ||
| which takes a signed offset. | ||
|
|
||
| `count` is in units of T; e.g., a `count` of 3 represents a pointer | ||
| offset of `3 * size_of::<T>()` bytes. | ||
|
|
||
| # Safety | ||
|
|
||
| If any of the following conditions are violated, the result is Undefined Behavior: | ||
|
|
||
| * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without | ||
| "wrapping around"), must fit in an `isize`. | ||
|
|
||
| * Let `result` be `self.addr() - count * size_of::<T>()`, computed on mathematical integers. | ||
| This must fit in a `usize`. | ||
|
|
||
| * If the computed offset is non-zero, then `self` must be [derived from][crate::ptr#provenance] a pointer to some | ||
| [allocation], and the entire memory range between `self` and `result` | ||
| (i.e., `result..self.addr()`) must be in bounds of that allocation. | ||
|
|
||
| Allocations can never be larger than `isize::MAX` bytes and they can only contain addresses | ||
| representable by `usize`, so technically the last condition implies the first two. | ||
|
|
||
| [allocation]: crate::ptr#allocation | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm misunderstanding what you mean by this, doesn't this mean
count * size_of::<T>()later on is effectivelyN * size_of::<T>() * size_of::<T>(), which is obviously incorrect? On its own, the safety section looks fine, but the addition of this callout seems unusual.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what you mean, and I didn't even change this text. Where are we multiplying twice? We are just defining "offset in bytes" twice, once by example in the introduction and once more generally in the safety docs.
This is the equivalent of
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bit I highlighted sounds like you're defining
count, not "offset in bytes".Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read this 5 times now and I cannot figure out which part of it sounds to you like it defines
count. It defines how to interpretcount.countis a function argument given by the user, we cannot define it.Since this is the same wording we already have on stable and had for many years, can we move this discussion into an issue or so? It's not really about the changes this PR proposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I didn't realize that was pre-existing. I don't care enough to open an issue for this; it's moreso that I found it confusing at first read.
@bors r+ rollup