-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
impl more traits for ptr::Alignment, add mask method #115249
Conversation
r? @cuviper (rustbot has picked a reviewer for you, use r? to override) |
library/core/src/ptr/alignment.rs
Outdated
} | ||
|
||
impl Alignment { | ||
// longest string: 1p-128 |
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.
even though no targets have 128-bit usize, I figured I might as well future-proof it
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
#[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) }) |
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.
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.
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.
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.
library/core/src/ptr/alignment.rs
Outdated
#[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) |
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.
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.
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.
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.
ac74183
to
f8cd316
Compare
This comment has been minimized.
This comment has been minimized.
f8cd316
to
9227f94
Compare
9227f94
to
26b97a2
Compare
26b97a2
to
5c7f096
Compare
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 @rustbot author |
6651cb6
to
644e025
Compare
impl more traits for ptr::Alignment, add mask method Changes: * Adds `rustc_const_unstable` attributes where missing * Makes `log2` method const * Adds `mask` method * Implements `Default`, which is equivalent to `Alignment::MIN` No longer included in PR: * Removes indirection of `AlignmentEnum` type alias (this was intentional) * Implements `Display`, `Binary`, `Octal`, `LowerHex`, and `UpperHex` (should go through libs-api instead) * Controversially implements `LowerExp` and `UpperExp` using `p` instead of `e` to indicate a power of 2 (also should go through libs-api) Tracking issue for `ptr::Alignment`: rust-lang#102070
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
library/core/src/ptr/alignment.rs
Outdated
/// assert_eq!(four.mask(Alignment::of::<u8>().mask()), four); | ||
/// assert_eq!(four.mask(Alignment::of::<u16>().mask()), four); | ||
/// assert_eq!(four.mask(Alignment::of::<u32>().mask()), four); | ||
/// assert_ne!(four.mask(Alignment::of::<u64>().mask()), four); |
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.
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-
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. |
Ping @scottmcm when you have time to take a look at this |
1d840e2
to
3a8ec1c
Compare
Ping @scottmcm again when you're free. |
3a8ec1c
to
114873d
Compare
Oops, thanks for the ping! @bors r+ rollup=iffy (had a per-target failure last time) |
☀️ Test successful - checks-actions |
Finished benchmarking commit (61d3b26): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis 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.
CyclesResultsThis 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.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 675.975s -> 678.192s (0.33%) |
Changes:
rustc_const_unstable
attributes where missinglog2
method constmask
methodDefault
, which is equivalent toAlignment::MIN
No longer included in PR:
AlignmentEnum
type alias (this was intentional)Display
,Binary
,Octal
,LowerHex
, andUpperHex
(should go through libs-api instead)LowerExp
andUpperExp
usingp
instead ofe
to indicate a power of 2 (also should go through libs-api)Tracking issue for
ptr::Alignment
: #102070