-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add stronger alternatives to align_to
#105296
Conversation
This comment has been minimized.
This comment has been minimized.
One fn seems to change to bigger elements and the other to smaller elements. I'm not clear why it's two separate functions, and also the naming doesn't really suggest which is which. |
Minor naming thought: Would it be worth leaving space for a future safe-transmute version to have the "ideal" name, and have the unsafe versions be (Perhaps |
the |
FWIW, I'm leaning towards suggesting that the safe transmute APIs use the word "reinterpret" instead of "transmute". |
@jswrenn Sounds great -- I've long been a fan of that choice :) |
0ca73d3
to
2933ad9
Compare
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
@rustbot label +T-libs-api -T-libs |
r? libs-api |
#[must_use] | ||
#[unstable(feature = "slice_align_to_ish", issue = "none")] | ||
#[inline] | ||
pub fn aligned_subslice(&self, align: usize) -> &[T] { |
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.
Could the align
be const? Does one ever need dynamic alignment?
if size_u > size_t { | ||
assert!( | ||
self.len() * size_u % size_t == 0, | ||
"input slice does not fit exactly into a slice of the output element type" | ||
); | ||
} else { | ||
assert!( | ||
self.len() * size_t % size_u == 0, | ||
"input slice does not fit exactly into a slice of the output element type" | ||
); | ||
} |
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.
Do the panics get eliminated when the types are exact multiples in size and alignment?
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.
no, I should optimize that.
Hello @oli-obk! I just want to ping you as part of the triage procedure to let you know that this PR has received reviews :) |
The PR is blocked on rust-lang/libs-team#164 |
align % size == 0, | ||
"aligned_subslice only works for alignments that are multiples of the element's size" | ||
); | ||
let offset = self.as_ptr().addr() % align; |
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.
If this align
is ever a non-constant it'll have to actually emit the division.
Consider taking https://doc.rust-lang.org/std/ptr/struct.Alignment.html instead so you can optimize based on it always being a power of two.
r? @ghost
some code for the discussion
ACP: rust-lang/libs-team#164