From: bors Date: Fri, 18 May 2018 21:49:38 +0000 (+0000) Subject: Auto merge of #50319 - nagisa:align_to, r=alexcrichton X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=37a409177c80e6f301c41833fc7ee8fda2412c00;p=rust.git Auto merge of #50319 - nagisa:align_to, r=alexcrichton Implement [T]::align_to Note that this PR deviates from what is accepted by RFC slightly by making `align_offset` to return an offset in elements, rather than bytes. This is necessary to sanely support `[T]::align_to` and also simply makes more sense™. The caveat is that trying to align a pointer of ZST is now an equivalent to `is_aligned` check, rather than anything else (as no number of ZST elements will align a misaligned ZST pointer). It also implements the `align_to` slightly differently than proposed in the RFC to properly handle cases where size of T and U aren’t co-prime. Furthermore, a promise is made that the slice containing `U`s will be as large as possible (contrary to the RFC) – otherwise the function is quite useless. The implementation uses quite a few underhanded tricks and takes advantage of the fact that alignment is a power-of-two quite heavily to optimise the machine code down to something that results in as few known-expensive instructions as possible. Currently calling `ptr.align_offset` with an unknown-at-compile-time `align` results in code that has just a single "expensive" modulo operation; the rest is "cheap" arithmetic and bitwise ops. cc https://github.com/rust-lang/rust/issues/44488 @oli-obk As mentioned in the commit message for align_offset, many thanks go to Chris McDonald. --- 37a409177c80e6f301c41833fc7ee8fda2412c00 diff --cc src/libcore/tests/lib.rs index cd6b5c6a4ad,dbd26b2c718..13189d532ab --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@@ -41,7 -41,10 +41,9 @@@ #![feature(try_from)] #![feature(try_trait)] #![feature(exact_chunks)] + #![feature(slice_align_to)] + #![feature(align_offset)] #![feature(reverse_bits)] -#![feature(inclusive_range_methods)] #![feature(iterator_find_map)] #![feature(slice_internals)]