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.
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.
Do the panics get eliminated when the types are exact multiples in size and alignment?
There was a problem hiding this comment.
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.
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