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

Document safety in memchr, remove unsafe. #76488

Closed
wants to merge 1 commit into from
Closed

Document safety in memchr, remove unsafe. #76488

wants to merge 1 commit into from

Conversation

moonheart08
Copy link
Contributor

@moonheart08 moonheart08 commented Sep 8, 2020

Documents the safety of two unsafe blocks and removes an unnecessary one.
I kindly ask someone take a closer look at lines 100..106 to make sure I didn't calculate the aligned offsets wrong. I ran the test suite with and without an alignment assert included so they should be correct.

Documents the safety of two unsafe blocks and removes an unnecessary one
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 8, 2020
@jyn514 jyn514 added T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Sep 9, 2020
} else {
let (lower, rest) = text.split_at(offset);
(lower.len(), len - rest.align_to_offsets::<(Chunk, Chunk)>().1)
}
Copy link
Member

Choose a reason for hiding this comment

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

I think the previous code is better in this case. Even though it's unsafe, it is much clearer (IMO) than this replication of the body of align_to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Mark-Simulacrum One of the main reasons I removed it is because I was having some trouble figuring out the best way to document it. Alongside that, it wasn't immediately clear to me what align_to did, and why it was being used instead of some other more specialized function (which doesn't exist, in fact. shame.)

Copy link
Member

Choose a reason for hiding this comment

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

Hm, so the documentation on align_to is fairly clear to my eyes, but if you're not understanding it then we should definitely improve it! It's sort of a "core primitive" in my eyes for this sort of task. In the case of &[u8] and usize as the middle type, align_to is also safe in all cases, because transmutes of byte slices to slices of usize, when respecting alignment (and length), are always safe.

@Mark-Simulacrum
Copy link
Member

r? @Mark-Simulacrum

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 13, 2020
@Mark-Simulacrum
Copy link
Member

It looks like another (closer to what I would prefer) version is up here: #76688 (comment)

Up to you, but we can close this - I'm happy to merge whichever.

@moonheart08
Copy link
Contributor Author

moonheart08 commented Sep 14, 2020

This should be ready, though if you prefer the one with no unsafe removed, that's fine.
ADDENDUM: I personally feel removing the unsafe is one less unsafe block to keep track of and understand in the codebase, which is why I did it in the first place

@Mark-Simulacrum
Copy link
Member

I disagree that "one less unsafe" outweighs the costs of the non-trivial inlined algorithm from align_to. Once safe transmute work progress I imagine we'll probably think about adding align_to_safe (probably with a different name) that could be used here.

@JohnCSimon
Copy link
Member

Ping from triage:
@moonheart08 - this has sat idle for the last fifteen days - do you want to close this?

@moonheart08
Copy link
Contributor Author

Ah, shoot. Sorry for forgetting about the PR. I'll make requested changes when I have time.

@bors
Copy link
Contributor

bors commented Oct 1, 2020

☔ The latest upstream changes (presumably #76971) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@moonheart08
Copy link
Contributor Author

time has not been in my favor.

@moonheart08 moonheart08 closed this Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants