impl more traits for ptr::Alignment, add mask method by clarfonthey · Pull Request #115249 · rust-lang/rust (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation29 Commits1 Checks0 Files changed

Conversation

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

[ Show hidden characters]({{ revealButtonHref }})

clarfonthey

Changes:

No longer included in PR:

Tracking issue for ptr::Alignment: #102070

@rustbot

r? @cuviper

(rustbot has picked a reviewer for you, use r? to override)

@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

Aug 26, 2023

clarfonthey

}
impl Alignment {
// longest string: 1p-128

Choose a reason for hiding this comment

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

even though no targets have 128-bit usize, I figured I might as well future-proof it

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

scottmcm

#[inline]
pub const fn mask(self) -> usize {
// SAFETY: The alignment is always nonzero, and therefore decrementing won't overflow.
!(unsafe { self.as_usize().unchecked_sub(1) })

Choose a reason for hiding this comment

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

Sadly, unchecked_sub(1) is useless on unsigned types llvm/llvm-project#53377

I guess it's fine to leave it, though, since it's clearly correct. And trying to do it in isize would be UB.

Choose a reason for hiding this comment

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

Oh, that's unfortunate. I even was considering at some point adding a decrement() method to NonZeroU* that could use unchecked_sub since it would never overflow, but I guess that that also wouldn't work.

scottmcm

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl fmt::Display for Alignment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.as_nonzero(), f)

Choose a reason for hiding this comment

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

Pondering: I have no idea when we should be including Display on types. I guess it's not a problem, but it feels like alignment isn't something I'd usually be showing to people. But showing it in binary or hex seems logical, so maybe display is ok too.

Choose a reason for hiding this comment

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

Yeah, that was my thought too. It's less that it's necessarily a common operation, but more that it's well-defined and not a whole lot of extra code to do. I'm fine removing it if it seems too weird.

scottmcm

@rust-log-analyzer

This comment has been minimized.

@scottmcm

r? @scottmcm

Would you be willing to split this change? I feel like it's got a bunch of things that are easy to take, but also some unprecedented things (as you note in the op), and I'd be happy to just merge the former to try to make progress here for you, since this has been open a while.

If you make this PR be these bits:

Then I'd be happy to just r+ that, and we can leave the question of Display and Exp and such to libs-api for their opinions in another PR.

@rustbot author

@rustbot rustbot 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

Oct 13, 2023

@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

Oct 13, 2023

@bors

bors added a commit to rust-lang-ci/rust that referenced this pull request

Oct 14, 2023

@bors

impl more traits for ptr::Alignment, add mask method

Changes:

No longer included in PR:

Tracking issue for ptr::Alignment: rust-lang#102070

@rust-log-analyzer

This comment has been minimized.

@bors

@bors bors added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

and removed S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

labels

Oct 14, 2023

scottmcm

/// assert_eq!(four.mask(Alignment::of::().mask()), four);
/// assert_eq!(four.mask(Alignment::of::().mask()), four);
/// assert_eq!(four.mask(Alignment::of::().mask()), four);
/// assert_ne!(four.mask(Alignment::of::().mask()), four);

Choose a reason for hiding this comment

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

Looks like you need to not make assumptions about the alignment of primitives like this in these tests. There's no guarantee for alignof(u64), and on i686 it looks like it's 4. Maybe these demos should use Alignment::new or have specific repr(align(…)) types, rather than get them from primitives?

@bors r-

@bors bors 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

Oct 14, 2023

@clarfonthey

I don't like it, but I updated the example to have explicitly-aligned structs.

Right now, it's operating way more as a test than an example of behaviour, but that can be resolved during stabilisation.

@clarfonthey

Ping @scottmcm when you have time to take a look at this

@Dylan-DPC Dylan-DPC added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Nov 6, 2023

@clarfonthey

@clarfonthey

@scottmcm

Oops, thanks for the ping!

@bors r+ rollup=iffy (had a per-target failure last time)

@bors

📌 Commit 114873d has been approved by scottmcm

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

Nov 18, 2023

@bors

@bors

@rust-timer

Finished benchmarking commit (61d3b26): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌ (primary) - - 0
Regressions ❌ (secondary) 3.9% [2.4%, 4.9%] 6
Improvements ✅ (primary) - - 0
Improvements ✅ (secondary) - - 0
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌ (primary) - - 0
Regressions ❌ (secondary) 2.4% [1.8%, 2.9%] 10
Improvements ✅ (primary) - - 0
Improvements ✅ (secondary) - - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 675.975s -> 678.192s (0.33%)
Artifact size: 313.82 MiB -> 313.84 MiB (0.01%)

Labels

merged-by-bors

This PR was explicitly merged by bors.

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.