Skip to content

Implement ptr::try_cast_aligned and NonNull::try_cast_aligned. #141222

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
May 21, 2025

Conversation

mathisbot
Copy link
Contributor

@mathisbot mathisbot commented May 18, 2025

Implement three common methods on raw pointers and NonNulls: try_cast_aligned.

Related links

About #[inline]

Since the result of a call to align_of is a power of two known at compile time, the compiler is able to reduce a call to try_cast_aligned to only test and sete (or test and jne if followed by unwrap), at least on every tier 1 target's arch. This seemed like a good reason to #[inline] the function.

@rustbot
Copy link
Collaborator

rustbot commented May 18, 2025

r? @jhpratt

rustbot has assigned @jhpratt.
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 May 18, 2025
@mathisbot
Copy link
Contributor Author

@rustbot label +T-libs-api -T-libs

r? @ghost

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 18, 2025
@rust-log-analyzer

This comment has been minimized.

@hanna-kruppe
Copy link
Contributor

#[inline] makes sense and shouldn’t do any harm (may not even be necessary, though). #[inline(always)] shouldn’t be needed and only generates more pointless work for LLVM at opt-level=0.

@mathisbot
Copy link
Contributor Author

r? libs-api

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.

Two small requests and please squash, then lgtm

@tgross35 tgross35 assigned tgross35 and unassigned BurntSushi May 20, 2025
@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 20, 2025
@mathisbot mathisbot force-pushed the ptr_trycastaligned branch from 8cb6727 to bb6afec Compare May 20, 2025 20:47
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 20, 2025
@mathisbot mathisbot requested a review from tgross35 May 20, 2025 20:48
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.

Changes lgtm but would you mind cleaning up the commit message? It's a bit nonsensical (assuming this came from an autosquash)

Implement `ptr::try_cast_aligned`

`ptr::try_cast_aligned`: link tracking issue

`ptr::try_cast_aligned`: remove forgotten `const`

`NonNull::try_cast_aligned`: fix doctest

`ptr::try_cast_aligned`: #[inline]

ptr::`try_cast_aligned`: fix doc

Implement `ptr::try_cast_aligned` and `NonNull::try_cast_aligned`.

You should just be able to run git commit --amend and delete everything but the first (or last) line, then git push --force-with-lease.

@mathisbot mathisbot force-pushed the ptr_trycastaligned branch from bb6afec to 9d1cf12 Compare May 20, 2025 20:51
@mathisbot
Copy link
Contributor Author

Did it work this time ? 😅

@tgross35
Copy link
Contributor

Great, thank you!

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented May 20, 2025

📌 Commit 9d1cf12 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 May 20, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request May 21, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#140981 (Add match guard let chain drop order and scoping tests)
 - rust-lang#141042 (ci: split powerpc64le-linux job)
 - rust-lang#141078 (ci: split dist-arm-linux job)
 - rust-lang#141222 (Implement `ptr::try_cast_aligned` and `NonNull::try_cast_aligned`.)
 - rust-lang#141308 (Do not call name() on rpitit assoc_item)
 - rust-lang#141316 (Update books)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit bb7291e into rust-lang:master May 21, 2025
6 checks passed
@rustbot rustbot added this to the 1.89.0 milestone May 21, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 21, 2025
Rollup merge of rust-lang#141222 - mathisbot:ptr_trycastaligned, r=tgross35

Implement `ptr::try_cast_aligned` and `NonNull::try_cast_aligned`.

Implement three common methods on raw pointers and `NonNull`s: `try_cast_aligned`.

## Related links

- Tracking Issue: rust-lang#141221

## About `#[inline]`

Since the result of a call to `align_of` is a power of two known at compile time, the compiler is able to reduce a call to `try_cast_aligned` to only test and sete (or test and jne if followed by `unwrap`), at least on every tier 1 target's arch. This seemed like a good reason to `#[inline]` the function.

- https://godbolt.org/z/ocehvPWMx (raw inlining)
- https://godbolt.org/z/3qa4j4Yrn (comparison with no inlining)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 22, 2025
…rovenance, r=tgross35

try_cast_aligned: avoid bare int-to-ptr casts

This fixes a CI failure in https://github.com/rust-lang/miri-test-libstd caused by strict provenance violations in doctests added in rust-lang#141222.

r? `@tgross35`
Cc `@mathisbot`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 22, 2025
Rollup merge of rust-lang#141381 - RalfJung:try_cast_aligned-strict-provenance, r=tgross35

try_cast_aligned: avoid bare int-to-ptr casts

This fixes a CI failure in https://github.com/rust-lang/miri-test-libstd caused by strict provenance violations in doctests added in rust-lang#141222.

r? `@tgross35`
Cc `@mathisbot`
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-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.

8 participants